source: trunk/MagicSoft/Cosy/videodev/Writer.cc@ 8709

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