| 1 | #include "Writer.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream.h> // cout
|
|---|
| 4 | #include <fstream.h> // ofstream
|
|---|
| 5 |
|
|---|
| 6 | #include <stdio.h> // FILE
|
|---|
| 7 | #include <png.h>
|
|---|
| 8 |
|
|---|
| 9 | #include "MTime.h"
|
|---|
| 10 |
|
|---|
| 11 | ClassImp(Writer);
|
|---|
| 12 |
|
|---|
| 13 | void Writer::Png(const char *fname, const byte *buf,
|
|---|
| 14 | struct timeval *date)
|
|---|
| 15 | {
|
|---|
| 16 | MTime t(*date);
|
|---|
| 17 | TString mjd;
|
|---|
| 18 | mjd += t.GetMjd()-52000;
|
|---|
| 19 | mjd = mjd.Strip(TString::kBoth);
|
|---|
| 20 | if (mjd.Length()<10)
|
|---|
| 21 | mjd.Append('0', 10-mjd.Length());
|
|---|
| 22 |
|
|---|
| 23 | TString name = fname;
|
|---|
| 24 | name += "_";
|
|---|
| 25 | name += mjd;
|
|---|
| 26 | name += ".png";
|
|---|
| 27 |
|
|---|
| 28 | cout << "Writing PNG '" << name << "'" << endl;
|
|---|
| 29 |
|
|---|
| 30 | //
|
|---|
| 31 | // open file
|
|---|
| 32 | //
|
|---|
| 33 | FILE *fd = fopen(name, "w");
|
|---|
| 34 | if (!fd)
|
|---|
| 35 | {
|
|---|
| 36 | cout << "Warning: Cannot open file for writing." << endl;
|
|---|
| 37 | return;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | //
|
|---|
| 41 | // allocate memory
|
|---|
| 42 | //
|
|---|
| 43 | png_structp fPng = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
|---|
| 44 | NULL, NULL, NULL);
|
|---|
| 45 |
|
|---|
| 46 | if (!fPng)
|
|---|
| 47 | {
|
|---|
| 48 | cout << "Warning: Unable to create PNG structure" << endl;
|
|---|
| 49 | fclose(fd);
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | png_infop fInfo = png_create_info_struct(fPng);
|
|---|
| 55 |
|
|---|
| 56 | if (!fInfo)
|
|---|
| 57 | {
|
|---|
| 58 | cout << "Warning: Unable to create PNG info structure" << endl;
|
|---|
| 59 | png_destroy_write_struct (&fPng, NULL);
|
|---|
| 60 | fclose(fd);
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | fInfo->width = 768;
|
|---|
| 65 | fInfo->height = 576;
|
|---|
| 66 | fInfo->bit_depth = 8;
|
|---|
| 67 | fInfo->color_type = PNG_COLOR_TYPE_GRAY;
|
|---|
| 68 |
|
|---|
| 69 | //
|
|---|
| 70 | // set jump-back point in case of errors
|
|---|
| 71 | //
|
|---|
| 72 | if (setjmp(fPng->jmpbuf))
|
|---|
| 73 | {
|
|---|
| 74 | cout << "longjmp Warning: PNG encounterd an error!" << endl;
|
|---|
| 75 | png_destroy_write_struct (&fPng, &fInfo);
|
|---|
| 76 | fclose(fd);
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | //
|
|---|
| 81 | // connect file to PNG-Structure
|
|---|
| 82 | //
|
|---|
| 83 | png_init_io(fPng, fd);
|
|---|
| 84 |
|
|---|
| 85 | // png_set_compression_level (fPng, Z_BEST_COMPRESSION);
|
|---|
| 86 |
|
|---|
| 87 | //
|
|---|
| 88 | // Write header
|
|---|
| 89 | //
|
|---|
| 90 | png_write_info (fPng, fInfo);
|
|---|
| 91 |
|
|---|
| 92 | //
|
|---|
| 93 | // Write Time Chunks
|
|---|
| 94 | //
|
|---|
| 95 | /*
|
|---|
| 96 | if (date)
|
|---|
| 97 | {
|
|---|
| 98 | char text[36];
|
|---|
| 99 |
|
|---|
| 100 | Timer timet(date);
|
|---|
| 101 | sprintf(text, "*** %s ***", timet.GetTimeStr());
|
|---|
| 102 | png_write_chunk(fPng, (png_byte*)"UTC", (png_byte*)text, strlen(text));
|
|---|
| 103 | sprintf(text,"*** %s %s %.1f %i ***", tzname[0], tzname[1], 1.0/3600*timezone, daylight);
|
|---|
| 104 | png_write_chunk(fPng, (png_byte*)"ZONE", (png_byte*)text, strlen(text));
|
|---|
| 105 | }
|
|---|
| 106 | */
|
|---|
| 107 |
|
|---|
| 108 | //
|
|---|
| 109 | // Write bitmap data
|
|---|
| 110 | //
|
|---|
| 111 | for (unsigned int y=0; y<768*576; y+=768)
|
|---|
| 112 | png_write_row (fPng, (png_byte*)buf+y);
|
|---|
| 113 |
|
|---|
| 114 | //
|
|---|
| 115 | // Write footer
|
|---|
| 116 | //
|
|---|
| 117 | png_write_end (fPng, fInfo);
|
|---|
| 118 |
|
|---|
| 119 | //
|
|---|
| 120 | // free memory
|
|---|
| 121 | //
|
|---|
| 122 | png_destroy_write_struct (&fPng, &fInfo);
|
|---|
| 123 |
|
|---|
| 124 | fclose(fd);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void Writer::Ppm(const char *fname, const byte *img, struct timeval *date)
|
|---|
| 128 | {
|
|---|
| 129 | TString name = fname;
|
|---|
| 130 |
|
|---|
| 131 | MTime t(*date);
|
|---|
| 132 |
|
|---|
| 133 | name += "_";
|
|---|
| 134 | name += t.GetMjd()-52000;
|
|---|
| 135 | name += ".ppm";
|
|---|
| 136 |
|
|---|
| 137 | cout << "Writing PPM '" << name << "'" << endl;
|
|---|
| 138 |
|
|---|
| 139 | //
|
|---|
| 140 | // open file for writing
|
|---|
| 141 | //
|
|---|
| 142 | ofstream fout(name);
|
|---|
| 143 | if (!fout)
|
|---|
| 144 | {
|
|---|
| 145 | cout << "Warning: Cannot open file for writing." << endl;
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | //
|
|---|
| 150 | // write buffer to file
|
|---|
| 151 | //
|
|---|
| 152 | fout << "P6\n768 576\n255\n";
|
|---|
| 153 | for (byte const *buf = img; buf < img+768*576; buf++)
|
|---|
| 154 | fout << *buf << *buf << *buf;
|
|---|
| 155 | }
|
|---|