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