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

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