1 /* -*- C++ -*-
2 * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3 *
4 LibRaw is free software; you can redistribute it and/or modify
5 it under the terms of the one of two licenses as you choose:
6
7 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
8 (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
9
10 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
11 (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
12
13 */
14
15 #include "../../internal/libraw_cxx_defs.h"
16
sony_arq_load_raw()17 void LibRaw::sony_arq_load_raw()
18 {
19 int row, col;
20 read_shorts(imgdata.rawdata.raw_image,
21 imgdata.sizes.raw_width * imgdata.sizes.raw_height * 4);
22 libraw_internal_data.internal_data.input->seek(
23 -2, SEEK_CUR); // avoid wrong eof error
24
25 if(imgdata.rawparams.options & LIBRAW_RAWOPTIONS_ARQ_SKIP_CHANNEL_SWAP)
26 return;
27
28 for (row = 0; row < imgdata.sizes.raw_height; row++)
29 {
30 unsigned short(*rowp)[4] =
31 (unsigned short(*)[4]) &
32 imgdata.rawdata.raw_image[row * imgdata.sizes.raw_width * 4];
33 for (col = 0; col < imgdata.sizes.raw_width; col++)
34 {
35 unsigned short g2 = rowp[col][2];
36 rowp[col][2] = rowp[col][3];
37 rowp[col][3] = g2;
38 if (((unsigned)(row - imgdata.sizes.top_margin) < imgdata.sizes.height) &&
39 ((unsigned)(col - imgdata.sizes.left_margin) < imgdata.sizes.width) &&
40 (MAX(MAX(rowp[col][0], rowp[col][1]),
41 MAX(rowp[col][2], rowp[col][3])) > imgdata.color.maximum))
42 derror();
43 }
44 }
45 }
46
pentax_4shot_load_raw()47 void LibRaw::pentax_4shot_load_raw()
48 {
49 ushort *plane = (ushort *)malloc(imgdata.sizes.raw_width *
50 imgdata.sizes.raw_height * sizeof(ushort));
51 int alloc_sz = imgdata.sizes.raw_width * (imgdata.sizes.raw_height + 16) * 4 *
52 sizeof(ushort);
53 ushort(*result)[4] = (ushort(*)[4])malloc(alloc_sz);
54 struct movement_t
55 {
56 int row, col;
57 } _move[4] = {
58 {1, 1},
59 {0, 1},
60 {0, 0},
61 {1, 0},
62 };
63
64 int tidx = 0;
65 for (int i = 0; i < 4; i++)
66 {
67 int move_row, move_col;
68 if (imgdata.rawparams.p4shot_order[i] >= '0' &&
69 imgdata.rawparams.p4shot_order[i] <= '3')
70 {
71 move_row = ((imgdata.rawparams.p4shot_order[i] - '0') & 2) ? 1 : 0;
72 move_col = ((imgdata.rawparams.p4shot_order[i] - '0') & 1) ? 1 : 0;
73 }
74 else
75 {
76 move_row = _move[i].row;
77 move_col = _move[i].col;
78 }
79 for (; tidx < 16; tidx++)
80 if (tiff_ifd[tidx].t_width == imgdata.sizes.raw_width &&
81 tiff_ifd[tidx].t_height == imgdata.sizes.raw_height &&
82 tiff_ifd[tidx].bps > 8 && tiff_ifd[tidx].samples == 1)
83 break;
84 if (tidx >= 16)
85 break;
86 imgdata.rawdata.raw_image = plane;
87 ID.input->seek(tiff_ifd[tidx].offset, SEEK_SET);
88 imgdata.idata.filters = 0xb4b4b4b4;
89 libraw_internal_data.unpacker_data.data_offset = tiff_ifd[tidx].offset;
90 (this->*pentax_component_load_raw)();
91 for (int row = 0; row < imgdata.sizes.raw_height - move_row; row++)
92 {
93 int colors[2];
94 for (int c = 0; c < 2; c++)
95 colors[c] = COLOR(row, c);
96 ushort *srcrow = &plane[imgdata.sizes.raw_width * row];
97 ushort(*dstrow)[4] =
98 &result[(imgdata.sizes.raw_width) * (row + move_row) + move_col];
99 for (int col = 0; col < imgdata.sizes.raw_width - move_col; col++)
100 dstrow[col][colors[col % 2]] = srcrow[col];
101 }
102 tidx++;
103 }
104
105 if (imgdata.color.cblack[4] == 2 && imgdata.color.cblack[5] == 2)
106 for (int c = 0; c < 4; c++)
107 imgdata.color.cblack[FC(c / 2, c % 2)] +=
108 imgdata.color.cblack[6 +
109 c / 2 % imgdata.color.cblack[4] *
110 imgdata.color.cblack[5] +
111 c % 2 % imgdata.color.cblack[5]];
112 imgdata.color.cblack[4] = imgdata.color.cblack[5] = 0;
113
114 // assign things back:
115 imgdata.sizes.raw_pitch = imgdata.sizes.raw_width * 8;
116 imgdata.idata.filters = 0;
117 imgdata.rawdata.raw_alloc = imgdata.rawdata.color4_image = result;
118 free(plane);
119 imgdata.rawdata.raw_image = 0;
120 }
121
hasselblad_full_load_raw()122 void LibRaw::hasselblad_full_load_raw()
123 {
124 int row, col;
125
126 for (row = 0; row < S.height; row++)
127 for (col = 0; col < S.width; col++)
128 {
129 read_shorts(&imgdata.image[row * S.width + col][2], 1); // B
130 read_shorts(&imgdata.image[row * S.width + col][1], 1); // G
131 read_shorts(&imgdata.image[row * S.width + col][0], 1); // R
132 }
133 }
134
unpack7bytesto4x16(unsigned char * src,unsigned short * dest)135 static inline void unpack7bytesto4x16(unsigned char *src, unsigned short *dest)
136 {
137 dest[0] = (src[0] << 6) | (src[1] >> 2);
138 dest[1] = ((src[1] & 0x3) << 12) | (src[2] << 4) | (src[3] >> 4);
139 dest[2] = (src[3] & 0xf) << 10 | (src[4] << 2) | (src[5] >> 6);
140 dest[3] = ((src[5] & 0x3f) << 8) | src[6];
141 }
142
unpack28bytesto16x16ns(unsigned char * src,unsigned short * dest)143 static inline void unpack28bytesto16x16ns(unsigned char *src,
144 unsigned short *dest)
145 {
146 dest[0] = (src[3] << 6) | (src[2] >> 2);
147 dest[1] = ((src[2] & 0x3) << 12) | (src[1] << 4) | (src[0] >> 4);
148 dest[2] = (src[0] & 0xf) << 10 | (src[7] << 2) | (src[6] >> 6);
149 dest[3] = ((src[6] & 0x3f) << 8) | src[5];
150 dest[4] = (src[4] << 6) | (src[11] >> 2);
151 dest[5] = ((src[11] & 0x3) << 12) | (src[10] << 4) | (src[9] >> 4);
152 dest[6] = (src[9] & 0xf) << 10 | (src[8] << 2) | (src[15] >> 6);
153 dest[7] = ((src[15] & 0x3f) << 8) | src[14];
154 dest[8] = (src[13] << 6) | (src[12] >> 2);
155 dest[9] = ((src[12] & 0x3) << 12) | (src[19] << 4) | (src[18] >> 4);
156 dest[10] = (src[18] & 0xf) << 10 | (src[17] << 2) | (src[16] >> 6);
157 dest[11] = ((src[16] & 0x3f) << 8) | src[23];
158 dest[12] = (src[22] << 6) | (src[21] >> 2);
159 dest[13] = ((src[21] & 0x3) << 12) | (src[20] << 4) | (src[27] >> 4);
160 dest[14] = (src[27] & 0xf) << 10 | (src[26] << 2) | (src[25] >> 6);
161 dest[15] = ((src[25] & 0x3f) << 8) | src[24];
162 }
163
164 #define swab32(x) \
165 ((unsigned int)((((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
166 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
167 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
168 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))
169
swab32arr(unsigned * arr,unsigned len)170 static inline void swab32arr(unsigned *arr, unsigned len)
171 {
172 for (unsigned i = 0; i < len; i++)
173 arr[i] = swab32(arr[i]);
174 }
175 #undef swab32
176
unpack7bytesto4x16_nikon(unsigned char * src,unsigned short * dest)177 static inline void unpack7bytesto4x16_nikon(unsigned char *src,
178 unsigned short *dest)
179 {
180 dest[3] = (src[6] << 6) | (src[5] >> 2);
181 dest[2] = ((src[5] & 0x3) << 12) | (src[4] << 4) | (src[3] >> 4);
182 dest[1] = (src[3] & 0xf) << 10 | (src[2] << 2) | (src[1] >> 6);
183 dest[0] = ((src[1] & 0x3f) << 8) | src[0];
184 }
185
nikon_14bit_load_raw()186 void LibRaw::nikon_14bit_load_raw()
187 {
188 const unsigned linelen =
189 (unsigned)(ceilf((float)(S.raw_width * 7 / 4) / 16.0)) *
190 16; // 14512; // S.raw_width * 7 / 4;
191 const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width;
192 unsigned char *buf = (unsigned char *)malloc(linelen);
193 merror(buf, "nikon_14bit_load_raw()");
194 for (int row = 0; row < S.raw_height; row++)
195 {
196 unsigned bytesread =
197 libraw_internal_data.internal_data.input->read(buf, 1, linelen);
198 unsigned short *dest = &imgdata.rawdata.raw_image[pitch * row];
199 // swab32arr((unsigned *)buf, bytesread / 4);
200 for (unsigned int sp = 0, dp = 0;
201 dp < pitch - 3 && sp < linelen - 6 && sp < bytesread - 6;
202 sp += 7, dp += 4)
203 unpack7bytesto4x16_nikon(buf + sp, dest + dp);
204 }
205 free(buf);
206 }
207
fuji_14bit_load_raw()208 void LibRaw::fuji_14bit_load_raw()
209 {
210 const unsigned linelen = S.raw_width * 7 / 4;
211 const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width;
212 unsigned char *buf = (unsigned char *)malloc(linelen);
213 merror(buf, "fuji_14bit_load_raw()");
214
215 for (int row = 0; row < S.raw_height; row++)
216 {
217 unsigned bytesread =
218 libraw_internal_data.internal_data.input->read(buf, 1, linelen);
219 unsigned short *dest = &imgdata.rawdata.raw_image[pitch * row];
220 if (bytesread % 28)
221 {
222 swab32arr((unsigned *)buf, bytesread / 4);
223 for (unsigned int sp = 0, dp = 0;
224 dp < pitch - 3 && sp < linelen - 6 && sp < bytesread - 6;
225 sp += 7, dp += 4)
226 unpack7bytesto4x16(buf + sp, dest + dp);
227 }
228 else
229 for (unsigned int sp = 0, dp = 0;
230 dp < pitch - 15 && sp < linelen - 27 && sp < bytesread - 27;
231 sp += 28, dp += 16)
232 unpack28bytesto16x16ns(buf + sp, dest + dp);
233 }
234 free(buf);
235 }
nikon_load_padded_packed_raw()236 void LibRaw::nikon_load_padded_packed_raw() // 12 bit per pixel, padded to 16
237 // bytes
238 {
239 // libraw_internal_data.unpacker_data.load_flags -> row byte count
240 if (libraw_internal_data.unpacker_data.load_flags < 2000 ||
241 libraw_internal_data.unpacker_data.load_flags > 64000)
242 return;
243 unsigned char *buf =
244 (unsigned char *)malloc(libraw_internal_data.unpacker_data.load_flags);
245 for (int row = 0; row < S.raw_height; row++)
246 {
247 checkCancel();
248 libraw_internal_data.internal_data.input->read(
249 buf, libraw_internal_data.unpacker_data.load_flags, 1);
250 for (int icol = 0; icol < S.raw_width / 2; icol++)
251 {
252 imgdata.rawdata.raw_image[(row)*S.raw_width + (icol * 2)] =
253 ((buf[icol * 3 + 1] & 0xf) << 8) | buf[icol * 3];
254 imgdata.rawdata.raw_image[(row)*S.raw_width + (icol * 2 + 1)] =
255 buf[icol * 3 + 2] << 4 | ((buf[icol * 3 + 1] & 0xf0) >> 4);
256 }
257 }
258 free(buf);
259 }
260
nikon_load_striped_packed_raw()261 void LibRaw::nikon_load_striped_packed_raw()
262 {
263 int vbits = 0, bwide, rbits, bite, row, col, i;
264
265 UINT64 bitbuf = 0;
266 unsigned load_flags = 24; // libraw_internal_data.unpacker_data.load_flags;
267 unsigned tiff_bps = libraw_internal_data.unpacker_data.tiff_bps;
268
269 struct tiff_ifd_t *ifd = &tiff_ifd[0];
270 while (ifd < &tiff_ifd[libraw_internal_data.identify_data.tiff_nifds] &&
271 ifd->offset != libraw_internal_data.unpacker_data.data_offset)
272 ++ifd;
273 if (ifd == &tiff_ifd[libraw_internal_data.identify_data.tiff_nifds])
274 throw LIBRAW_EXCEPTION_DECODE_RAW;
275
276 if (!ifd->rows_per_strip || !ifd->strip_offsets_count)
277 return; // not unpacked
278 int stripcnt = 0;
279
280 bwide = S.raw_width * tiff_bps / 8;
281 bwide += bwide & load_flags >> 7;
282 rbits = bwide * 8 - S.raw_width * tiff_bps;
283 if (load_flags & 1)
284 bwide = bwide * 16 / 15;
285 bite = 8 + (load_flags & 24);
286 for (row = 0; row < S.raw_height; row++)
287 {
288 checkCancel();
289 if (!(row % ifd->rows_per_strip))
290 {
291 if (stripcnt >= ifd->strip_offsets_count)
292 return; // run out of data
293 libraw_internal_data.internal_data.input->seek(
294 ifd->strip_offsets[stripcnt], SEEK_SET);
295 stripcnt++;
296 }
297 for (col = 0; col < S.raw_width; col++)
298 {
299 for (vbits -= tiff_bps; vbits < 0; vbits += bite)
300 {
301 bitbuf <<= bite;
302 for (i = 0; i < bite; i += 8)
303 bitbuf |=
304 (unsigned)(libraw_internal_data.internal_data.input->get_char()
305 << i);
306 }
307 imgdata.rawdata.raw_image[(row)*S.raw_width + (col)] =
308 bitbuf << (64 - tiff_bps - vbits) >> (64 - tiff_bps);
309 }
310 vbits -= rbits;
311 }
312 }
313
314 struct pana_cs6_page_decoder
315 {
316 unsigned int pixelbuffer[18], lastoffset, maxoffset;
317 unsigned char current, *buffer;
pana_cs6_page_decoderpana_cs6_page_decoder318 pana_cs6_page_decoder(unsigned char *_buffer, unsigned int bsize)
319 : lastoffset(0), maxoffset(bsize), current(0), buffer(_buffer)
320 {
321 }
322 void read_page(); // will throw IO error if not enough space in buffer
323 void read_page12(); // 12-bit variant
nextpixelpana_cs6_page_decoder324 unsigned int nextpixel() { return current < 14 ? pixelbuffer[current++] : 0; }
nextpixel12pana_cs6_page_decoder325 unsigned int nextpixel12() { return current < 18 ? pixelbuffer[current++] : 0; }
326 };
327
read_page()328 void pana_cs6_page_decoder::read_page()
329 {
330 if (!buffer || (maxoffset - lastoffset < 16))
331 throw LIBRAW_EXCEPTION_IO_EOF;
332 #define wbuffer(i) ((unsigned short)buffer[lastoffset + 15 - i])
333 pixelbuffer[0] = (wbuffer(0) << 6) | (wbuffer(1) >> 2); // 14 bit
334 pixelbuffer[1] = (((wbuffer(1) & 0x3) << 12) | (wbuffer(2) << 4) | (wbuffer(3) >> 4)) & 0x3fff; // 14 bit
335 pixelbuffer[2] = (wbuffer(3) >> 2) & 0x3; // 2
336 pixelbuffer[3] = ((wbuffer(3) & 0x3) << 8) | wbuffer(4); // 10
337 pixelbuffer[4] = (wbuffer(5) << 2) | (wbuffer(6) >> 6); // 10
338 pixelbuffer[5] = ((wbuffer(6) & 0x3f) << 4) | (wbuffer(7) >> 4); // 10
339 pixelbuffer[6] = (wbuffer(7) >> 2) & 0x3;
340 pixelbuffer[7] = ((wbuffer(7) & 0x3) << 8) | wbuffer(8);
341 pixelbuffer[8] = ((wbuffer(9) << 2) & 0x3fc) | (wbuffer(10) >> 6);
342 pixelbuffer[9] = ((wbuffer(10) << 4) | (wbuffer(11) >> 4)) & 0x3ff;
343 pixelbuffer[10] = (wbuffer(11) >> 2) & 0x3;
344 pixelbuffer[11] = ((wbuffer(11) & 0x3) << 8) | wbuffer(12);
345 pixelbuffer[12] = (((wbuffer(13) << 2) & 0x3fc) | wbuffer(14) >> 6) & 0x3ff;
346 pixelbuffer[13] = ((wbuffer(14) << 4) | (wbuffer(15) >> 4)) & 0x3ff;
347 #undef wbuffer
348 current = 0;
349 lastoffset += 16;
350 }
351
read_page12()352 void pana_cs6_page_decoder::read_page12()
353 {
354 if (!buffer || (maxoffset - lastoffset < 16))
355 throw LIBRAW_EXCEPTION_IO_EOF;
356 #define wb(i) ((unsigned short)buffer[lastoffset + 15 - i])
357 pixelbuffer[0] = (wb(0) << 4) | (wb(1) >> 4); // 12 bit: 8/0 + 4 upper bits of /1
358 pixelbuffer[1] = (((wb(1) & 0xf) << 8) | (wb(2))) & 0xfff; // 12 bit: 4l/1 + 8/2
359
360 pixelbuffer[2] = (wb(3) >> 6) & 0x3; // 2; 2u/3, 6 low bits remains in wb(3)
361 pixelbuffer[3] = ((wb(3) & 0x3f) << 2) | (wb(4) >> 6); // 8; 6l/3 + 2u/4; 6 low bits remains in wb(4)
362 pixelbuffer[4] = ((wb(4) & 0x3f) << 2) | (wb(5) >> 6); // 8: 6l/4 + 2u/5; 6 low bits remains in wb(5)
363 pixelbuffer[5] = ((wb(5) & 0x3f) << 2) | (wb(6) >> 6); // 8: 6l/5 + 2u/6, 6 low bits remains in wb(6)
364
365 pixelbuffer[6] = (wb(6) >> 4) & 0x3; // 2, 4 low bits remains in wb(6)
366 pixelbuffer[7] = ((wb(6) & 0xf) << 4) | (wb(7) >> 4); // 8: 4 low bits from wb(6), 4 upper bits from wb(7)
367 pixelbuffer[8] = ((wb(7) & 0xf) << 4) | (wb(8) >> 4); // 8: 4 low bits from wb7, 4 upper bits from wb8
368 pixelbuffer[9] = ((wb(8) & 0xf) << 4) | (wb(9) >> 4); // 8: 4 low bits from wb8, 4 upper bits from wb9
369
370 pixelbuffer[10] = (wb(9) >> 2) & 0x3; // 2: bits 2-3 from wb9, two low bits remain in wb9
371 pixelbuffer[11] = ((wb(9) & 0x3) << 6) | (wb(10) >> 2); // 8: 2 bits from wb9, 6 bits from wb10
372 pixelbuffer[12] = ((wb(10) & 0x3) << 6) | (wb(11) >> 2); // 8: 2 bits from wb10, 6 bits from wb11
373 pixelbuffer[13] = ((wb(11) & 0x3) << 6) | (wb(12) >> 2); // 8: 2 bits from wb11, 6 bits from wb12
374
375 pixelbuffer[14] = wb(12) & 0x3; // 2: low bits from wb12
376 pixelbuffer[15] = wb(13);
377 pixelbuffer[16] = wb(14);
378 pixelbuffer[17] = wb(15);
379 #undef wb
380 current = 0;
381 lastoffset += 16;
382 }
383
384
385
panasonicC6_load_raw()386 void LibRaw::panasonicC6_load_raw()
387 {
388 const int rowstep = 16;
389 const bool _12bit = libraw_internal_data.unpacker_data.pana_bpp == 12;
390 const int pixperblock = _12bit ? 14 : 11;
391 const int blocksperrow = imgdata.sizes.raw_width / pixperblock;
392 const int rowbytes = blocksperrow * 16;
393 const unsigned pixelbase0 = _12bit ? 0x80 : 0x200;
394 const unsigned pixelbase_compare = _12bit ? 0x800 : 0x2000;
395 const unsigned spix_compare = _12bit ? 0x3fff : 0xffff;
396 const unsigned pixel_mask = _12bit ? 0xfff : 0x3fff;
397 std::vector<unsigned char> iobuf;
398 try
399 {
400 iobuf.resize(rowbytes * rowstep);
401 }
402 catch (...)
403 {
404 merror(NULL, "panasonicC6_load_raw()");
405 throw LIBRAW_EXCEPTION_ALLOC;
406 }
407
408 for (int row = 0; row < imgdata.sizes.raw_height - rowstep + 1;
409 row += rowstep)
410 {
411 int rowstoread = MIN(rowstep, imgdata.sizes.raw_height - row);
412 if (libraw_internal_data.internal_data.input->read(
413 iobuf.data(), rowbytes, rowstoread) != rowstoread)
414 throw LIBRAW_EXCEPTION_IO_EOF;
415 pana_cs6_page_decoder page(iobuf.data(), rowbytes * rowstoread);
416 for (int crow = 0, col = 0; crow < rowstoread; crow++, col = 0)
417 {
418 unsigned short *rowptr =
419 &imgdata.rawdata
420 .raw_image[(row + crow) * imgdata.sizes.raw_pitch / 2];
421 for (int rblock = 0; rblock < blocksperrow; rblock++)
422 {
423 if (_12bit)
424 page.read_page12();
425 else
426 page.read_page();
427 unsigned oddeven[2] = {0, 0}, nonzero[2] = {0, 0};
428 unsigned pmul = 0, pixel_base = 0;
429 for (int pix = 0; pix < pixperblock; pix++)
430 {
431 if (pix % 3 == 2)
432 {
433 unsigned base = _12bit ? page.nextpixel12(): page.nextpixel();
434 if (base > 3)
435 throw LIBRAW_EXCEPTION_IO_CORRUPT; // not possible b/c of 2-bit
436 // field, but....
437 if (base == 3)
438 base = 4;
439 pixel_base = pixelbase0 << base;
440 pmul = 1 << base;
441 }
442 unsigned epixel = _12bit ? page.nextpixel12() : page.nextpixel();
443 if (oddeven[pix % 2])
444 {
445 epixel *= pmul;
446 if (pixel_base < pixelbase_compare && nonzero[pix % 2] > pixel_base)
447 epixel += nonzero[pix % 2] - pixel_base;
448 nonzero[pix % 2] = epixel;
449 }
450 else
451 {
452 oddeven[pix % 2] = epixel;
453 if (epixel)
454 nonzero[pix % 2] = epixel;
455 else
456 epixel = nonzero[pix % 2];
457 }
458 unsigned spix = epixel - 0xf;
459 if (spix <= spix_compare)
460 rowptr[col++] = spix & spix_compare;
461 else
462 {
463 epixel = (((signed int)(epixel + 0x7ffffff1)) >> 0x1f);
464 rowptr[col++] = epixel & pixel_mask;
465 }
466 }
467 }
468 }
469 }
470 }
471
472
panasonicC7_load_raw()473 void LibRaw::panasonicC7_load_raw()
474 {
475 const int rowstep = 16;
476 int pixperblock = libraw_internal_data.unpacker_data.pana_bpp == 14 ? 9 : 10;
477 int rowbytes = imgdata.sizes.raw_width / pixperblock * 16;
478 unsigned char *iobuf = (unsigned char *)malloc(rowbytes * rowstep);
479 merror(iobuf, "panasonicC7_load_raw()");
480 for (int row = 0; row < imgdata.sizes.raw_height - rowstep + 1;
481 row += rowstep)
482 {
483 int rowstoread = MIN(rowstep, imgdata.sizes.raw_height - row);
484 if (libraw_internal_data.internal_data.input->read(
485 iobuf, rowbytes, rowstoread) != rowstoread)
486 throw LIBRAW_EXCEPTION_IO_EOF;
487 unsigned char *bytes = iobuf;
488 for (int crow = 0; crow < rowstoread; crow++)
489 {
490 unsigned short *rowptr =
491 &imgdata.rawdata
492 .raw_image[(row + crow) * imgdata.sizes.raw_pitch / 2];
493 for (int col = 0; col < imgdata.sizes.raw_width - pixperblock + 1;
494 col += pixperblock, bytes += 16)
495 {
496 if (libraw_internal_data.unpacker_data.pana_bpp == 14)
497 {
498 rowptr[col] = bytes[0] + ((bytes[1] & 0x3F) << 8);
499 rowptr[col + 1] =
500 (bytes[1] >> 6) + 4 * (bytes[2]) + ((bytes[3] & 0xF) << 10);
501 rowptr[col + 2] =
502 (bytes[3] >> 4) + 16 * (bytes[4]) + ((bytes[5] & 3) << 12);
503 rowptr[col + 3] = ((bytes[5] & 0xFC) >> 2) + (bytes[6] << 6);
504 rowptr[col + 4] = bytes[7] + ((bytes[8] & 0x3F) << 8);
505 rowptr[col + 5] =
506 (bytes[8] >> 6) + 4 * bytes[9] + ((bytes[10] & 0xF) << 10);
507 rowptr[col + 6] =
508 (bytes[10] >> 4) + 16 * bytes[11] + ((bytes[12] & 3) << 12);
509 rowptr[col + 7] = ((bytes[12] & 0xFC) >> 2) + (bytes[13] << 6);
510 rowptr[col + 8] = bytes[14] + ((bytes[15] & 0x3F) << 8);
511 }
512 else if (libraw_internal_data.unpacker_data.pana_bpp ==
513 12) // have not seen in the wild yet
514 {
515 rowptr[col] = ((bytes[1] & 0xF) << 8) + bytes[0];
516 rowptr[col + 1] = 16 * bytes[2] + (bytes[1] >> 4);
517 rowptr[col + 2] = ((bytes[4] & 0xF) << 8) + bytes[3];
518 rowptr[col + 3] = 16 * bytes[5] + (bytes[4] >> 4);
519 rowptr[col + 4] = ((bytes[7] & 0xF) << 8) + bytes[6];
520 rowptr[col + 5] = 16 * bytes[8] + (bytes[7] >> 4);
521 rowptr[col + 6] = ((bytes[10] & 0xF) << 8) + bytes[9];
522 rowptr[col + 7] = 16 * bytes[11] + (bytes[10] >> 4);
523 rowptr[col + 8] = ((bytes[13] & 0xF) << 8) + bytes[12];
524 rowptr[col + 9] = 16 * bytes[14] + (bytes[13] >> 4);
525 }
526 }
527 }
528 }
529 free(iobuf);
530 }
531
unpacked_load_raw_fuji_f700s20()532 void LibRaw::unpacked_load_raw_fuji_f700s20()
533 {
534 int base_offset = 0;
535 int row_size = imgdata.sizes.raw_width * 2; // in bytes
536 if (imgdata.idata.raw_count == 2 && imgdata.rawparams.shot_select)
537 {
538 libraw_internal_data.internal_data.input->seek(-row_size, SEEK_CUR);
539 base_offset = row_size; // in bytes
540 }
541 unsigned char *buffer = (unsigned char *)malloc(row_size * 2);
542 for (int row = 0; row < imgdata.sizes.raw_height; row++)
543 {
544 read_shorts((ushort *)buffer, imgdata.sizes.raw_width * 2);
545 memmove(&imgdata.rawdata.raw_image[row * imgdata.sizes.raw_pitch / 2],
546 buffer + base_offset, row_size);
547 }
548 free(buffer);
549 }
550
nikon_load_sraw()551 void LibRaw::nikon_load_sraw()
552 {
553 // We're already seeked to data!
554 unsigned char *rd =
555 (unsigned char *)malloc(3 * (imgdata.sizes.raw_width + 2));
556 if (!rd)
557 throw LIBRAW_EXCEPTION_ALLOC;
558 try
559 {
560 int row, col;
561 for (row = 0; row < imgdata.sizes.raw_height; row++)
562 {
563 checkCancel();
564 libraw_internal_data.internal_data.input->read(rd, 3,
565 imgdata.sizes.raw_width);
566 for (col = 0; col < imgdata.sizes.raw_width - 1; col += 2)
567 {
568 int bi = col * 3;
569 ushort bits1 = (rd[bi + 1] & 0xf) << 8 | rd[bi]; // 3,0,1
570 ushort bits2 = rd[bi + 2] << 4 | ((rd[bi + 1] >> 4) & 0xf); // 452
571 ushort bits3 = ((rd[bi + 4] & 0xf) << 8) | rd[bi + 3]; // 967
572 ushort bits4 = rd[bi + 5] << 4 | ((rd[bi + 4] >> 4) & 0xf); // ab8
573 imgdata.image[row * imgdata.sizes.raw_width + col][0] = bits1;
574 imgdata.image[row * imgdata.sizes.raw_width + col][1] = bits3;
575 imgdata.image[row * imgdata.sizes.raw_width + col][2] = bits4;
576 imgdata.image[row * imgdata.sizes.raw_width + col + 1][0] = bits2;
577 imgdata.image[row * imgdata.sizes.raw_width + col + 1][1] = 2048;
578 imgdata.image[row * imgdata.sizes.raw_width + col + 1][2] = 2048;
579 }
580 }
581 }
582 catch (...)
583 {
584 free(rd);
585 throw;
586 }
587 free(rd);
588 C.maximum = 0xfff; // 12 bit?
589 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE)
590 {
591 return; // no CbCr interpolation
592 }
593 // Interpolate CC channels
594 int row, col;
595 for (row = 0; row < imgdata.sizes.raw_height; row++)
596 {
597 checkCancel(); // will throw out
598 for (col = 0; col < imgdata.sizes.raw_width; col += 2)
599 {
600 int col2 = col < imgdata.sizes.raw_width - 2 ? col + 2 : col;
601 imgdata.image[row * imgdata.sizes.raw_width + col + 1][1] =
602 (unsigned short)(int(imgdata.image[row * imgdata.sizes.raw_width +
603 col][1] +
604 imgdata.image[row * imgdata.sizes.raw_width +
605 col2][1]) /
606 2);
607 imgdata.image[row * imgdata.sizes.raw_width + col + 1][2] =
608 (unsigned short)(int(imgdata.image[row * imgdata.sizes.raw_width +
609 col][2] +
610 imgdata.image[row * imgdata.sizes.raw_width +
611 col2][2]) /
612 2);
613 }
614 }
615 if (imgdata.rawparams.specials & LIBRAW_RAWSPECIAL_SRAW_NO_RGB)
616 return;
617
618 for (row = 0; row < imgdata.sizes.raw_height; row++)
619 {
620 checkCancel(); // will throw out
621 for (col = 0; col < imgdata.sizes.raw_width; col++)
622 {
623 float Y =
624 float(imgdata.image[row * imgdata.sizes.raw_width + col][0]) / 2549.f;
625 float Ch2 =
626 float(imgdata.image[row * imgdata.sizes.raw_width + col][1] - 1280) /
627 1536.f;
628 float Ch3 =
629 float(imgdata.image[row * imgdata.sizes.raw_width + col][2] - 1280) /
630 1536.f;
631 if (Y > 1.f)
632 Y = 1.f;
633 if (Y > 0.803f)
634 Ch2 = Ch3 = 0.5f;
635 float r = Y + 1.40200f * (Ch3 - 0.5f);
636 if (r < 0.f)
637 r = 0.f;
638 if (r > 1.f)
639 r = 1.f;
640 float g = Y - 0.34414f * (Ch2 - 0.5f) - 0.71414 * (Ch3 - 0.5f);
641 if (g > 1.f)
642 g = 1.f;
643 if (g < 0.f)
644 g = 0.f;
645 float b = Y + 1.77200 * (Ch2 - 0.5f);
646 if (b > 1.f)
647 b = 1.f;
648 if (b < 0.f)
649 b = 0.f;
650 imgdata.image[row * imgdata.sizes.raw_width + col][0] =
651 imgdata.color.curve[int(r * 3072.f)];
652 imgdata.image[row * imgdata.sizes.raw_width + col][1] =
653 imgdata.color.curve[int(g * 3072.f)];
654 imgdata.image[row * imgdata.sizes.raw_width + col][2] =
655 imgdata.color.curve[int(b * 3072.f)];
656 }
657 }
658 C.maximum = 16383;
659 }
660