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