Grok  7.6.0
FileFormat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2020 Grok Image Compression Inc.
3  *
4  * This source code is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This source code is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  *
17  * This source code incorporates work covered by the BSD 2-clause license.
18  * Please see the LICENSE file in the root directory for details.
19  *
20  */
21 
22 
23 #pragma once
24 namespace grk {
25 
34 
35 #define JP2_JP 0x6a502020
36 #define JP2_FTYP 0x66747970
37 #define JP2_JP2H 0x6a703268
38 #define JP2_IHDR 0x69686472
39 #define JP2_COLR 0x636f6c72
40 #define JP2_JP2C 0x6a703263
41 #define JP2_PCLR 0x70636c72
42 #define JP2_CMAP 0x636d6170
43 #define JP2_CDEF 0x63646566
44 #define JP2_DTBL 0x6474626c
45 #define JP2_BPCC 0x62706363
46 #define JP2_JP2 0x6a703220
48 #define JP2_RES 0x72657320
49 #define JP2_CAPTURE_RES 0x72657363
50 #define JP2_DISPLAY_RES 0x72657364
52 #define JP2_JP2I 0x6a703269
53 #define JP2_XML 0x786d6c20
54 #define JP2_UUID 0x75756964
55 #define JP2_UINF 0x75696e66
56 #define JP2_ULST 0x756c7374
57 #define JP2_URL 0x75726c20
59 /* ----------------------------------------------------------------------- */
60 
61 #define JP2_MAX_NUM_UUIDS 128
62 
63 const uint8_t IPTC_UUID[16] = { 0x33, 0xC7, 0xA4, 0xD2, 0xB8, 0x1D, 0x47, 0x23,
64  0xA0, 0xBA, 0xF1, 0xA3, 0xE0, 0x97, 0xAD, 0x38 };
65 const uint8_t XMP_UUID[16] = { 0xBE, 0x7A, 0xCF, 0xCB, 0x97, 0xA9, 0x42, 0xE8,
66  0x9C, 0x71, 0x99, 0x94, 0x91, 0xE3, 0xAF, 0xAC };
67 
68 enum JP2_STATE {
75  JP2_STATE_UNKNOWN = 0x7fffffff /* ISO C restricts enumerator values to range of 'int' */
76 };
77 
80 };
81 
85 struct grk_jp2_comps {
86  uint32_t depth;
87  uint32_t sgnd;
88  uint8_t bpcc;
89 };
90 
92  grk_jp2_buffer(uint8_t *buf, size_t size, bool owns) :
93  buffer(buf), len(size), ownsData(owns) {
94  }
96  grk_jp2_buffer(nullptr, 0, false) {
97  }
98  void alloc(size_t length) {
99  dealloc();
100  buffer = new uint8_t[length];
101  len = length;
102  ownsData = true;
103  }
104  void dealloc() {
105  if (ownsData)
106  delete[] buffer;
107  buffer = nullptr;
108  ownsData = false;
109  len = 0;
110  }
111  uint8_t *buffer;
112  size_t len;
113  bool ownsData;
114 };
115 
116 struct grk_jp2_uuid: public grk_jp2_buffer {
118  grk_jp2_uuid(const uint8_t myuuid[16], uint8_t *buf, size_t size, bool owns) :
119  grk_jp2_buffer(buf, size, owns) {
120  for (int i = 0; i < 16; ++i)
121  uuid[i] = myuuid[i];
122  }
123  uint8_t uuid[16];
124 };
125 
126 struct FileFormat;
127 typedef bool (*jp2_procedure)(FileFormat *fileFormat, BufferedStream*);
128 
132 struct FileFormat {
133  FileFormat(bool isDecoder);
135 
136  bool decompress_tile(BufferedStream *stream, grk_image *p_image,
137  uint16_t tile_index);
138 
139 
140 
144  std::vector<jp2_procedure> *m_validation_list;
146  std::vector<jp2_procedure> *m_procedure_list;
147 
148  /* width of image */
149  uint32_t w;
150  /* height of image */
151  uint32_t h;
152  /* number of components in the image */
153  uint32_t numcomps;
154  uint32_t bpc;
155  uint32_t C;
156  uint32_t UnkC;
157  uint32_t IPR;
158  uint32_t meth;
159  uint32_t approx;
161  uint32_t precedence;
162  uint32_t brand;
163  uint32_t minversion;
164  uint32_t numcl;
165  uint32_t *cl;
169  uint32_t jp2_state;
170  uint32_t jp2_img_state;
172 
175 
178 
181  uint32_t numUuids;
182 };
183 
187 struct grk_jp2_box {
188  uint64_t length;
189  uint32_t type;
190 };
191 
193  /* marker value */
194  uint32_t id;
195  /* action linked to the marker */
196  bool (*handler)(FileFormat *fileFormat, uint8_t *p_header_data, uint32_t header_size);
197 };
198 
200  /* action to perform */
201  uint8_t* (*handler)(FileFormat *fileFormat, uint32_t *data_size);
202  /* result of the action : data */
203  uint8_t *m_data;
204  /* size of data */
205  uint32_t m_size;
206 };
207 
210 /* ----------------------------------------------------------------------- */
211 
212 
217 void jp2_destroy(FileFormat *fileFormat);
218 
219 
230 bool jp2_init_compress(FileFormat *fileFormat, grk_cparameters *parameters,
231  grk_image *image);
232 
239 bool jp2_start_compress(FileFormat *fileFormat, BufferedStream *stream);
240 
250 bool jp2_compress(FileFormat *fileFormat, grk_plugin_tile *tile, BufferedStream *stream);
251 
252 
264 bool jp2_compress_tile(FileFormat *fileFormat, TileProcessor *tileProcessor,
265  uint16_t tile_index, uint8_t *p_data,
266  uint64_t uncompressed_data_size, BufferedStream *stream);
267 
272 bool jp2_end_compress(FileFormat *fileFormat, BufferedStream *stream);
273 
274 /* ----------------------------------------------------------------------- */
275 
283 void jp2_init_decompress(void *fileFormat, grk_dparameters *parameters);
284 
295 bool jp2_decompress(FileFormat *fileFormat, grk_plugin_tile *tile, BufferedStream *stream,
296  grk_image *p_image);
297 
302 bool jp2_end_decompress(FileFormat *fileFormat, BufferedStream *stream);
303 
313 bool jp2_read_header(BufferedStream *stream, FileFormat *fileFormat,
314  grk_header_info *header_info, grk_image **p_image);
315 
326 bool jp2_read_tile_header(FileFormat *fileFormat, uint16_t *tile_index,
327  bool *can_decode_tile_data, BufferedStream *stream);
328 
329 
343 bool jp2_set_decompress_area(FileFormat *fileFormat, grk_image *image, uint32_t start_x,
344  uint32_t start_y, uint32_t end_x, uint32_t end_y);
345 
349 bool jp2_decompress_tile(FileFormat *fileFormat, BufferedStream *stream, grk_image *p_image, uint16_t tile_index);
350 
351 
355 
356 }
357 
grk::jp2_read_header
bool jp2_read_header(BufferedStream *stream, FileFormat *fileFormat, grk_header_info *header_info, grk_image **p_image)
Read a JPEG 2000 file header.
grk::FileFormat::w
uint32_t w
Definition: FileFormat.h:149
grk::FileFormat::enumcs
GRK_ENUM_COLOUR_SPACE enumcs
Definition: FileFormat.h:160
grk::FileFormat::jp2_img_state
uint32_t jp2_img_state
Definition: FileFormat.h:170
grk::jp2_destroy
void jp2_destroy(FileFormat *fileFormat)
Destroy a JP2 decompressor handle.
grk::grk_jp2_uuid::uuid
uint8_t uuid[16]
Definition: FileFormat.h:123
grk::IPTC_UUID
const uint8_t IPTC_UUID[16]
Definition: FileFormat.h:63
grk::FileFormat::capture_resolution
double capture_resolution[2]
Definition: FileFormat.h:174
grk::FileFormat::brand
uint32_t brand
Definition: FileFormat.h:162
grk::FileFormat::~FileFormat
~FileFormat()
grk::jp2_end_compress
bool jp2_end_compress(FileFormat *fileFormat, BufferedStream *stream)
Ends the compression procedures and possibly add data to be read after the code stream.
grk::TileProcessor
Tile coder/decoder.
Definition: TileProcessor.h:211
grk::grk_jp2_img_header_writer_handler::m_data
uint8_t * m_data
Definition: FileFormat.h:203
grk::grk_jp2_header_handler
Definition: FileFormat.h:192
grk::FileFormat::numcomps
uint32_t numcomps
Definition: FileFormat.h:153
grk::jp2_procedure
bool(* jp2_procedure)(FileFormat *fileFormat, BufferedStream *)
Definition: FileFormat.h:127
grk::FileFormat::IPR
uint32_t IPR
Definition: FileFormat.h:157
grk::BufferedStream
Byte input-output stream.
Definition: BufferedStream.h:38
grk::JP2_STATE_CODESTREAM
@ JP2_STATE_CODESTREAM
Definition: FileFormat.h:73
grk::FileFormat::meth
uint32_t meth
Definition: FileFormat.h:158
grk::JP2_STATE_SIGNATURE
@ JP2_STATE_SIGNATURE
Definition: FileFormat.h:70
grk::FileFormat::xml
grk_jp2_buffer xml
Definition: FileFormat.h:179
grk::grk_jp2_buffer::grk_jp2_buffer
grk_jp2_buffer()
Definition: FileFormat.h:95
grk::FileFormat
JPEG 2000 file format reader/writer.
Definition: FileFormat.h:132
grk::jp2_set_decompress_area
bool jp2_set_decompress_area(FileFormat *fileFormat, grk_image *image, uint32_t start_x, uint32_t start_y, uint32_t end_x, uint32_t end_y)
Sets the given area to be decompressed.
_grk_plugin_tile
Plugin tile.
Definition: grok.h:1242
grk::grk_jp2_buffer
Definition: FileFormat.h:91
grk::jp2_end_decompress
bool jp2_end_decompress(FileFormat *fileFormat, BufferedStream *stream)
Ends the decompression procedures and possibly add data to be read after the code stream.
grk::grk_jp2_uuid
Definition: FileFormat.h:116
grk::jp2_start_compress
bool jp2_start_compress(FileFormat *fileFormat, BufferedStream *stream)
Starts a compression scheme, i.e.
grk::JP2_STATE_UNKNOWN
@ JP2_STATE_UNKNOWN
Definition: FileFormat.h:75
grk::JP2_STATE_END_CODESTREAM
@ JP2_STATE_END_CODESTREAM
Definition: FileFormat.h:74
grk::FileFormat::m_procedure_list
std::vector< jp2_procedure > * m_procedure_list
list of execution procedures
Definition: FileFormat.h:146
grk::grk_jp2_box
JP2 Box.
Definition: FileFormat.h:187
grk::jp2_init_decompress
void jp2_init_decompress(void *fileFormat, grk_dparameters *parameters)
Set up the decompress parameters using user parameters.
grk::FileFormat::codeStream
CodeStream * codeStream
handle to the J2K codec
Definition: FileFormat.h:142
grk::grk_jp2_img_header_writer_handler
Definition: FileFormat.h:199
grk::jp2_init_compress
bool jp2_init_compress(FileFormat *fileFormat, grk_cparameters *parameters, grk_image *image)
Set up compress parameters using the current image and using user parameters.
grk::JP2_STATE_HEADER
@ JP2_STATE_HEADER
Definition: FileFormat.h:72
grk::FileFormat::UnkC
uint32_t UnkC
Definition: FileFormat.h:156
grk::FileFormat::color
grk_jp2_color color
Definition: FileFormat.h:171
grk::FileFormat::C
uint32_t C
Definition: FileFormat.h:155
grk::FileFormat::uuids
grk_jp2_uuid uuids[JP2_MAX_NUM_UUIDS]
Definition: FileFormat.h:180
grk::grk_jp2_buffer::ownsData
bool ownsData
Definition: FileFormat.h:113
grk::FileFormat::display_resolution
double display_resolution[2]
Definition: FileFormat.h:177
grk::FileFormat::needs_xl_jp2c_box_length
bool needs_xl_jp2c_box_length
Definition: FileFormat.h:168
grk::FileFormat::cl
uint32_t * cl
Definition: FileFormat.h:165
grk::FileFormat::approx
uint32_t approx
Definition: FileFormat.h:159
grk::CodeStream
Definition: CodeStream.h:130
grk::grk_jp2_comps
JP2 component.
Definition: FileFormat.h:85
grk::FileFormat::jp2_state
uint32_t jp2_state
Definition: FileFormat.h:169
grk::grk_jp2_comps::sgnd
uint32_t sgnd
Definition: FileFormat.h:87
grk::FileFormat::has_display_resolution
bool has_display_resolution
Definition: FileFormat.h:176
_grk_dparameters
Core decompress parameters.
Definition: grok.h:669
grk::grk_jp2_comps::bpcc
uint8_t bpcc
Definition: FileFormat.h:88
grk::JP2_IMG_STATE_NONE
@ JP2_IMG_STATE_NONE
Definition: FileFormat.h:79
grk::grk_jp2_uuid::grk_jp2_uuid
grk_jp2_uuid(const uint8_t myuuid[16], uint8_t *buf, size_t size, bool owns)
Definition: FileFormat.h:118
grk::FileFormat::precedence
uint32_t precedence
Definition: FileFormat.h:161
grk::grk_jp2_buffer::len
size_t len
Definition: FileFormat.h:112
grk::FileFormat::bpc
uint32_t bpc
Definition: FileFormat.h:154
grk
Copyright (C) 2016-2020 Grok Image Compression Inc.
Definition: BitIO.h:27
grk::FileFormat::m_validation_list
std::vector< jp2_procedure > * m_validation_list
list of validation procedures
Definition: FileFormat.h:144
grk::jp2_compress_tile
bool jp2_compress_tile(FileFormat *fileFormat, TileProcessor *tileProcessor, uint16_t tile_index, uint8_t *p_data, uint64_t uncompressed_data_size, BufferedStream *stream)
Compress tile.
grk::grk_jp2_box::length
uint64_t length
Definition: FileFormat.h:188
grk::grk_jp2_img_header_writer_handler::m_size
uint32_t m_size
Definition: FileFormat.h:205
grk::JP2_STATE_NONE
@ JP2_STATE_NONE
Definition: FileFormat.h:69
_grk_header_info
Header info.
Definition: grok.h:603
grk::FileFormat::minversion
uint32_t minversion
Definition: FileFormat.h:163
grk::grk_jp2_buffer::grk_jp2_buffer
grk_jp2_buffer(uint8_t *buf, size_t size, bool owns)
Definition: FileFormat.h:92
grk::grk_jp2_buffer::alloc
void alloc(size_t length)
Definition: FileFormat.h:98
grk::grk_jp2_buffer::buffer
uint8_t * buffer
Definition: FileFormat.h:111
grk::XMP_UUID
const uint8_t XMP_UUID[16]
Definition: FileFormat.h:65
grk::FileFormat::comps
grk_jp2_comps * comps
Definition: FileFormat.h:166
grk::FileFormat::decompress_tile
bool decompress_tile(BufferedStream *stream, grk_image *p_image, uint16_t tile_index)
grk::JP2_IMG_STATE_UNKNOWN
@ JP2_IMG_STATE_UNKNOWN
Definition: FileFormat.h:79
grk::FileFormat::j2k_codestream_offset
uint64_t j2k_codestream_offset
Definition: FileFormat.h:167
grk::jp2_decompress
bool jp2_decompress(FileFormat *fileFormat, grk_plugin_tile *tile, BufferedStream *stream, grk_image *p_image)
Decompress an image from a JPEG 2000 file stream.
grk::FileFormat::FileFormat
FileFormat(bool isDecoder)
grk::FileFormat::numcl
uint32_t numcl
Definition: FileFormat.h:164
grk::grk_jp2_uuid::grk_jp2_uuid
grk_jp2_uuid()
Definition: FileFormat.h:117
grk::JP2_STATE
JP2_STATE
Definition: FileFormat.h:68
grk::grk_jp2_header_handler::handler
bool(* handler)(FileFormat *fileFormat, uint8_t *p_header_data, uint32_t header_size)
Definition: FileFormat.h:196
JP2_MAX_NUM_UUIDS
#define JP2_MAX_NUM_UUIDS
Definition: FileFormat.h:61
grk::grk_jp2_buffer::dealloc
void dealloc()
Definition: FileFormat.h:104
grk::jp2_compress
bool jp2_compress(FileFormat *fileFormat, grk_plugin_tile *tile, BufferedStream *stream)
Encode an image into a JPEG 2000 file stream.
_grk_jp2_color
ICC profile, palette, component mapping, channel description.
Definition: grok.h:592
grk::grk_jp2_box::type
uint32_t type
Definition: FileFormat.h:189
_grk_image
Image.
Definition: grok.h:880
_grk_cparameters
Compress parameters.
Definition: grok.h:428
grk::jp2_read_tile_header
bool jp2_read_tile_header(FileFormat *fileFormat, uint16_t *tile_index, bool *can_decode_tile_data, BufferedStream *stream)
Reads a tile header.
grk::grk_jp2_header_handler::id
uint32_t id
Definition: FileFormat.h:194
grk::FileFormat::h
uint32_t h
Definition: FileFormat.h:151
grk::grk_jp2_comps::depth
uint32_t depth
Definition: FileFormat.h:86
grk::JP2_IMG_STATE
JP2_IMG_STATE
Definition: FileFormat.h:78
grk::JP2_STATE_FILE_TYPE
@ JP2_STATE_FILE_TYPE
Definition: FileFormat.h:71
GRK_ENUM_COLOUR_SPACE
GRK_ENUM_COLOUR_SPACE
Definition: grok.h:329
grk::FileFormat::numUuids
uint32_t numUuids
Definition: FileFormat.h:181
grk::FileFormat::has_capture_resolution
bool has_capture_resolution
Definition: FileFormat.h:173
grk::jp2_decompress_tile
bool jp2_decompress_tile(FileFormat *fileFormat, BufferedStream *stream, grk_image *p_image, uint16_t tile_index)