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