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  //
298  //
299  //
300  //
301  //
304  {
305  public:
306  virtual ~image_out_base() {}
307  virtual ui32 write(const line_buf* line, ui32 comp_num) = 0;
308  virtual void close() {}
309  };
310 
312  //
313  //
314  //
315  //
316  //
318  class ppm_out : public image_out_base
319  {
320  public:
322  {
323  fh = NULL;
324  fname = NULL;
325  buffer = NULL;
326  width = height = num_components = 0;
328  buffer_size = 0;
330  }
331  virtual ~ppm_out()
332  {
333  close();
334  if (buffer)
335  free(buffer);
336  }
337 
338  void open(char* filename);
340  ui32 bit_depth);
341  virtual ui32 write(const line_buf* line, ui32 comp_num);
342  virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
343 
344  private:
345  FILE *fh;
346  const char *fname;
352  };
353 
355 //
356 //
357 //
358 //
359 //
361 #ifdef OJPH_ENABLE_TIFF_SUPPORT
362  class tif_out : public image_out_base
363  {
364  public:
365  tif_out()
366  {
367  tiff_handle = NULL;
368  fname = NULL;
369  buffer = NULL;
370  width = height = num_components = 0;
371  bytes_per_sample = 0;
372  bit_depth_of_data[0] = bit_depth_of_data[1] = bit_depth_of_data[2] = bit_depth_of_data[3] = 0;
373  buffer_size = 0;
374  cur_line = samples_per_line = 0;
375  bytes_per_line = 0;
376 
377  planar_configuration = 0;
378  }
379  virtual ~tif_out()
380  {
381  close();
382  if (buffer)
383  free(buffer);
384  }
385 
386  void open(char* filename);
387  void configure(ui32 width, ui32 height, ui32 num_components,
388  ui32 *bit_depth);
389  virtual ui32 write(const line_buf* line, ui32 comp_num);
390  virtual void close() {
391  if (tiff_handle) {
392  TIFFClose(tiff_handle);
393  tiff_handle = NULL;
394  }
395  fname = NULL;
396  }
397 
398  private:
399  TIFF* tiff_handle;
400  size_t bytes_per_line;
401  unsigned short planar_configuration;
402 
403  const char* fname;
404  ui32 width, height, num_components;
405  ui32 bit_depth_of_data[4];
406  ui32 bytes_per_sample;
407  ui8* buffer;
408  ui32 buffer_size;
409  ui32 cur_line, samples_per_line;
410  };
411 #endif /* OJPH_ENABLE_TIFF_SUPPORT */
412 
413 
415  //
416  //
417  //
418  //
419  //
421  class yuv_out : public image_out_base
422  {
423  public:
425  {
426  fh = NULL;
427  fname = NULL;
428  width = num_components = 0;
429  bit_depth = 0;
430  comp_width = NULL;
431  buffer = NULL;
432  buffer_size = 0;
433  }
434  virtual ~yuv_out();
435 
436  void open(char* filename);
438  virtual ui32 write(const line_buf* line, ui32 comp_num);
439  virtual void close() { if(fh) { fclose(fh); fh = NULL; } fname = NULL; }
440 
441  private:
442  FILE *fh;
443  const char *fname;
450  };
451 
452 
453 
454 
455 }
456 
457 #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:308
virtual ~image_out_base()
Definition: ojph_img_io.h:306
point subsampling[3]
Definition: ojph_img_io.h:141
ui32 get_height()
Definition: ojph_img_io.h:117
void open(const char *filename)
Definition: ojph_img_io.cpp:90
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:347
const char * fname
Definition: ojph_img_io.h:346
virtual void close()
Definition: ojph_img_io.h:342
void open(char *filename)
virtual ui32 write(const line_buf *line, ui32 comp_num)
ui32 bytes_per_line
Definition: ojph_img_io.h:351
ui32 bytes_per_sample
Definition: ojph_img_io.h:348
virtual ~ppm_out()
Definition: ojph_img_io.h:331
void configure(ui32 width, ui32 height, ui32 num_components, ui32 bit_depth)
ui32 samples_per_line
Definition: ojph_img_io.h:351
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:443
void open(char *filename)
ui32 * comp_width
Definition: ojph_img_io.h:447
ui32 num_components
Definition: ojph_img_io.h:445
void configure(ui32 bit_depth, ui32 num_components, ui32 *comp_width)
virtual void close()
Definition: ojph_img_io.h:439
virtual ~yuv_out()
virtual ui32 write(const line_buf *line, ui32 comp_num)
int64_t si64
Definition: ojph_defs.h:57
uint16_t ui16
Definition: ojph_defs.h:52
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50