summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jpegli/test_utils-inl.h
blob: 4fbcb721e4c61da3e907b66b1efe4dfaa079ded8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This template file is included in both the libjpeg_test_util.cc and the
// test_utils.cc files with different JPEG_API_FN macros and possibly different
// include paths for the jpeg headers.

// Sequential non-interleaved.
constexpr jpeg_scan_info kScript1[] = {
    {1, {0}, 0, 63, 0, 0},
    {1, {1}, 0, 63, 0, 0},
    {1, {2}, 0, 63, 0, 0},
};
// Sequential partially interleaved, chroma first.
constexpr jpeg_scan_info kScript2[] = {
    {2, {1, 2}, 0, 63, 0, 0},
    {1, {0}, 0, 63, 0, 0},
};

// Rest of the scan scripts are progressive.

constexpr jpeg_scan_info kScript3[] = {
    // Interleaved full DC.
    {3, {0, 1, 2}, 0, 0, 0, 0},
    // Full AC scans.
    {1, {0}, 1, 63, 0, 0},
    {1, {1}, 1, 63, 0, 0},
    {1, {2}, 1, 63, 0, 0},
};
constexpr jpeg_scan_info kScript4[] = {
    // Non-interleaved full DC.
    {1, {0}, 0, 0, 0, 0},
    {1, {1}, 0, 0, 0, 0},
    {1, {2}, 0, 0, 0, 0},
    // Full AC scans.
    {1, {0}, 1, 63, 0, 0},
    {1, {1}, 1, 63, 0, 0},
    {1, {2}, 1, 63, 0, 0},
};
constexpr jpeg_scan_info kScript5[] = {
    // Partially interleaved full DC, chroma first.
    {2, {1, 2}, 0, 0, 0, 0},
    {1, {0}, 0, 0, 0, 0},
    // AC shifted by 1 bit.
    {1, {0}, 1, 63, 0, 1},
    {1, {1}, 1, 63, 0, 1},
    {1, {2}, 1, 63, 0, 1},
    // AC refinement scan.
    {1, {0}, 1, 63, 1, 0},
    {1, {1}, 1, 63, 1, 0},
    {1, {2}, 1, 63, 1, 0},
};
constexpr jpeg_scan_info kScript6[] = {
    // Interleaved DC shifted by 2 bits.
    {3, {0, 1, 2}, 0, 0, 0, 2},
    // Interleaved DC refinement scans.
    {3, {0, 1, 2}, 0, 0, 2, 1},
    {3, {0, 1, 2}, 0, 0, 1, 0},
    // Full AC scans.
    {1, {0}, 1, 63, 0, 0},
    {1, {1}, 1, 63, 0, 0},
    {1, {2}, 1, 63, 0, 0},
};

constexpr jpeg_scan_info kScript7[] = {
    // Non-interleaved DC shifted by 2 bits.
    {1, {0}, 0, 0, 0, 2},
    {1, {1}, 0, 0, 0, 2},
    {1, {2}, 0, 0, 0, 2},
    // Non-interleaved DC first refinement scans.
    {1, {0}, 0, 0, 2, 1},
    {1, {1}, 0, 0, 2, 1},
    {1, {2}, 0, 0, 2, 1},
    // Non-interleaved DC second refinement scans.
    {1, {0}, 0, 0, 1, 0},
    {1, {1}, 0, 0, 1, 0},
    {1, {2}, 0, 0, 1, 0},
    // Full AC scans.
    {1, {0}, 1, 63, 0, 0},
    {1, {1}, 1, 63, 0, 0},
    {1, {2}, 1, 63, 0, 0},
};

constexpr jpeg_scan_info kScript8[] = {
    // Partially interleaved DC shifted by 2 bits, chroma first
    {2, {1, 2}, 0, 0, 0, 2},
    {1, {0}, 0, 0, 0, 2},
    // Partially interleaved DC first refinement scans.
    {2, {0, 2}, 0, 0, 2, 1},
    {1, {1}, 0, 0, 2, 1},
    // Partially interleaved DC first refinement scans, chroma first.
    {2, {1, 2}, 0, 0, 1, 0},
    {1, {0}, 0, 0, 1, 0},
    // Full AC scans.
    {1, {0}, 1, 63, 0, 0},
    {1, {1}, 1, 63, 0, 0},
    {1, {2}, 1, 63, 0, 0},
};

constexpr jpeg_scan_info kScript9[] = {
    // Interleaved full DC.
    {3, {0, 1, 2}, 0, 0, 0, 0},
    // AC scans for component 0
    // shifted by 1 bit, two spectral ranges
    {1, {0}, 1, 6, 0, 1},
    {1, {0}, 7, 63, 0, 1},
    // refinement scan, full
    {1, {0}, 1, 63, 1, 0},
    // AC scans for component 1
    // shifted by 1 bit, full
    {1, {1}, 1, 63, 0, 1},
    // refinement scan, two spectral ranges
    {1, {1}, 1, 6, 1, 0},
    {1, {1}, 7, 63, 1, 0},
    // AC scans for component 2
    // shifted by 1 bit, two spectral ranges
    {1, {2}, 1, 6, 0, 1},
    {1, {2}, 7, 63, 0, 1},
    // refinement scan, two spectral ranges (but different from above)
    {1, {2}, 1, 16, 1, 0},
    {1, {2}, 17, 63, 1, 0},
};

constexpr jpeg_scan_info kScript10[] = {
    // Interleaved full DC.
    {3, {0, 1, 2}, 0, 0, 0, 0},
    // AC scans for spectral range 1..16
    // shifted by 1
    {1, {0}, 1, 16, 0, 1},
    {1, {1}, 1, 16, 0, 1},
    {1, {2}, 1, 16, 0, 1},
    // refinement scans, two sub-ranges
    {1, {0}, 1, 8, 1, 0},
    {1, {0}, 9, 16, 1, 0},
    {1, {1}, 1, 8, 1, 0},
    {1, {1}, 9, 16, 1, 0},
    {1, {2}, 1, 8, 1, 0},
    {1, {2}, 9, 16, 1, 0},
    // AC scans for spectral range 17..63
    {1, {0}, 17, 63, 0, 1},
    {1, {1}, 17, 63, 0, 1},
    {1, {2}, 17, 63, 0, 1},
    // refinement scans, two sub-ranges
    {1, {0}, 17, 28, 1, 0},
    {1, {0}, 29, 63, 1, 0},
    {1, {1}, 17, 28, 1, 0},
    {1, {1}, 29, 63, 1, 0},
    {1, {2}, 17, 28, 1, 0},
    {1, {2}, 29, 63, 1, 0},
};

struct ScanScript {
  int num_scans;
  const jpeg_scan_info* scans;
};

constexpr ScanScript kTestScript[] = {
    {ARRAY_SIZE(kScript1), kScript1}, {ARRAY_SIZE(kScript2), kScript2},
    {ARRAY_SIZE(kScript3), kScript3}, {ARRAY_SIZE(kScript4), kScript4},
    {ARRAY_SIZE(kScript5), kScript5}, {ARRAY_SIZE(kScript6), kScript6},
    {ARRAY_SIZE(kScript7), kScript7}, {ARRAY_SIZE(kScript8), kScript8},
    {ARRAY_SIZE(kScript9), kScript9}, {ARRAY_SIZE(kScript10), kScript10},
};
constexpr int kNumTestScripts = ARRAY_SIZE(kTestScript);

void SetScanDecompressParams(const DecompressParams& dparams,
                             j_decompress_ptr cinfo, int scan_number) {
  const ScanDecompressParams* sparams = nullptr;
  for (const auto& sp : dparams.scan_params) {
    if (scan_number <= sp.max_scan_number) {
      sparams = &sp;
      break;
    }
  }
  if (sparams == nullptr) {
    return;
  }
  if (dparams.quantize_colors) {
    cinfo->dither_mode = static_cast<J_DITHER_MODE>(sparams->dither_mode);
    if (sparams->color_quant_mode == CQUANT_1PASS) {
      cinfo->two_pass_quantize = FALSE;
      cinfo->colormap = nullptr;
    } else if (sparams->color_quant_mode == CQUANT_2PASS) {
      JXL_CHECK(cinfo->out_color_space == JCS_RGB);
      cinfo->two_pass_quantize = TRUE;
      cinfo->colormap = nullptr;
    } else if (sparams->color_quant_mode == CQUANT_EXTERNAL) {
      JXL_CHECK(cinfo->out_color_space == JCS_RGB);
      cinfo->two_pass_quantize = FALSE;
      bool have_colormap = cinfo->colormap != nullptr;
      cinfo->actual_number_of_colors = kTestColorMapNumColors;
      cinfo->colormap = (*cinfo->mem->alloc_sarray)(
          reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE,
          cinfo->actual_number_of_colors, 3);
      jxl::msan::UnpoisonMemory(reinterpret_cast<void*>(cinfo->colormap),
                                3 * sizeof(JSAMPLE*));
      for (int i = 0; i < kTestColorMapNumColors; ++i) {
        cinfo->colormap[0][i] = (kTestColorMap[i] >> 16) & 0xff;
        cinfo->colormap[1][i] = (kTestColorMap[i] >> 8) & 0xff;
        cinfo->colormap[2][i] = (kTestColorMap[i] >> 0) & 0xff;
      }
      if (have_colormap) {
        JPEG_API_FN(new_colormap)(cinfo);
      }
    } else if (sparams->color_quant_mode == CQUANT_REUSE) {
      JXL_CHECK(cinfo->out_color_space == JCS_RGB);
      JXL_CHECK(cinfo->colormap);
    }
  }
}

void SetDecompressParams(const DecompressParams& dparams,
                         j_decompress_ptr cinfo) {
  cinfo->do_block_smoothing = dparams.do_block_smoothing ? 1 : 0;
  cinfo->do_fancy_upsampling = dparams.do_fancy_upsampling ? 1 : 0;
  if (dparams.output_mode == RAW_DATA) {
    cinfo->raw_data_out = TRUE;
  }
  if (dparams.set_out_color_space) {
    cinfo->out_color_space =
        static_cast<J_COLOR_SPACE>(dparams.out_color_space);
    if (dparams.out_color_space == JCS_UNKNOWN) {
      cinfo->jpeg_color_space = JCS_UNKNOWN;
    }
  }
  cinfo->scale_num = dparams.scale_num;
  cinfo->scale_denom = dparams.scale_denom;
  cinfo->quantize_colors = dparams.quantize_colors ? 1 : 0;
  cinfo->desired_number_of_colors = dparams.desired_number_of_colors;
  if (!dparams.scan_params.empty()) {
    if (cinfo->buffered_image) {
      for (const auto& sparams : dparams.scan_params) {
        if (sparams.color_quant_mode == CQUANT_1PASS) {
          cinfo->enable_1pass_quant = TRUE;
        } else if (sparams.color_quant_mode == CQUANT_2PASS) {
          cinfo->enable_2pass_quant = TRUE;
        } else if (sparams.color_quant_mode == CQUANT_EXTERNAL) {
          cinfo->enable_external_quant = TRUE;
        }
      }
      SetScanDecompressParams(dparams, cinfo, 1);
    } else {
      SetScanDecompressParams(dparams, cinfo, kLastScan);
    }
  }
}

void CheckMarkerPresent(j_decompress_ptr cinfo, uint8_t marker_type) {
  bool marker_found = false;
  for (jpeg_saved_marker_ptr marker = cinfo->marker_list; marker != nullptr;
       marker = marker->next) {
    jxl::msan::UnpoisonMemory(marker, sizeof(*marker));
    jxl::msan::UnpoisonMemory(marker->data, marker->data_length);
    if (marker->marker == marker_type &&
        marker->data_length == sizeof(kMarkerData) &&
        memcmp(marker->data, kMarkerData, sizeof(kMarkerData)) == 0) {
      marker_found = true;
    }
  }
  JXL_CHECK(marker_found);
}

void VerifyHeader(const CompressParams& jparams, j_decompress_ptr cinfo) {
  if (jparams.set_jpeg_colorspace) {
    JXL_CHECK(cinfo->jpeg_color_space == jparams.jpeg_color_space);
  }
  if (jparams.override_JFIF >= 0) {
    JXL_CHECK(cinfo->saw_JFIF_marker == jparams.override_JFIF);
  }
  if (jparams.override_Adobe >= 0) {
    JXL_CHECK(cinfo->saw_Adobe_marker == jparams.override_Adobe);
  }
  if (jparams.add_marker) {
    CheckMarkerPresent(cinfo, kSpecialMarker0);
    CheckMarkerPresent(cinfo, kSpecialMarker1);
  }
  jxl::msan::UnpoisonMemory(
      cinfo->comp_info, cinfo->num_components * sizeof(cinfo->comp_info[0]));
  int max_h_samp_factor = 1;
  int max_v_samp_factor = 1;
  for (int i = 0; i < cinfo->num_components; ++i) {
    jpeg_component_info* comp = &cinfo->comp_info[i];
    if (!jparams.comp_ids.empty()) {
      JXL_CHECK(comp->component_id == jparams.comp_ids[i]);
    }
    if (!jparams.h_sampling.empty()) {
      JXL_CHECK(comp->h_samp_factor == jparams.h_sampling[i]);
    }
    if (!jparams.v_sampling.empty()) {
      JXL_CHECK(comp->v_samp_factor == jparams.v_sampling[i]);
    }
    if (!jparams.quant_indexes.empty()) {
      JXL_CHECK(comp->quant_tbl_no == jparams.quant_indexes[i]);
    }
    max_h_samp_factor = std::max(max_h_samp_factor, comp->h_samp_factor);
    max_v_samp_factor = std::max(max_v_samp_factor, comp->v_samp_factor);
  }
  JXL_CHECK(max_h_samp_factor == cinfo->max_h_samp_factor);
  JXL_CHECK(max_v_samp_factor == cinfo->max_v_samp_factor);
  int referenced_tables[NUM_QUANT_TBLS] = {};
  for (int i = 0; i < cinfo->num_components; ++i) {
    jpeg_component_info* comp = &cinfo->comp_info[i];
    JXL_CHECK(comp->width_in_blocks ==
              DivCeil(cinfo->image_width * comp->h_samp_factor,
                      max_h_samp_factor * DCTSIZE));
    JXL_CHECK(comp->height_in_blocks ==
              DivCeil(cinfo->image_height * comp->v_samp_factor,
                      max_v_samp_factor * DCTSIZE));
    referenced_tables[comp->quant_tbl_no] = 1;
  }
  for (const auto& table : jparams.quant_tables) {
    JQUANT_TBL* quant_table = cinfo->quant_tbl_ptrs[table.slot_idx];
    if (!referenced_tables[table.slot_idx]) {
      JXL_CHECK(quant_table == nullptr);
      continue;
    }
    JXL_CHECK(quant_table != nullptr);
    jxl::msan::UnpoisonMemory(quant_table, sizeof(*quant_table));
    for (int k = 0; k < DCTSIZE2; ++k) {
      JXL_CHECK(quant_table->quantval[k] == table.quantval[k]);
    }
  }
}

void VerifyScanHeader(const CompressParams& jparams, j_decompress_ptr cinfo) {
  JXL_CHECK(cinfo->input_scan_number > 0);
  if (cinfo->progressive_mode) {
    JXL_CHECK(cinfo->Ss != 0 || cinfo->Se != 63);
  } else {
    JXL_CHECK(cinfo->Ss == 0 && cinfo->Se == 63);
  }
  if (jparams.progressive_mode > 2) {
    JXL_CHECK(jparams.progressive_mode < 3 + kNumTestScripts);
    const ScanScript& script = kTestScript[jparams.progressive_mode - 3];
    JXL_CHECK(cinfo->input_scan_number <= script.num_scans);
    const jpeg_scan_info& scan = script.scans[cinfo->input_scan_number - 1];
    JXL_CHECK(cinfo->comps_in_scan == scan.comps_in_scan);
    for (int i = 0; i < cinfo->comps_in_scan; ++i) {
      JXL_CHECK(cinfo->cur_comp_info[i]->component_index ==
                scan.component_index[i]);
    }
    JXL_CHECK(cinfo->Ss == scan.Ss);
    JXL_CHECK(cinfo->Se == scan.Se);
    JXL_CHECK(cinfo->Ah == scan.Ah);
    JXL_CHECK(cinfo->Al == scan.Al);
  }
  if (jparams.restart_interval > 0) {
    JXL_CHECK(cinfo->restart_interval == jparams.restart_interval);
  } else if (jparams.restart_in_rows > 0) {
    JXL_CHECK(cinfo->restart_interval ==
              jparams.restart_in_rows * cinfo->MCUs_per_row);
  }
  if (jparams.progressive_mode == 0 && jparams.optimize_coding == 0) {
    if (cinfo->jpeg_color_space == JCS_RGB) {
      JXL_CHECK(cinfo->comp_info[0].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[2].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[0].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[2].ac_tbl_no == 0);
    } else if (cinfo->jpeg_color_space == JCS_YCbCr) {
      JXL_CHECK(cinfo->comp_info[0].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].dc_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[2].dc_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[0].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].ac_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[2].ac_tbl_no == 1);
    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
      JXL_CHECK(cinfo->comp_info[0].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[2].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[3].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[0].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[2].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[3].ac_tbl_no == 0);
    } else if (cinfo->jpeg_color_space == JCS_YCCK) {
      JXL_CHECK(cinfo->comp_info[0].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].dc_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[2].dc_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[3].dc_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[0].ac_tbl_no == 0);
      JXL_CHECK(cinfo->comp_info[1].ac_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[2].ac_tbl_no == 1);
      JXL_CHECK(cinfo->comp_info[3].ac_tbl_no == 0);
    }
    if (jparams.use_flat_dc_luma_code) {
      JHUFF_TBL* tbl = cinfo->dc_huff_tbl_ptrs[0];
      jxl::msan::UnpoisonMemory(tbl, sizeof(*tbl));
      for (int i = 0; i < 15; ++i) {
        JXL_CHECK(tbl->huffval[i] == i);
      }
    }
  }
}

void UnmapColors(uint8_t* row, size_t xsize, int components,
                 JSAMPARRAY colormap, size_t num_colors) {
  JXL_CHECK(colormap != nullptr);
  std::vector<uint8_t> tmp(xsize * components);
  for (size_t x = 0; x < xsize; ++x) {
    JXL_CHECK(row[x] < num_colors);
    for (int c = 0; c < components; ++c) {
      tmp[x * components + c] = colormap[c][row[x]];
    }
  }
  memcpy(row, tmp.data(), tmp.size());
}

void CopyCoefficients(j_decompress_ptr cinfo, jvirt_barray_ptr* coef_arrays,
                      TestImage* output) {
  output->xsize = cinfo->image_width;
  output->ysize = cinfo->image_height;
  output->components = cinfo->num_components;
  output->color_space = cinfo->out_color_space;
  j_common_ptr comptr = reinterpret_cast<j_common_ptr>(cinfo);
  for (int c = 0; c < cinfo->num_components; ++c) {
    jpeg_component_info* comp = &cinfo->comp_info[c];
    std::vector<JCOEF> coeffs(comp->width_in_blocks * comp->height_in_blocks *
                              DCTSIZE2);
    for (size_t by = 0; by < comp->height_in_blocks; ++by) {
      JBLOCKARRAY ba = (*cinfo->mem->access_virt_barray)(comptr, coef_arrays[c],
                                                         by, 1, TRUE);
      size_t stride = comp->width_in_blocks * sizeof(JBLOCK);
      size_t offset = by * comp->width_in_blocks * DCTSIZE2;
      memcpy(&coeffs[offset], ba[0], stride);
    }
    output->coeffs.emplace_back(std::move(coeffs));
  }
}