OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_img_io.h
Go to the documentation of this file.
1 //***************************************************************************/
2 // This software is released under the 2-Clause BSD license, included
3 // below.
4 //
5 // Copyright (c) 2019, Aous Naman
6 // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7 // Copyright (c) 2019, The University of New South Wales, Australia
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //***************************************************************************/
32 // This file is part of the OpenJPH software implementation.
33 // File: ojph_img_io.h
34 // Author: Aous Naman
35 // Date: 28 August 2019
36 //***************************************************************************/
37 
38 
39 #ifndef OJPH_IMG_IO_H
40 #define OJPH_IMG_IO_H
41 
42 #include <cstdio>
43 #include <cassert>
44 
45 #include "ojph_base.h"
46 #include "ojph_defs.h"
47 
48 #ifdef OJPH_ENABLE_TIFF_SUPPORT
49  #include "tiffio.h"
50 #endif /* OJPH_ENABLE_TIFF_SUPPORT */
51 
52 namespace ojph {
53 
55  // defined elsewhere
56  class mem_fixed_allocator;
57  struct line_buf;
58 
60  //
61  //
62  //
63  //
64  //
67  {
68  public:
69  virtual ~image_in_base() {}
70  virtual ui32 read(const line_buf* line, ui32 comp_num) = 0;
71  virtual void close() {}
72  };
73 
75  //
76  //
77  //
78  //
79  //
81  class ppm_in : public image_in_base
82  {
83  public:
85  {
86  fh = 0;
87  fname = NULL;
88  alloc_p = p;
89  temp_buf = NULL;
93 
94  cur_line = 0;
95  start_of_data = 0;
96  planar = false;
97 
98  bit_depth[2] = bit_depth[1] = bit_depth[0] = 0;
99  is_signed[2] = is_signed[1] = is_signed[0] = false;
100  subsampling[2] = subsampling[1] = subsampling[0] = point(1,1);
101  }
102  virtual ~ppm_in()
103  {
104  close();
105  if (alloc_p == NULL && temp_buf)
106  free(temp_buf);
107  }
108 
109  void open(const char* filename);
110  void finalize_alloc();
111  virtual ui32 read(const line_buf* line, ui32 comp_num);
112  void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
113  void set_planar(bool planar) { this->planar = planar; }
114 
115  //size get_size() { assert(fh); return size(width, height); }
116  ui32 get_width() { assert(fh); return width; }
117  ui32 get_height() { assert(fh); return height; }
118  ui32 get_max_val() { assert(fh); return max_val; }
119  ui32 get_num_components() { assert(fh); return num_comps; }
121  { assert(fh && comp_num < num_comps); return bit_depth[comp_num]; }
122  bool get_is_signed(ui32 comp_num)
123  { assert(fh && comp_num < num_comps); return is_signed[comp_num]; }
125  { assert(fh && comp_num < num_comps); return subsampling[comp_num]; }
126 
127  private:
128  FILE *fh;
129  const char *fname;
131  void *temp_buf;
135 
138  int planar;
140  bool is_signed[3];
142  };
143 
145  //
146  //
147  //
148  //
149  //
151 #ifdef OJPH_ENABLE_TIFF_SUPPORT
152  class tif_in : public image_in_base
153  {
154  public:
155  tif_in()
156  {
157  tiff_handle = NULL;
158  fname = NULL;
159  line_buffer = NULL;
160  line_buffer_for_planar_support_uint8 = NULL;
161  line_buffer_for_planar_support_uint16 = NULL;
162 
163  width = height = num_comps = 0;
164  bytes_per_sample = 0;
165 
166  bytes_per_line = 0;
167  planar_configuration = 0;
168 
169  cur_line = 0;
170 
171  bit_depth[3] = bit_depth[2] = bit_depth[1] = bit_depth[0] = 0;
172  is_signed[3] = is_signed[2] = is_signed[1] = is_signed[0] = false;
173  subsampling[3] = subsampling[2] = point(1, 1);
174  subsampling[1] = subsampling[0] = point(1, 1);
175  }
176  virtual ~tif_in()
177  {
178  close();
179  if (line_buffer)
180  free(line_buffer);
181  if (line_buffer_for_planar_support_uint8)
182  free(line_buffer_for_planar_support_uint8);
183  if (line_buffer_for_planar_support_uint16)
184  free(line_buffer_for_planar_support_uint16);
185  }
186 
187  void open(const char* filename);
188  virtual ui32 read(const line_buf* line, ui32 comp_num);
189  void close() {
190  if (tiff_handle) {
191  TIFFClose(tiff_handle);
192  tiff_handle = NULL;
193  }
194  fname = NULL;
195  }
196 
197  size get_size() { assert(tiff_handle); return size(width, height); }
198  ui32 get_num_components() { assert(tiff_handle); return num_comps; }
199  void set_bit_depth(ui32 num_bit_depths, ui32* bit_depth);
200  ui32 get_bit_depth(ui32 comp_num)
201  {
202  assert(tiff_handle && comp_num < num_comps); return bit_depth[comp_num];
203  }
204  bool get_is_signed(ui32 comp_num)
205  {
206  assert(tiff_handle && comp_num < num_comps); return is_signed[comp_num];
207  }
208  point get_comp_subsampling(ui32 comp_num)
209  {
210  assert(tiff_handle && comp_num < num_comps); return subsampling[comp_num];
211  }
212 
213  private:
214  TIFF* tiff_handle;
215  size_t bytes_per_line;
216  ui16 planar_configuration;
217 
218  const char* fname;
219  void* line_buffer;
220  ui8* line_buffer_for_planar_support_uint8;
221  ui16* line_buffer_for_planar_support_uint16;
222  ui32 width, height;
223  ui32 num_comps;
224  ui32 bytes_per_sample;
225  ui32 cur_line;
226  ui32 bit_depth[4];
227  bool is_signed[4];
228  point subsampling[4];
229  };
230 #endif /* OJPH_ENABLE_TIFF_SUPPORT */
231 
233  //
234  //
235  //
236  //
237  //
239  class yuv_in : public image_in_base
240  {
241  public:
243  {
244  fh = NULL;
245  fname = NULL;
246  temp_buf = NULL;
247  for (int i = 0; i < 3; ++i)
248  {
249  width[i] = height[i] = bit_depth[i] = 0;
250  is_signed[i] = false;
251  subsampling[i] = point(1,1);
252  comp_address[i] = 0;
253  bytes_per_sample[i] = 0;
254  }
255  num_com = 0;
256 
257  cur_line = 0;
258  last_comp = 0;
259  planar = false;
260  }
261  virtual ~yuv_in()
262  {
263  close();
264  if (temp_buf)
265  free(temp_buf);
266  }
267 
268  void open(const char* filename);
269  virtual ui32 read(const line_buf* line, ui32 comp_num);
270  void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
271 
272  void set_bit_depth(ui32 num_bit_depths, ui32* bit_depth);
273  void set_img_props(const size& s, ui32 num_components,
274  ui32 num_downsampling, const point *downsampling);
275 
276  ui32 get_num_components() { assert(fh); return num_com; }
277  ui32 *get_bit_depth() { assert(fh); return bit_depth; }
278  bool *get_is_signed() { assert(fh); return is_signed; }
279  point *get_comp_subsampling() { assert(fh); return subsampling; }
280 
281  private:
282  FILE *fh;
283  const char *fname;
284  void *temp_buf;
288 
290  bool planar;
292  bool is_signed[3];
294  };
295 
297  // Accelerators (defined in ojph_img_io_*)
298  typedef void (*conversion_fun)(const line_buf *ln0, const line_buf *ln1,
299  const line_buf *ln2, void *dp,
300  int bit_depth, int count);
301 
302  void gen_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1,
303  const line_buf *ln2, void *dp,
304  int bit_depth, int count);
305  void gen_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1,
306  const line_buf *ln2, void *dp,
307  int bit_depth, int count);
308  void gen_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1,
309  const line_buf *ln2, void *dp,
310  int bit_depth, int count);
311  void gen_cvrt_32b3c_to_16ub3c_le(const line_buf *ln0, const line_buf *ln1,
312  const line_buf *ln2, void *dp,
313  int bit_depth, int count);
314  void gen_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1,
315  const line_buf *ln2, void *dp,
316  int bit_depth, int count);
317  void gen_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1,
318  const line_buf *ln2, void *dp,
319  int bit_depth, int count);
320 
321  void sse41_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1,
322  const line_buf *ln2, void *dp,
323  int bit_depth, int count);
324  void sse41_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1,
325  const line_buf *ln2, void *dp,
326  int bit_depth, int count);
327  void sse41_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1,
328  const line_buf *ln2, void *dp,
329  int bit_depth, int count);
330  void sse41_cvrt_32b3c_to_16ub3c_le(const line_buf *ln0, const line_buf *ln1,
331  const line_buf *ln2, void *dp,
332  int bit_depth, int count);
333  void sse41_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1,
334  const line_buf *ln2, void *dp,
335  int bit_depth, int count);
336  void sse41_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1,
337  const line_buf *ln2, void *dp,
338  int bit_depth, int count);
339 
340  void avx2_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1,
341  const line_buf *ln2, void *dp,
342  int bit_depth, int count);
343  void avx2_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1,
344  const line_buf *ln2, void *dp,
345  int bit_depth, int count);
346  void avx2_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1,
347  const line_buf *ln2, void *dp,
348  int bit_depth, int count);
349  void avx2_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1,
350  const line_buf *ln2, void *dp,
351  int bit_depth, int count);
352 
354  //
355  //
356  //
357  //
358  //
361  {
362  public:
363  virtual ~image_out_base() {}
364  virtual ui32 write(const line_buf* line, ui32 comp_num) = 0;
365  virtual void close() {}
366  };
367 
369  //
370  //
371  //
372  //
373  //
375  class ppm_out : public image_out_base
376  {
377  public:
379  {
380  fh = NULL;
381  fname = NULL;
382  buffer = NULL;
383  width = height = num_components = 0;
385  buffer_size = 0;
387  converter = NULL;
388  lptr[0] = lptr[1] = lptr[2] = 0;
389  }
390  virtual ~ppm_out()
391  {
392  close();
393  if (buffer)
394  free(buffer);
395  }
396 
397  void open(char* filename);
399  ui32 bit_depth);
400  virtual ui32 write(const line_buf* line, ui32 comp_num);
401  virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
402 
403  private:
404  FILE *fh;
405  const char *fname;
412  const line_buf *lptr[3];
413  };
414 
416 //
417 //
418 //
419 //
420 //
422 #ifdef OJPH_ENABLE_TIFF_SUPPORT
423  class tif_out : public image_out_base
424  {
425  public:
426  tif_out()
427  {
428  tiff_handle = NULL;
429  fname = NULL;
430  buffer = NULL;
431  width = height = num_components = 0;
432  bytes_per_sample = 0;
433  bit_depth_of_data[0] = bit_depth_of_data[1] = 0;
434  bit_depth_of_data[2] = bit_depth_of_data[3] = 0;
435  buffer_size = 0;
436  cur_line = samples_per_line = 0;
437  bytes_per_line = 0;
438 
439  planar_configuration = 0;
440  }
441  virtual ~tif_out()
442  {
443  close();
444  if (buffer)
445  free(buffer);
446  }
447 
448  void open(char* filename);
449  void configure(ui32 width, ui32 height, ui32 num_components,
450  ui32 *bit_depth);
451  virtual ui32 write(const line_buf* line, ui32 comp_num);
452  virtual void close() {
453  if (tiff_handle) {
454  TIFFClose(tiff_handle);
455  tiff_handle = NULL;
456  }
457  fname = NULL;
458  }
459 
460  private:
461  TIFF* tiff_handle;
462  size_t bytes_per_line;
463  unsigned short planar_configuration;
464 
465  const char* fname;
466  ui32 width, height, num_components;
467  ui32 bit_depth_of_data[4];
468  ui32 bytes_per_sample;
469  ui8* buffer;
470  ui32 buffer_size;
471  ui32 cur_line, samples_per_line;
472  };
473 #endif /* OJPH_ENABLE_TIFF_SUPPORT */
474 
475 
477  //
478  //
479  //
480  //
481  //
483  class yuv_out : public image_out_base
484  {
485  public:
487  {
488  fh = NULL;
489  fname = NULL;
490  width = num_components = 0;
491  bit_depth = 0;
492  comp_width = NULL;
493  buffer = NULL;
494  buffer_size = 0;
495  }
496  virtual ~yuv_out();
497 
498  void open(char* filename);
500  virtual ui32 write(const line_buf* line, ui32 comp_num);
501  virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
502 
503  private:
504  FILE *fh;
505  const char *fname;
512  };
513 
514 
515 }
516 
517 #endif // !OJPH_IMG_IO_H
virtual void close()
Definition: ojph_img_io.h:71
virtual ui32 read(const line_buf *line, ui32 comp_num)=0
virtual ~image_in_base()
Definition: ojph_img_io.h:69
virtual ui32 write(const line_buf *line, ui32 comp_num)=0
virtual void close()
Definition: ojph_img_io.h:365
virtual ~image_out_base()
Definition: ojph_img_io.h:363
point subsampling[3]
Definition: ojph_img_io.h:141
ui32 get_height()
Definition: ojph_img_io.h:117
void open(const char *filename)
ui32 get_num_components()
Definition: ojph_img_io.h:119
void set_planar(bool planar)
Definition: ojph_img_io.h:113
ui32 num_ele_per_line
Definition: ojph_img_io.h:133
ui32 bytes_per_sample
Definition: ojph_img_io.h:133
ui32 get_width()
Definition: ojph_img_io.h:116
ui32 max_val_num_bits
Definition: ojph_img_io.h:132
ui32 get_bit_depth(ui32 comp_num)
Definition: ojph_img_io.h:120
const char * fname
Definition: ojph_img_io.h:129
void finalize_alloc()
ui32 temp_buf_byte_size
Definition: ojph_img_io.h:134
ppm_in(mem_fixed_allocator *p=NULL)
Definition: ojph_img_io.h:84
ui32 get_max_val()
Definition: ojph_img_io.h:118
void * temp_buf
Definition: ojph_img_io.h:131
virtual ~ppm_in()
Definition: ojph_img_io.h:102
void close()
Definition: ojph_img_io.h:112
point get_comp_subsampling(ui32 comp_num)
Definition: ojph_img_io.h:124
mem_fixed_allocator * alloc_p
Definition: ojph_img_io.h:130
bool get_is_signed(ui32 comp_num)
Definition: ojph_img_io.h:122
bool is_signed[3]
Definition: ojph_img_io.h:140
si64 start_of_data
Definition: ojph_img_io.h:137
ui32 bit_depth[3]
Definition: ojph_img_io.h:139
virtual ui32 read(const line_buf *line, ui32 comp_num)
ui32 num_components
Definition: ojph_img_io.h:406
const char * fname
Definition: ojph_img_io.h:405
virtual void close()
Definition: ojph_img_io.h:401
void open(char *filename)
virtual ui32 write(const line_buf *line, ui32 comp_num)
ui32 bytes_per_line
Definition: ojph_img_io.h:410
ui32 bytes_per_sample
Definition: ojph_img_io.h:407
const line_buf * lptr[3]
Definition: ojph_img_io.h:412
virtual ~ppm_out()
Definition: ojph_img_io.h:390
conversion_fun converter
Definition: ojph_img_io.h:411
void configure(ui32 width, ui32 height, ui32 num_components, ui32 bit_depth)
ui32 samples_per_line
Definition: ojph_img_io.h:410
ui32 width[3]
Definition: ojph_img_io.h:285
virtual ui32 read(const line_buf *line, ui32 comp_num)
ui32 height[3]
Definition: ojph_img_io.h:285
void open(const char *filename)
bool is_signed[3]
Definition: ojph_img_io.h:292
void * temp_buf
Definition: ojph_img_io.h:284
const char * fname
Definition: ojph_img_io.h:283
ui32 * get_bit_depth()
Definition: ojph_img_io.h:277
void set_img_props(const size &s, ui32 num_components, ui32 num_downsampling, const point *downsampling)
void set_bit_depth(ui32 num_bit_depths, ui32 *bit_depth)
virtual ~yuv_in()
Definition: ojph_img_io.h:261
ui32 bytes_per_sample[3]
Definition: ojph_img_io.h:286
point * get_comp_subsampling()
Definition: ojph_img_io.h:279
point subsampling[3]
Definition: ojph_img_io.h:293
void close()
Definition: ojph_img_io.h:270
ui32 get_num_components()
Definition: ojph_img_io.h:276
ui32 bit_depth[3]
Definition: ojph_img_io.h:291
ui32 comp_address[3]
Definition: ojph_img_io.h:287
bool * get_is_signed()
Definition: ojph_img_io.h:278
const char * fname
Definition: ojph_img_io.h:505
void open(char *filename)
ui32 * comp_width
Definition: ojph_img_io.h:509
ui32 num_components
Definition: ojph_img_io.h:507
void configure(ui32 bit_depth, ui32 num_components, ui32 *comp_width)
virtual void close()
Definition: ojph_img_io.h:501
virtual ~yuv_out()
virtual ui32 write(const line_buf *line, ui32 comp_num)
void gen_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void avx2_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void sse41_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void gen_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
Definition: ojph_img_io.cpp:89
int64_t si64
Definition: ojph_defs.h:57
void sse41_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void avx2_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
uint16_t ui16
Definition: ojph_defs.h:52
void sse41_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void gen_cvrt_32b3c_to_16ub3c_le(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void gen_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void sse41_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void gen_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void sse41_cvrt_32b3c_to_16ub3c_le(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void(* conversion_fun)(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
Definition: ojph_img_io.h:298
void avx2_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void sse41_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50
void avx2_cvrt_32b1c_to_16ub1c_le(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)
void gen_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, int bit_depth, int count)