Imported Upstream version 0.9.0
[platform/upstream/libjxl.git] / lib / jpegli / common.cc
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 #include "lib/jpegli/common.h"
7
8 #include "lib/jpegli/decode_internal.h"
9 #include "lib/jpegli/encode_internal.h"
10 #include "lib/jpegli/memory_manager.h"
11
12 void jpegli_abort(j_common_ptr cinfo) {
13   if (cinfo->mem == nullptr) return;
14   for (int pool_id = 0; pool_id < JPOOL_NUMPOOLS; ++pool_id) {
15     if (pool_id == JPOOL_PERMANENT) continue;
16     (*cinfo->mem->free_pool)(cinfo, pool_id);
17   }
18   if (cinfo->is_decompressor) {
19     cinfo->global_state = jpegli::kDecStart;
20   } else {
21     cinfo->global_state = jpegli::kEncStart;
22   }
23 }
24
25 void jpegli_destroy(j_common_ptr cinfo) {
26   if (cinfo->mem == nullptr) return;
27   (*cinfo->mem->self_destruct)(cinfo);
28   if (cinfo->is_decompressor) {
29     cinfo->global_state = jpegli::kDecNull;
30     delete reinterpret_cast<j_decompress_ptr>(cinfo)->master;
31   } else {
32     cinfo->global_state = jpegli::kEncNull;
33   }
34 }
35
36 JQUANT_TBL* jpegli_alloc_quant_table(j_common_ptr cinfo) {
37   JQUANT_TBL* table = jpegli::Allocate<JQUANT_TBL>(cinfo, 1);
38   table->sent_table = FALSE;
39   return table;
40 }
41
42 JHUFF_TBL* jpegli_alloc_huff_table(j_common_ptr cinfo) {
43   JHUFF_TBL* table = jpegli::Allocate<JHUFF_TBL>(cinfo, 1);
44   table->sent_table = FALSE;
45   return table;
46 }
47
48 int jpegli_bytes_per_sample(JpegliDataType data_type) {
49   switch (data_type) {
50     case JPEGLI_TYPE_UINT8:
51       return 1;
52     case JPEGLI_TYPE_UINT16:
53       return 2;
54     case JPEGLI_TYPE_FLOAT:
55       return 4;
56     default:
57       return 0;
58   }
59 }