Debian Codegraph @

gphoto_readimage.cpp

raw
1
2 #include "gphoto_readimage.h"
3
4 #include <indilogger.h>
5
6 #include <jpeglib.h>
7 #include <fitsio.h>
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
10 // the older libraw uses auto_ptr
11 #include <libraw.h>
12 #pragma GCC diagnostic pop
13
14
15 #include <unistd.h>
16 #include <arpa/inet.h>
17
18
19 char dcraw_cmd[] = "dcraw";
20 char device[64];
21
22 #define err_printf IDLog,
23 struct dcraw_header
24 {
25 time_t time;
26 float exposure;
27 int width;
28 int height;
29 int cfa_type;
30 float wbr;
31 float wbg;
32 float wbgp;
33 float wbb;
34 };
35
36 enum
37 {
38 CFA_RGGB,
39 };
40
41 void gphoto_read_set_debug(const char *name)
42 {
43 strncpy(device, name, 64);
44 }
45
46 void *tstrealloc(void *ptr, size_t size)
47 {
48 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Realloc: %lu", (unsigned long)size);
49 return realloc(ptr, size);
50 }
51
52 void addFITSKeywords(fitsfile *fptr)
53 {
54 int status = 0;
55
56 /* TODO add other data later */
57 fits_write_date(fptr, &status);
58 }
59
60 int dcraw_parse_time(char *month, int day, int year, char *timestr)
61 {
62 char mon_map[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
63 struct tm tm;
64 int i;
65
66 memset(&tm, 0, sizeof(struct tm));
67
68 for (i = 0; i < 12; i++)
69 {
70 if (strncmp(month, mon_map[i], 3) == 0)
71 {
72 tm.tm_mon = i;
73 break;
74 }
75 }
76 tm.tm_year = year - 1900;
77 tm.tm_mday = day;
78
79 sscanf(timestr, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
80 return mktime(&tm);
81 }
82
83 int dcraw_parse_header_info(const char *filename, struct dcraw_header *header)
84 {
85 FILE *handle;
86 char line[256], cfa[80];
87 char *cmd, timestr[10], month[10], daystr[10];
88 int day, year;
89 float r, g, b, gp;
90 int ret = 0;
91
92 memset(header, 0, sizeof(struct dcraw_header));
93 ret = asprintf(&cmd, "%s -i -t 0 -v %s 2> /dev/null", dcraw_cmd, filename);
94 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%s (%d)", cmd, ret);
95 handle = popen(cmd, "r");
96 free(cmd);
97 if (handle == nullptr)
98 {
99 return 1;
100 }
101
102 while (fgets(line, sizeof(line), handle))
103 {
104 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "%s", line);
105
106 if (sscanf(line, "Timestamp: %s %s %d %s %d", daystr, month, &day, timestr, &year))
107 header->time = dcraw_parse_time(month, day, year, timestr);
108 else if (sscanf(line, "Shutter: 1/%f sec", &header->exposure))
109 header->exposure = 1.0 / header->exposure;
110 else if (sscanf(line, "Shutter: %f sec", &header->exposure))
111 ;
112 /*#ifdef USE_THUMB_SIZE
113 else if (sscanf(line, "Thumb size: %d x %d", &header->width, &header->height) )
114 ;
115 #else*/
116 else if (sscanf(line, "Output size: %d x %d", &header->width, &header->height))
117 ;
118 //#endif
119 else if (sscanf(line, "Filter pattern: %s", cfa))
120 {
121 if (strncmp(cfa, "RGGBRGGBRGGBRGGB", sizeof(cfa)) == 0)
122 {
123 header->cfa_type = CFA_RGGB;
124 }
125 }
126 else if (sscanf(line, "Camera multipliers: %f %f %f %f", &r, &g, &b, &gp) && r > 0.0)
127 {
128 header->wbr = 1.0;
129 header->wbg = g / r;
130 header->wbgp = gp / r;
131 header->wbb = b / r;
132 }
133 }
134
135 pclose(handle);
136 return 0;
137 }
138
139 int read_libraw(const char *filename, uint8_t **memptr, size_t *memsize, int *n_axis, int *w, int *h, int *bitsperpixel,
140 char *bayer_pattern)
141 {
142 int ret = 0;
143 // Creation of image processing object
144 LibRaw RawProcessor;
145
146 // Let us open the file
147 if ((ret = RawProcessor.open_file(filename)) != LIBRAW_SUCCESS)
148 {
149 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Cannot open %s: %s", filename, libraw_strerror(ret));
150 RawProcessor.recycle();
151 return -1;
152 }
153
154 // Let us unpack the image
155 if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
156 {
157 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Cannot unpack %s: %s", filename, libraw_strerror(ret));
158 RawProcessor.recycle();
159 return -1;
160 }
161
162 // Covert to image
163 if ((ret = RawProcessor.raw2image()) != LIBRAW_SUCCESS)
164 {
165 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "Cannot convert %s : %s", filename, libraw_strerror(ret));
166 RawProcessor.recycle();
167 return -1;
168 }
169
170 *n_axis = 2;
171 *w = RawProcessor.imgdata.rawdata.sizes.width;
172 *h = RawProcessor.imgdata.rawdata.sizes.height;
173 *bitsperpixel = 16;
174 // cdesc contains counter-clock wise e.g. RGBG CFA pattern while we want it sequential as RGGB
175 bayer_pattern[0] = RawProcessor.imgdata.idata.cdesc[RawProcessor.COLOR(0, 0)];
176 bayer_pattern[1] = RawProcessor.imgdata.idata.cdesc[RawProcessor.COLOR(0, 1)];
177 bayer_pattern[2] = RawProcessor.imgdata.idata.cdesc[RawProcessor.COLOR(1, 0)];
178 bayer_pattern[3] = RawProcessor.imgdata.idata.cdesc[RawProcessor.COLOR(1, 1)];
179 bayer_pattern[4] = '\0';
180
181 int first_visible_pixel = RawProcessor.imgdata.rawdata.sizes.raw_width * RawProcessor.imgdata.sizes.top_margin +
182 RawProcessor.imgdata.sizes.left_margin;
183
184 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
185 "read_libraw: raw_width: %d top_margin %d left_margin %d first_visible_pixel %d",
186 RawProcessor.imgdata.rawdata.sizes.raw_width, RawProcessor.imgdata.sizes.top_margin,
187 RawProcessor.imgdata.sizes.left_margin, first_visible_pixel);
188
189 *memsize = RawProcessor.imgdata.rawdata.sizes.width * RawProcessor.imgdata.rawdata.sizes.height * sizeof(uint16_t);
190 *memptr = static_cast<uint8_t *>(IDSharedBlobRealloc(*memptr, *memsize));
191 if (*memptr == nullptr)
192 *memptr = static_cast<uint8_t *>(IDSharedBlobAlloc(*memsize));
193 if (*memptr == nullptr)
194 {
195 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "%s: Failed to allocate %d bytes of memory!", __PRETTY_FUNCTION__, *memsize);
196 return -1;
197 }
198
199 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG,
200 "read_libraw: rawdata.sizes.width: %d rawdata.sizes.height %d memsize %d bayer_pattern %s",
201 RawProcessor.imgdata.rawdata.sizes.width, RawProcessor.imgdata.rawdata.sizes.height, *memsize,
202 bayer_pattern);
203
204 uint16_t *image = reinterpret_cast<uint16_t *>(*memptr);
205 uint16_t *src = RawProcessor.imgdata.rawdata.raw_image + first_visible_pixel;
206
207 for (int i = 0; i < RawProcessor.imgdata.rawdata.sizes.height; i++)
208 {
209 memcpy(image, src, RawProcessor.imgdata.rawdata.sizes.width * 2);
210 image += RawProcessor.imgdata.rawdata.sizes.width;
211 src += RawProcessor.imgdata.rawdata.sizes.raw_width;
212 }
213
214 return 0;
215 }
216
217 int read_jpeg(const char *filename, uint8_t **memptr, size_t *memsize, int *naxis, int *w, int *h)
218 {
219 unsigned char *r_data = nullptr, *g_data = nullptr, *b_data = nullptr;
220
221 /* these are standard libjpeg structures for reading(decompression) */
222 struct jpeg_decompress_struct cinfo;
223 struct jpeg_error_mgr jerr;
224 /* libjpeg data structure for storing one row, that is, scanline of an image */
225 JSAMPROW row_pointer[1] = { nullptr };
226
227 FILE *infile = fopen(filename, "rb");
228
229 if (!infile)
230 {
231 DEBUGFDEVICE(device, INDI::Logger::DBG_DEBUG, "Error opening jpeg file %s!", filename);
232 return -1;
233 }
234 /* here we set up the standard libjpeg error handler */
235 cinfo.err = jpeg_std_error(&jerr);
236 /* setup decompression process and source, then read JPEG header */
237 jpeg_create_decompress(&cinfo);
238 /* this makes the library read from infile */
239 jpeg_stdio_src(&cinfo, infile);
240 /* reading the image header which contains image information */
241 jpeg_read_header(&cinfo, (boolean)TRUE);
242
243 /* Start decompression jpeg here */
244 jpeg_start_decompress(&cinfo);
245
246 *memsize = cinfo.output_width * cinfo.output_height * cinfo.num_components;
247 *memptr = static_cast<uint8_t *>(IDSharedBlobRealloc(*memptr, *memsize));
248 if (*memptr == nullptr)
249 *memptr = static_cast<uint8_t *>(IDSharedBlobAlloc(*memsize));
250 if (*memptr == nullptr)
251 {
252 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "%s: Failed to allocate %d bytes of memory!", __PRETTY_FUNCTION__, *memsize);
253 return -1;
254 }
255 // if you do some ugly pointer math, remember to restore the original pointer or some random crashes will happen. This is why I do not like pointers!!
256 uint8_t *oldmem = *memptr;
257 *naxis = cinfo.num_components;
258 *w = cinfo.output_width;
259 *h = cinfo.output_height;
260
261 /* now actually read the jpeg into the raw buffer */
262 row_pointer[0] = (unsigned char *)malloc(cinfo.output_width * cinfo.num_components);
263 if (cinfo.num_components)
264 {
265 r_data = (unsigned char *)*memptr;
266 g_data = r_data + cinfo.output_width * cinfo.output_height;
267 b_data = r_data + 2 * cinfo.output_width * cinfo.output_height;
268 }
269 /* read one scan line at a time */
270 for (unsigned int row = 0; row < cinfo.image_height; row++)
271 {
272 unsigned char *ppm8 = row_pointer[0];
273 jpeg_read_scanlines(&cinfo, row_pointer, 1);
274
275 if (cinfo.num_components == 3)
276 {
277 for (unsigned int i = 0; i < cinfo.output_width; i++)
278 {
279 *r_data++ = *ppm8++;
280 *g_data++ = *ppm8++;
281 *b_data++ = *ppm8++;
282 }
283 }
284 else
285 {
286 memcpy(*memptr, ppm8, cinfo.output_width);
287 *memptr += cinfo.output_width;
288 }
289 }
290
291 /* wrap up decompression, destroy objects, free pointers and close open files */
292 jpeg_finish_decompress(&cinfo);
293 jpeg_destroy_decompress(&cinfo);
294
295 if (row_pointer[0])
296 free(row_pointer[0]);
297 if (infile)
298 fclose(infile);
299
300 *memptr = oldmem;
301
302 return 0;
303 }
304
305 int read_jpeg_mem(unsigned char *inBuffer, unsigned long inSize, uint8_t **memptr, size_t *memsize, int *naxis, int *w,
306 int *h)
307 {
308 /* these are standard libjpeg structures for reading(decompression) */
309 struct jpeg_decompress_struct cinfo;
310 struct jpeg_error_mgr jerr;
311 /* libjpeg data structure for storing one row, that is, scanline of an image */
312 JSAMPROW row_pointer[1] = { nullptr };
313
314 /* here we set up the standard libjpeg error handler */
315 cinfo.err = jpeg_std_error(&jerr);
316 /* setup decompression process and source, then read JPEG header */
317 jpeg_create_decompress(&cinfo);
318 /* this makes the library read from infile */
319 jpeg_mem_src(&cinfo, inBuffer, inSize);
320
321 /* reading the image header which contains image information */
322 jpeg_read_header(&cinfo, (boolean)TRUE);
323
324 /* Start decompression jpeg here */
325 jpeg_start_decompress(&cinfo);
326
327 *memsize = cinfo.output_width * cinfo.output_height * cinfo.num_components;
328 *memptr = static_cast<uint8_t *>(IDSharedBlobRealloc(*memptr, *memsize));
329 if (*memptr == nullptr)
330 *memptr = static_cast<uint8_t *>(IDSharedBlobAlloc(*memsize));
331 if (*memptr == nullptr)
332 {
333 DEBUGFDEVICE(device, INDI::Logger::DBG_ERROR, "%s: Failed to allocate %d bytes of memory!", __PRETTY_FUNCTION__, *memsize);
334 return -1;
335 }
336
337 uint8_t *destmem = *memptr;
338
339 *naxis = cinfo.num_components;
340 *w = cinfo.output_width;
341 *h = cinfo.output_height;
342
343 /* now actually read the jpeg into the raw buffer */
344 row_pointer[0] = (unsigned char *)malloc(cinfo.output_width * cinfo.num_components);
345
346 /* read one scan line at a time */
347 for (unsigned int row = 0; row < cinfo.image_height; row++)
348 {
349 unsigned char *ppm8 = row_pointer[0];
350 jpeg_read_scanlines(&cinfo, row_pointer, 1);
351 memcpy(destmem, ppm8, cinfo.output_width * cinfo.num_components);
352 destmem += cinfo.output_width * cinfo.num_components;
353 }
354
355 /* wrap up decompression, destroy objects, free pointers and close open files */
356 jpeg_finish_decompress(&cinfo);
357 jpeg_destroy_decompress(&cinfo);
358
359 if (row_pointer[0])
360 free(row_pointer[0]);
361
362 return 0;
363 }
364
365 int read_jpeg_size(unsigned char *inBuffer, unsigned long inSize, int *w, int *h)
366 {
367 /* these are standard libjpeg structures for reading(decompression) */
368 struct jpeg_decompress_struct cinfo;
369 struct jpeg_error_mgr jerr;
370
371 /* here we set up the standard libjpeg error handler */
372 cinfo.err = jpeg_std_error(&jerr);
373 /* setup decompression process and source, then read JPEG header */
374 jpeg_create_decompress(&cinfo);
375 /* this makes the library read from infile */
376 jpeg_mem_src(&cinfo, inBuffer, inSize);
377
378 /* reading the image header which contains image information */
379 jpeg_read_header(&cinfo, (boolean)TRUE);
380
381 *w = cinfo.image_width;
382 *h = cinfo.image_height;
383
384 /* wrap up decompression, destroy objects, free pointers and close open files */
385 jpeg_destroy_decompress(&cinfo);
386
387 return 0;
388 }