source: branches/FACT++_lidctrl_new_eth/drive/Writer.cc@ 19937

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