1 /* -*- C++ -*-
2 * File: libraw_fuji_compressed.cpp
3 * Copyright (C) 2016 Alexey Danilchenko
4 *
5 * Adopted to LibRaw by Alex Tutubalin, lexa@lexa.ru
6 * LibRaw Fujifilm/compressed decoder
7
8 LibRaw is free software; you can redistribute it and/or modify
9 it under the terms of the one of two licenses as you choose:
10
11 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12 (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13
14 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15 (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16
17 */
18
19 // This file is #included in libraw_cxx
20
21 #ifdef _abs
22 #undef _abs
23 #undef _min
24 #undef _max
25 #endif
26 #define _abs(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
27 #define _min(a, b) ((a) < (b) ? (a) : (b))
28 #define _max(a, b) ((a) > (b) ? (a) : (b))
29
30 struct int_pair
31 {
32 int value1;
33 int value2;
34 };
35
36 enum _xt_lines
37 {
38 _R0 = 0,
39 _R1,
40 _R2,
41 _R3,
42 _R4,
43 _G0,
44 _G1,
45 _G2,
46 _G3,
47 _G4,
48 _G5,
49 _G6,
50 _G7,
51 _B0,
52 _B1,
53 _B2,
54 _B3,
55 _B4,
56 _ltotal
57 };
58
59 struct fuji_compressed_block
60 {
61 int cur_bit; // current bit being read (from left to right)
62 int cur_pos; // current position in a buffer
63 INT64 cur_buf_offset; // offset of this buffer in a file
64 unsigned max_read_size; // Amount of data to be read
65 int cur_buf_size; // buffer size
66 uchar *cur_buf; // currently read block
67 int fillbytes; // Counter to add extra byte for block size N*16
68 LibRaw_abstract_datastream *input;
69 struct int_pair grad_even[3][41]; // tables of gradients
70 struct int_pair grad_odd[3][41];
71 ushort *linealloc;
72 ushort *linebuf[_ltotal];
73 };
74
sgetn(int n,uchar * s)75 static unsigned sgetn(int n, uchar *s)
76 {
77 unsigned result = 0;
78 while (n-- > 0)
79 result = (result << 8) | (*s++);
80 return result;
81 }
82
init_fuji_compr(struct fuji_compressed_params * info)83 void LibRaw::init_fuji_compr(struct fuji_compressed_params *info)
84 {
85 int cur_val, i;
86 int8_t *qt;
87
88 if ((libraw_internal_data.unpacker_data.fuji_block_width % 3 &&
89 libraw_internal_data.unpacker_data.fuji_raw_type == 16) ||
90 (libraw_internal_data.unpacker_data.fuji_block_width & 1 &&
91 libraw_internal_data.unpacker_data.fuji_raw_type == 0))
92 derror();
93
94 info->q_table = (int8_t *)malloc(32768);
95 merror(info->q_table, "init_fuji_compr()");
96
97 if (libraw_internal_data.unpacker_data.fuji_raw_type == 16)
98 info->line_width = (libraw_internal_data.unpacker_data.fuji_block_width * 2) / 3;
99 else
100 info->line_width = libraw_internal_data.unpacker_data.fuji_block_width >> 1;
101
102 info->q_point[0] = 0;
103 info->q_point[1] = 0x12;
104 info->q_point[2] = 0x43;
105 info->q_point[3] = 0x114;
106 info->q_point[4] = (1 << libraw_internal_data.unpacker_data.fuji_bits) - 1;
107 info->min_value = 0x40;
108
109 cur_val = -info->q_point[4];
110 for (qt = info->q_table; cur_val <= info->q_point[4]; ++qt, ++cur_val)
111 {
112 if (cur_val <= -info->q_point[3])
113 *qt = -4;
114 else if (cur_val <= -info->q_point[2])
115 *qt = -3;
116 else if (cur_val <= -info->q_point[1])
117 *qt = -2;
118 else if (cur_val < 0)
119 *qt = -1;
120 else if (cur_val == 0)
121 *qt = 0;
122 else if (cur_val < info->q_point[1])
123 *qt = 1;
124 else if (cur_val < info->q_point[2])
125 *qt = 2;
126 else if (cur_val < info->q_point[3])
127 *qt = 3;
128 else
129 *qt = 4;
130 }
131
132 // populting gradients
133 if (info->q_point[4] == 0x3FFF)
134 {
135 info->total_values = 0x4000;
136 info->raw_bits = 14;
137 info->max_bits = 56;
138 info->maxDiff = 256;
139 }
140 else if (info->q_point[4] == 0xFFF)
141 {
142 info->total_values = 4096;
143 info->raw_bits = 12;
144 info->max_bits = 48;
145 info->maxDiff = 64;
146 }
147 else
148 derror();
149 }
150
151 #define XTRANS_BUF_SIZE 0x10000
152
fuji_fill_buffer(struct fuji_compressed_block * info)153 static inline void fuji_fill_buffer(struct fuji_compressed_block *info)
154 {
155 if (info->cur_pos >= info->cur_buf_size)
156 {
157 info->cur_pos = 0;
158 info->cur_buf_offset += info->cur_buf_size;
159 #ifdef LIBRAW_USE_OPENMP
160 #pragma omp critical
161 #endif
162 {
163 #ifndef LIBRAW_USE_OPENMP
164 info->input->lock();
165 #endif
166 info->input->seek(info->cur_buf_offset, SEEK_SET);
167 info->cur_buf_size = info->input->read(info->cur_buf, 1, _min(info->max_read_size, XTRANS_BUF_SIZE));
168 #ifndef LIBRAW_USE_OPENMP
169 info->input->unlock();
170 #endif
171 if (info->cur_buf_size < 1) // nothing read
172 {
173 if (info->fillbytes > 0)
174 {
175 int ls = _max(1, _min(info->fillbytes, XTRANS_BUF_SIZE));
176 memset(info->cur_buf, 0, ls);
177 info->fillbytes -= ls;
178 }
179 else
180 throw LIBRAW_EXCEPTION_IO_EOF;
181 }
182 info->max_read_size -= info->cur_buf_size;
183 }
184 }
185 }
186
init_fuji_block(struct fuji_compressed_block * info,const struct fuji_compressed_params * params,INT64 raw_offset,unsigned dsize)187 void LibRaw::init_fuji_block(struct fuji_compressed_block *info, const struct fuji_compressed_params *params,
188 INT64 raw_offset, unsigned dsize)
189 {
190 info->linealloc = (ushort *)calloc(sizeof(ushort), _ltotal * (params->line_width + 2));
191 merror(info->linealloc, "init_fuji_block()");
192
193 INT64 fsize = libraw_internal_data.internal_data.input->size();
194 info->max_read_size = _min(unsigned(fsize - raw_offset), dsize); // Data size may be incorrect?
195 info->fillbytes = 1;
196
197 info->input = libraw_internal_data.internal_data.input;
198 info->linebuf[_R0] = info->linealloc;
199 for (int i = _R1; i <= _B4; i++)
200 info->linebuf[i] = info->linebuf[i - 1] + params->line_width + 2;
201
202 // init buffer
203 info->cur_buf = (uchar *)malloc(XTRANS_BUF_SIZE);
204 merror(info->cur_buf, "init_fuji_block()");
205 info->cur_bit = 0;
206 info->cur_pos = 0;
207 info->cur_buf_offset = raw_offset;
208 for (int j = 0; j < 3; j++)
209 for (int i = 0; i < 41; i++)
210 {
211 info->grad_even[j][i].value1 = params->maxDiff;
212 info->grad_even[j][i].value2 = 1;
213 info->grad_odd[j][i].value1 = params->maxDiff;
214 info->grad_odd[j][i].value2 = 1;
215 }
216
217 info->cur_buf_size = 0;
218 fuji_fill_buffer(info);
219 }
220
copy_line_to_xtrans(struct fuji_compressed_block * info,int cur_line,int cur_block,int cur_block_width)221 void LibRaw::copy_line_to_xtrans(struct fuji_compressed_block *info, int cur_line, int cur_block, int cur_block_width)
222 {
223 ushort *lineBufB[3];
224 ushort *lineBufG[6];
225 ushort *lineBufR[3];
226 unsigned pixel_count;
227 ushort *line_buf;
228 int index;
229
230 int offset = libraw_internal_data.unpacker_data.fuji_block_width * cur_block + 6 * imgdata.sizes.raw_width * cur_line;
231 ushort *raw_block_data = imgdata.rawdata.raw_image + offset;
232 int row_count = 0;
233
234 for (int i = 0; i < 3; i++)
235 {
236 lineBufR[i] = info->linebuf[_R2 + i] + 1;
237 lineBufB[i] = info->linebuf[_B2 + i] + 1;
238 }
239 for (int i = 0; i < 6; i++)
240 lineBufG[i] = info->linebuf[_G2 + i] + 1;
241
242 while (row_count < 6)
243 {
244 pixel_count = 0;
245 while (pixel_count < cur_block_width)
246 {
247 switch (imgdata.idata.xtrans_abs[row_count][(pixel_count % 6)])
248 {
249 case 0: // red
250 line_buf = lineBufR[row_count >> 1];
251 break;
252 case 1: // green
253 default: // to make static analyzer happy
254 line_buf = lineBufG[row_count];
255 break;
256 case 2: // blue
257 line_buf = lineBufB[row_count >> 1];
258 break;
259 }
260
261 index = (((pixel_count * 2 / 3) & 0x7FFFFFFE) | ((pixel_count % 3) & 1)) + ((pixel_count % 3) >> 1);
262 raw_block_data[pixel_count] = line_buf[index];
263
264 ++pixel_count;
265 }
266 ++row_count;
267 raw_block_data += imgdata.sizes.raw_width;
268 }
269 }
270
copy_line_to_bayer(struct fuji_compressed_block * info,int cur_line,int cur_block,int cur_block_width)271 void LibRaw::copy_line_to_bayer(struct fuji_compressed_block *info, int cur_line, int cur_block, int cur_block_width)
272 {
273 ushort *lineBufB[3];
274 ushort *lineBufG[6];
275 ushort *lineBufR[3];
276 unsigned pixel_count;
277 ushort *line_buf;
278
279 int fuji_bayer[2][2];
280 for (int r = 0; r < 2; r++)
281 for (int c = 0; c < 2; c++)
282 fuji_bayer[r][c] = FC(r, c); // We'll downgrade G2 to G below
283
284 int offset = libraw_internal_data.unpacker_data.fuji_block_width * cur_block + 6 * imgdata.sizes.raw_width * cur_line;
285 ushort *raw_block_data = imgdata.rawdata.raw_image + offset;
286 int row_count = 0;
287
288 for (int i = 0; i < 3; i++)
289 {
290 lineBufR[i] = info->linebuf[_R2 + i] + 1;
291 lineBufB[i] = info->linebuf[_B2 + i] + 1;
292 }
293 for (int i = 0; i < 6; i++)
294 lineBufG[i] = info->linebuf[_G2 + i] + 1;
295
296 while (row_count < 6)
297 {
298 pixel_count = 0;
299 while (pixel_count < cur_block_width)
300 {
301 switch (fuji_bayer[row_count & 1][pixel_count & 1])
302 {
303 case 0: // red
304 line_buf = lineBufR[row_count >> 1];
305 break;
306 case 1: // green
307 case 3: // second green
308 default: // to make static analyzer happy
309 line_buf = lineBufG[row_count];
310 break;
311 case 2: // blue
312 line_buf = lineBufB[row_count >> 1];
313 break;
314 }
315
316 raw_block_data[pixel_count] = line_buf[pixel_count >> 1];
317 ++pixel_count;
318 }
319 ++row_count;
320 raw_block_data += imgdata.sizes.raw_width;
321 }
322 }
323
324 #define fuji_quant_gradient(i, v1, v2) (9 * i->q_table[i->q_point[4] + (v1)] + i->q_table[i->q_point[4] + (v2)])
325
fuji_zerobits(struct fuji_compressed_block * info,int * count)326 static inline void fuji_zerobits(struct fuji_compressed_block *info, int *count)
327 {
328 uchar zero = 0;
329 *count = 0;
330 while (zero == 0)
331 {
332 zero = (info->cur_buf[info->cur_pos] >> (7 - info->cur_bit)) & 1;
333 info->cur_bit++;
334 info->cur_bit &= 7;
335 if (!info->cur_bit)
336 {
337 ++info->cur_pos;
338 fuji_fill_buffer(info);
339 }
340 if (zero)
341 break;
342 ++*count;
343 }
344 }
345
fuji_read_code(struct fuji_compressed_block * info,int * data,int bits_to_read)346 static inline void fuji_read_code(struct fuji_compressed_block *info, int *data, int bits_to_read)
347 {
348 uchar bits_left = bits_to_read;
349 uchar bits_left_in_byte = 8 - (info->cur_bit & 7);
350 *data = 0;
351 if (!bits_to_read)
352 return;
353 if (bits_to_read >= bits_left_in_byte)
354 {
355 do
356 {
357 *data <<= bits_left_in_byte;
358 bits_left -= bits_left_in_byte;
359 *data |= info->cur_buf[info->cur_pos] & ((1 << bits_left_in_byte) - 1);
360 ++info->cur_pos;
361 fuji_fill_buffer(info);
362 bits_left_in_byte = 8;
363 } while (bits_left >= 8);
364 }
365 if (!bits_left)
366 {
367 info->cur_bit = (8 - (bits_left_in_byte & 7)) & 7;
368 return;
369 }
370 *data <<= bits_left;
371 bits_left_in_byte -= bits_left;
372 *data |= ((1 << bits_left) - 1) & ((unsigned)info->cur_buf[info->cur_pos] >> bits_left_in_byte);
373 info->cur_bit = (8 - (bits_left_in_byte & 7)) & 7;
374 }
375
bitDiff(int value1,int value2)376 static inline int bitDiff(int value1, int value2)
377 {
378 int decBits = 0;
379 if (value2 < value1)
380 while (decBits <= 12 && (value2 << ++decBits) < value1)
381 ;
382 return decBits;
383 }
384
fuji_decode_sample_even(struct fuji_compressed_block * info,const struct fuji_compressed_params * params,ushort * line_buf,int pos,struct int_pair * grads)385 static inline int fuji_decode_sample_even(struct fuji_compressed_block *info,
386 const struct fuji_compressed_params *params, ushort *line_buf, int pos,
387 struct int_pair *grads)
388 {
389 int interp_val = 0;
390 // ushort decBits;
391 int errcnt = 0;
392
393 int sample = 0, code = 0;
394 ushort *line_buf_cur = line_buf + pos;
395 int Rb = line_buf_cur[-2 - params->line_width];
396 int Rc = line_buf_cur[-3 - params->line_width];
397 int Rd = line_buf_cur[-1 - params->line_width];
398 int Rf = line_buf_cur[-4 - 2 * params->line_width];
399
400 int grad, gradient, diffRcRb, diffRfRb, diffRdRb;
401
402 grad = fuji_quant_gradient(params, Rb - Rf, Rc - Rb);
403 gradient = _abs(grad);
404 diffRcRb = _abs(Rc - Rb);
405 diffRfRb = _abs(Rf - Rb);
406 diffRdRb = _abs(Rd - Rb);
407
408 if (diffRcRb > diffRfRb && diffRcRb > diffRdRb)
409 interp_val = Rf + Rd + 2 * Rb;
410 else if (diffRdRb > diffRcRb && diffRdRb > diffRfRb)
411 interp_val = Rf + Rc + 2 * Rb;
412 else
413 interp_val = Rd + Rc + 2 * Rb;
414
415 fuji_zerobits(info, &sample);
416
417 if (sample < params->max_bits - params->raw_bits - 1)
418 {
419 int decBits = bitDiff(grads[gradient].value1, grads[gradient].value2);
420 fuji_read_code(info, &code, decBits);
421 code += sample << decBits;
422 }
423 else
424 {
425 fuji_read_code(info, &code, params->raw_bits);
426 code++;
427 }
428
429 if (code < 0 || code >= params->total_values)
430 errcnt++;
431
432 if (code & 1)
433 code = -1 - code / 2;
434 else
435 code /= 2;
436
437 grads[gradient].value1 += _abs(code);
438 if (grads[gradient].value2 == params->min_value)
439 {
440 grads[gradient].value1 >>= 1;
441 grads[gradient].value2 >>= 1;
442 }
443 grads[gradient].value2++;
444 if (grad < 0)
445 interp_val = (interp_val >> 2) - code;
446 else
447 interp_val = (interp_val >> 2) + code;
448 if (interp_val < 0)
449 interp_val += params->total_values;
450 else if (interp_val > params->q_point[4])
451 interp_val -= params->total_values;
452
453 if (interp_val >= 0)
454 line_buf_cur[0] = _min(interp_val, params->q_point[4]);
455 else
456 line_buf_cur[0] = 0;
457 return errcnt;
458 }
459
fuji_decode_sample_odd(struct fuji_compressed_block * info,const struct fuji_compressed_params * params,ushort * line_buf,int pos,struct int_pair * grads)460 static inline int fuji_decode_sample_odd(struct fuji_compressed_block *info,
461 const struct fuji_compressed_params *params, ushort *line_buf, int pos,
462 struct int_pair *grads)
463 {
464 int interp_val = 0;
465 int errcnt = 0;
466
467 int sample = 0, code = 0;
468 ushort *line_buf_cur = line_buf + pos;
469 int Ra = line_buf_cur[-1];
470 int Rb = line_buf_cur[-2 - params->line_width];
471 int Rc = line_buf_cur[-3 - params->line_width];
472 int Rd = line_buf_cur[-1 - params->line_width];
473 int Rg = line_buf_cur[1];
474
475 int grad, gradient;
476
477 grad = fuji_quant_gradient(params, Rb - Rc, Rc - Ra);
478 gradient = _abs(grad);
479
480 if ((Rb > Rc && Rb > Rd) || (Rb < Rc && Rb < Rd))
481 interp_val = (Rg + Ra + 2 * Rb) >> 2;
482 else
483 interp_val = (Ra + Rg) >> 1;
484
485 fuji_zerobits(info, &sample);
486
487 if (sample < params->max_bits - params->raw_bits - 1)
488 {
489 int decBits = bitDiff(grads[gradient].value1, grads[gradient].value2);
490 fuji_read_code(info, &code, decBits);
491 code += sample << decBits;
492 }
493 else
494 {
495 fuji_read_code(info, &code, params->raw_bits);
496 code++;
497 }
498
499 if (code < 0 || code >= params->total_values)
500 errcnt++;
501
502 if (code & 1)
503 code = -1 - code / 2;
504 else
505 code /= 2;
506
507 grads[gradient].value1 += _abs(code);
508 if (grads[gradient].value2 == params->min_value)
509 {
510 grads[gradient].value1 >>= 1;
511 grads[gradient].value2 >>= 1;
512 }
513 grads[gradient].value2++;
514 if (grad < 0)
515 interp_val -= code;
516 else
517 interp_val += code;
518 if (interp_val < 0)
519 interp_val += params->total_values;
520 else if (interp_val > params->q_point[4])
521 interp_val -= params->total_values;
522
523 if (interp_val >= 0)
524 line_buf_cur[0] = _min(interp_val, params->q_point[4]);
525 else
526 line_buf_cur[0] = 0;
527 return errcnt;
528 }
529
fuji_decode_interpolation_even(int line_width,ushort * line_buf,int pos)530 static void fuji_decode_interpolation_even(int line_width, ushort *line_buf, int pos)
531 {
532 ushort *line_buf_cur = line_buf + pos;
533 int Rb = line_buf_cur[-2 - line_width];
534 int Rc = line_buf_cur[-3 - line_width];
535 int Rd = line_buf_cur[-1 - line_width];
536 int Rf = line_buf_cur[-4 - 2 * line_width];
537 int diffRcRb = _abs(Rc - Rb);
538 int diffRfRb = _abs(Rf - Rb);
539 int diffRdRb = _abs(Rd - Rb);
540 if (diffRcRb > diffRfRb && diffRcRb > diffRdRb)
541 *line_buf_cur = (Rf + Rd + 2 * Rb) >> 2;
542 else if (diffRdRb > diffRcRb && diffRdRb > diffRfRb)
543 *line_buf_cur = (Rf + Rc + 2 * Rb) >> 2;
544 else
545 *line_buf_cur = (Rd + Rc + 2 * Rb) >> 2;
546 }
547
fuji_extend_generic(ushort * linebuf[_ltotal],int line_width,int start,int end)548 static void fuji_extend_generic(ushort *linebuf[_ltotal], int line_width, int start, int end)
549 {
550 for (int i = start; i <= end; i++)
551 {
552 linebuf[i][0] = linebuf[i - 1][1];
553 linebuf[i][line_width + 1] = linebuf[i - 1][line_width];
554 }
555 }
556
fuji_extend_red(ushort * linebuf[_ltotal],int line_width)557 static void fuji_extend_red(ushort *linebuf[_ltotal], int line_width)
558 {
559 fuji_extend_generic(linebuf, line_width, _R2, _R4);
560 }
561
fuji_extend_green(ushort * linebuf[_ltotal],int line_width)562 static void fuji_extend_green(ushort *linebuf[_ltotal], int line_width)
563 {
564 fuji_extend_generic(linebuf, line_width, _G2, _G7);
565 }
566
fuji_extend_blue(ushort * linebuf[_ltotal],int line_width)567 static void fuji_extend_blue(ushort *linebuf[_ltotal], int line_width)
568 {
569 fuji_extend_generic(linebuf, line_width, _B2, _B4);
570 }
571
xtrans_decode_block(struct fuji_compressed_block * info,const struct fuji_compressed_params * params,int cur_line)572 void LibRaw::xtrans_decode_block(struct fuji_compressed_block *info, const struct fuji_compressed_params *params,
573 int cur_line)
574 {
575 int r_even_pos = 0, r_odd_pos = 1;
576 int g_even_pos = 0, g_odd_pos = 1;
577 int b_even_pos = 0, b_odd_pos = 1;
578
579 int errcnt = 0;
580
581 const int line_width = params->line_width;
582
583 while (g_even_pos < line_width || g_odd_pos < line_width)
584 {
585 if (g_even_pos < line_width)
586 {
587 fuji_decode_interpolation_even(line_width, info->linebuf[_R2] + 1, r_even_pos);
588 r_even_pos += 2;
589 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G2] + 1, g_even_pos, info->grad_even[0]);
590 g_even_pos += 2;
591 }
592 if (g_even_pos > 8)
593 {
594 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R2] + 1, r_odd_pos, info->grad_odd[0]);
595 r_odd_pos += 2;
596 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G2] + 1, g_odd_pos, info->grad_odd[0]);
597 g_odd_pos += 2;
598 }
599 }
600
601 fuji_extend_red(info->linebuf, line_width);
602 fuji_extend_green(info->linebuf, line_width);
603
604 g_even_pos = 0, g_odd_pos = 1;
605
606 while (g_even_pos < line_width || g_odd_pos < line_width)
607 {
608 if (g_even_pos < line_width)
609 {
610 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G3] + 1, g_even_pos, info->grad_even[1]);
611 g_even_pos += 2;
612 fuji_decode_interpolation_even(line_width, info->linebuf[_B2] + 1, b_even_pos);
613 b_even_pos += 2;
614 }
615 if (g_even_pos > 8)
616 {
617 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G3] + 1, g_odd_pos, info->grad_odd[1]);
618 g_odd_pos += 2;
619 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B2] + 1, b_odd_pos, info->grad_odd[1]);
620 b_odd_pos += 2;
621 }
622 }
623
624 fuji_extend_green(info->linebuf, line_width);
625 fuji_extend_blue(info->linebuf, line_width);
626
627 r_even_pos = 0, r_odd_pos = 1;
628 g_even_pos = 0, g_odd_pos = 1;
629
630 while (g_even_pos < line_width || g_odd_pos < line_width)
631 {
632 if (g_even_pos < line_width)
633 {
634 if (r_even_pos & 3)
635 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_R3] + 1, r_even_pos, info->grad_even[2]);
636 else
637 fuji_decode_interpolation_even(line_width, info->linebuf[_R3] + 1, r_even_pos);
638 r_even_pos += 2;
639 fuji_decode_interpolation_even(line_width, info->linebuf[_G4] + 1, g_even_pos);
640 g_even_pos += 2;
641 }
642 if (g_even_pos > 8)
643 {
644 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R3] + 1, r_odd_pos, info->grad_odd[2]);
645 r_odd_pos += 2;
646 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G4] + 1, g_odd_pos, info->grad_odd[2]);
647 g_odd_pos += 2;
648 }
649 }
650
651 fuji_extend_red(info->linebuf, line_width);
652 fuji_extend_green(info->linebuf, line_width);
653
654 g_even_pos = 0, g_odd_pos = 1;
655 b_even_pos = 0, b_odd_pos = 1;
656
657 while (g_even_pos < line_width || g_odd_pos < line_width)
658 {
659 if (g_even_pos < line_width)
660 {
661 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G5] + 1, g_even_pos, info->grad_even[0]);
662 g_even_pos += 2;
663 if ((b_even_pos & 3) == 2)
664 fuji_decode_interpolation_even(line_width, info->linebuf[_B3] + 1, b_even_pos);
665 else
666 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_B3] + 1, b_even_pos, info->grad_even[0]);
667 b_even_pos += 2;
668 }
669 if (g_even_pos > 8)
670 {
671 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G5] + 1, g_odd_pos, info->grad_odd[0]);
672 g_odd_pos += 2;
673 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B3] + 1, b_odd_pos, info->grad_odd[0]);
674 b_odd_pos += 2;
675 }
676 }
677
678 fuji_extend_green(info->linebuf, line_width);
679 fuji_extend_blue(info->linebuf, line_width);
680
681 r_even_pos = 0, r_odd_pos = 1;
682 g_even_pos = 0, g_odd_pos = 1;
683
684 while (g_even_pos < line_width || g_odd_pos < line_width)
685 {
686 if (g_even_pos < line_width)
687 {
688 if ((r_even_pos & 3) == 2)
689 fuji_decode_interpolation_even(line_width, info->linebuf[_R4] + 1, r_even_pos);
690 else
691 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_R4] + 1, r_even_pos, info->grad_even[1]);
692 r_even_pos += 2;
693 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G6] + 1, g_even_pos, info->grad_even[1]);
694 g_even_pos += 2;
695 }
696 if (g_even_pos > 8)
697 {
698 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R4] + 1, r_odd_pos, info->grad_odd[1]);
699 r_odd_pos += 2;
700 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G6] + 1, g_odd_pos, info->grad_odd[1]);
701 g_odd_pos += 2;
702 }
703 }
704
705 fuji_extend_red(info->linebuf, line_width);
706 fuji_extend_green(info->linebuf, line_width);
707
708 g_even_pos = 0, g_odd_pos = 1;
709 b_even_pos = 0, b_odd_pos = 1;
710
711 while (g_even_pos < line_width || g_odd_pos < line_width)
712 {
713 if (g_even_pos < line_width)
714 {
715 fuji_decode_interpolation_even(line_width, info->linebuf[_G7] + 1, g_even_pos);
716 g_even_pos += 2;
717 if (b_even_pos & 3)
718 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_B4] + 1, b_even_pos, info->grad_even[2]);
719 else
720 fuji_decode_interpolation_even(line_width, info->linebuf[_B4] + 1, b_even_pos);
721 b_even_pos += 2;
722 }
723 if (g_even_pos > 8)
724 {
725 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G7] + 1, g_odd_pos, info->grad_odd[2]);
726 g_odd_pos += 2;
727 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B4] + 1, b_odd_pos, info->grad_odd[2]);
728 b_odd_pos += 2;
729 }
730 }
731
732 fuji_extend_green(info->linebuf, line_width);
733 fuji_extend_blue(info->linebuf, line_width);
734
735 if (errcnt)
736 derror();
737 }
738
fuji_bayer_decode_block(struct fuji_compressed_block * info,const struct fuji_compressed_params * params,int cur_line)739 void LibRaw::fuji_bayer_decode_block(struct fuji_compressed_block *info, const struct fuji_compressed_params *params,
740 int cur_line)
741 {
742 int r_even_pos = 0, r_odd_pos = 1;
743 int g_even_pos = 0, g_odd_pos = 1;
744 int b_even_pos = 0, b_odd_pos = 1;
745
746 int errcnt = 0;
747
748 const int line_width = params->line_width;
749
750 while (g_even_pos < line_width || g_odd_pos < line_width)
751 {
752 if (g_even_pos < line_width)
753 {
754 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_R2] + 1, r_even_pos, info->grad_even[0]);
755 r_even_pos += 2;
756 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G2] + 1, g_even_pos, info->grad_even[0]);
757 g_even_pos += 2;
758 }
759 if (g_even_pos > 8)
760 {
761 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R2] + 1, r_odd_pos, info->grad_odd[0]);
762 r_odd_pos += 2;
763 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G2] + 1, g_odd_pos, info->grad_odd[0]);
764 g_odd_pos += 2;
765 }
766 }
767
768 fuji_extend_red(info->linebuf, line_width);
769 fuji_extend_green(info->linebuf, line_width);
770
771 g_even_pos = 0, g_odd_pos = 1;
772
773 while (g_even_pos < line_width || g_odd_pos < line_width)
774 {
775 if (g_even_pos < line_width)
776 {
777 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G3] + 1, g_even_pos, info->grad_even[1]);
778 g_even_pos += 2;
779 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_B2] + 1, b_even_pos, info->grad_even[1]);
780 b_even_pos += 2;
781 }
782 if (g_even_pos > 8)
783 {
784 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G3] + 1, g_odd_pos, info->grad_odd[1]);
785 g_odd_pos += 2;
786 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B2] + 1, b_odd_pos, info->grad_odd[1]);
787 b_odd_pos += 2;
788 }
789 }
790
791 fuji_extend_green(info->linebuf, line_width);
792 fuji_extend_blue(info->linebuf, line_width);
793
794 r_even_pos = 0, r_odd_pos = 1;
795 g_even_pos = 0, g_odd_pos = 1;
796
797 while (g_even_pos < line_width || g_odd_pos < line_width)
798 {
799 if (g_even_pos < line_width)
800 {
801 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_R3] + 1, r_even_pos, info->grad_even[2]);
802 r_even_pos += 2;
803 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G4] + 1, g_even_pos, info->grad_even[2]);
804 g_even_pos += 2;
805 }
806 if (g_even_pos > 8)
807 {
808 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R3] + 1, r_odd_pos, info->grad_odd[2]);
809 r_odd_pos += 2;
810 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G4] + 1, g_odd_pos, info->grad_odd[2]);
811 g_odd_pos += 2;
812 }
813 }
814
815 fuji_extend_red(info->linebuf, line_width);
816 fuji_extend_green(info->linebuf, line_width);
817
818 g_even_pos = 0, g_odd_pos = 1;
819 b_even_pos = 0, b_odd_pos = 1;
820
821 while (g_even_pos < line_width || g_odd_pos < line_width)
822 {
823 if (g_even_pos < line_width)
824 {
825 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G5] + 1, g_even_pos, info->grad_even[0]);
826 g_even_pos += 2;
827 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_B3] + 1, b_even_pos, info->grad_even[0]);
828 b_even_pos += 2;
829 }
830 if (g_even_pos > 8)
831 {
832 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G5] + 1, g_odd_pos, info->grad_odd[0]);
833 g_odd_pos += 2;
834 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B3] + 1, b_odd_pos, info->grad_odd[0]);
835 b_odd_pos += 2;
836 }
837 }
838
839 fuji_extend_green(info->linebuf, line_width);
840 fuji_extend_blue(info->linebuf, line_width);
841
842 r_even_pos = 0, r_odd_pos = 1;
843 g_even_pos = 0, g_odd_pos = 1;
844
845 while (g_even_pos < line_width || g_odd_pos < line_width)
846 {
847 if (g_even_pos < line_width)
848 {
849 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_R4] + 1, r_even_pos, info->grad_even[1]);
850 r_even_pos += 2;
851 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G6] + 1, g_even_pos, info->grad_even[1]);
852 g_even_pos += 2;
853 }
854 if (g_even_pos > 8)
855 {
856 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_R4] + 1, r_odd_pos, info->grad_odd[1]);
857 r_odd_pos += 2;
858 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G6] + 1, g_odd_pos, info->grad_odd[1]);
859 g_odd_pos += 2;
860 }
861 }
862
863 fuji_extend_red(info->linebuf, line_width);
864 fuji_extend_green(info->linebuf, line_width);
865
866 g_even_pos = 0, g_odd_pos = 1;
867 b_even_pos = 0, b_odd_pos = 1;
868
869 while (g_even_pos < line_width || g_odd_pos < line_width)
870 {
871 if (g_even_pos < line_width)
872 {
873 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_G7] + 1, g_even_pos, info->grad_even[2]);
874 g_even_pos += 2;
875 errcnt += fuji_decode_sample_even(info, params, info->linebuf[_B4] + 1, b_even_pos, info->grad_even[2]);
876 b_even_pos += 2;
877 }
878 if (g_even_pos > 8)
879 {
880 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_G7] + 1, g_odd_pos, info->grad_odd[2]);
881 g_odd_pos += 2;
882 errcnt += fuji_decode_sample_odd(info, params, info->linebuf[_B4] + 1, b_odd_pos, info->grad_odd[2]);
883 b_odd_pos += 2;
884 }
885 }
886
887 fuji_extend_green(info->linebuf, line_width);
888 fuji_extend_blue(info->linebuf, line_width);
889
890 if (errcnt)
891 derror();
892 }
893
fuji_decode_strip(const struct fuji_compressed_params * info_common,int cur_block,INT64 raw_offset,unsigned dsize)894 void LibRaw::fuji_decode_strip(const struct fuji_compressed_params *info_common, int cur_block, INT64 raw_offset,
895 unsigned dsize)
896 {
897 int cur_block_width, cur_line;
898 unsigned line_size;
899 struct fuji_compressed_block info;
900
901 init_fuji_block(&info, info_common, raw_offset, dsize);
902 line_size = sizeof(ushort) * (info_common->line_width + 2);
903
904 cur_block_width = libraw_internal_data.unpacker_data.fuji_block_width;
905 if (cur_block + 1 == libraw_internal_data.unpacker_data.fuji_total_blocks)
906 {
907 cur_block_width = imgdata.sizes.raw_width - (libraw_internal_data.unpacker_data.fuji_block_width * cur_block);
908 /* Old code, may get incorrect results on GFX50, but luckily large optical black
909 cur_block_width = imgdata.sizes.raw_width % libraw_internal_data.unpacker_data.fuji_block_width;
910 */
911 }
912
913 struct i_pair
914 {
915 int a, b;
916 };
917 const i_pair mtable[6] = {{_R0, _R3}, {_R1, _R4}, {_G0, _G6}, {_G1, _G7}, {_B0, _B3}, {_B1, _B4}},
918 ztable[3] = {{_R2, 3}, {_G2, 6}, {_B2, 3}};
919 for (cur_line = 0; cur_line < libraw_internal_data.unpacker_data.fuji_total_lines; cur_line++)
920 {
921 if (libraw_internal_data.unpacker_data.fuji_raw_type == 16)
922 xtrans_decode_block(&info, info_common, cur_line);
923 else
924 fuji_bayer_decode_block(&info, info_common, cur_line);
925
926 // copy data from line buffers and advance
927 for (int i = 0; i < 6; i++)
928 memcpy(info.linebuf[mtable[i].a], info.linebuf[mtable[i].b], line_size);
929
930 if (libraw_internal_data.unpacker_data.fuji_raw_type == 16)
931 copy_line_to_xtrans(&info, cur_line, cur_block, cur_block_width);
932 else
933 copy_line_to_bayer(&info, cur_line, cur_block, cur_block_width);
934
935 for (int i = 0; i < 3; i++)
936 {
937 memset(info.linebuf[ztable[i].a], 0, ztable[i].b * line_size);
938 info.linebuf[ztable[i].a][0] = info.linebuf[ztable[i].a - 1][1];
939 info.linebuf[ztable[i].a][info_common->line_width + 1] = info.linebuf[ztable[i].a - 1][info_common->line_width];
940 }
941 }
942
943 // release data
944 free(info.linealloc);
945 free(info.cur_buf);
946 }
947
fuji_compressed_load_raw()948 void LibRaw::fuji_compressed_load_raw()
949 {
950 struct fuji_compressed_params common_info;
951 int cur_block;
952 unsigned line_size, *block_sizes;
953 INT64 raw_offset, *raw_block_offsets;
954 // struct fuji_compressed_block info;
955
956 init_fuji_compr(&common_info);
957 line_size = sizeof(ushort) * (common_info.line_width + 2);
958
959 // read block sizes
960 block_sizes = (unsigned *)malloc(sizeof(unsigned) * libraw_internal_data.unpacker_data.fuji_total_blocks);
961 merror(block_sizes, "fuji_compressed_load_raw()");
962 raw_block_offsets = (INT64 *)malloc(sizeof(INT64) * libraw_internal_data.unpacker_data.fuji_total_blocks);
963 merror(raw_block_offsets, "fuji_compressed_load_raw()");
964
965 raw_offset = sizeof(unsigned) * libraw_internal_data.unpacker_data.fuji_total_blocks;
966 if (raw_offset & 0xC)
967 raw_offset += 0x10 - (raw_offset & 0xC);
968
969 raw_offset += libraw_internal_data.unpacker_data.data_offset;
970
971 libraw_internal_data.internal_data.input->seek(libraw_internal_data.unpacker_data.data_offset, SEEK_SET);
972 libraw_internal_data.internal_data.input->read(
973 block_sizes, 1, sizeof(unsigned) * libraw_internal_data.unpacker_data.fuji_total_blocks);
974
975 raw_block_offsets[0] = raw_offset;
976 // calculating raw block offsets
977 for (cur_block = 0; cur_block < libraw_internal_data.unpacker_data.fuji_total_blocks; cur_block++)
978 {
979 unsigned bsize = sgetn(4, (uchar *)(block_sizes + cur_block));
980 block_sizes[cur_block] = bsize;
981 }
982
983 for (cur_block = 1; cur_block < libraw_internal_data.unpacker_data.fuji_total_blocks; cur_block++)
984 raw_block_offsets[cur_block] = raw_block_offsets[cur_block - 1] + block_sizes[cur_block - 1];
985
986 fuji_decode_loop(&common_info, libraw_internal_data.unpacker_data.fuji_total_blocks, raw_block_offsets, block_sizes);
987
988 free(block_sizes);
989 free(raw_block_offsets);
990 free(common_info.q_table);
991 }
992
fuji_decode_loop(const struct fuji_compressed_params * common_info,int count,INT64 * raw_block_offsets,unsigned * block_sizes)993 void LibRaw::fuji_decode_loop(const struct fuji_compressed_params *common_info, int count, INT64 *raw_block_offsets,
994 unsigned *block_sizes)
995 {
996 int cur_block;
997 #ifdef LIBRAW_USE_OPENMP
998 #pragma omp parallel for private(cur_block)
999 #endif
1000 for (cur_block = 0; cur_block < count; cur_block++)
1001 {
1002 fuji_decode_strip(common_info, cur_block, raw_block_offsets[cur_block], block_sizes[cur_block]);
1003 }
1004 }
1005
parse_fuji_compressed_header()1006 void LibRaw::parse_fuji_compressed_header()
1007 {
1008 unsigned signature, version, h_raw_type, h_raw_bits, h_raw_height, h_raw_rounded_width, h_raw_width, h_block_size,
1009 h_blocks_in_row, h_total_lines;
1010
1011 uchar header[16];
1012
1013 libraw_internal_data.internal_data.input->seek(libraw_internal_data.unpacker_data.data_offset, SEEK_SET);
1014 libraw_internal_data.internal_data.input->read(header, 1, sizeof(header));
1015
1016 // read all header
1017 signature = sgetn(2, header);
1018 version = header[2];
1019 h_raw_type = header[3];
1020 h_raw_bits = header[4];
1021 h_raw_height = sgetn(2, header + 5);
1022 h_raw_rounded_width = sgetn(2, header + 7);
1023 h_raw_width = sgetn(2, header + 9);
1024 h_block_size = sgetn(2, header + 11);
1025 h_blocks_in_row = header[13];
1026 h_total_lines = sgetn(2, header + 14);
1027
1028 // general validation
1029 if (signature != 0x4953 || version != 1 || h_raw_height > 0x3000 || h_raw_height < 6 || h_raw_height % 6 ||
1030 h_block_size < 1 || h_raw_width > 0x3000 || h_raw_width < 0x300 || h_raw_width % 24 ||
1031 h_raw_rounded_width > 0x3000 || h_raw_rounded_width < h_block_size || h_raw_rounded_width % h_block_size ||
1032 h_raw_rounded_width - h_raw_width >= h_block_size || h_block_size != 0x300 || h_blocks_in_row > 0x10 ||
1033 h_blocks_in_row == 0 || h_blocks_in_row != h_raw_rounded_width / h_block_size || h_total_lines > 0x800 ||
1034 h_total_lines == 0 || h_total_lines != h_raw_height / 6 || (h_raw_bits != 12 && h_raw_bits != 14) ||
1035 (h_raw_type != 16 && h_raw_type != 0))
1036 return;
1037
1038 // modify data
1039 libraw_internal_data.unpacker_data.fuji_total_lines = h_total_lines;
1040 libraw_internal_data.unpacker_data.fuji_total_blocks = h_blocks_in_row;
1041 libraw_internal_data.unpacker_data.fuji_block_width = h_block_size;
1042 libraw_internal_data.unpacker_data.fuji_bits = h_raw_bits;
1043 libraw_internal_data.unpacker_data.fuji_raw_type = h_raw_type;
1044 imgdata.sizes.raw_width = h_raw_width;
1045 imgdata.sizes.raw_height = h_raw_height;
1046 libraw_internal_data.unpacker_data.data_offset += 16;
1047 load_raw = &LibRaw::fuji_compressed_load_raw;
1048 }
1049
1050 #undef _abs
1051 #undef _min
1052