OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_wrapper.cpp
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_wrapper.cpp
34 // Author: Aous Naman
35 // Date: 22 October 2019
36 /****************************************************************************/
37 
38 #include <exception>
39 #include <emscripten.h>
40 
41 #include "ojph_arch.h"
42 #include "ojph_file.h"
43 #include "ojph_mem.h"
44 #include "ojph_params.h"
45 #include "ojph_codestream.h"
46 
48 struct j2k_struct
49 {
52 };
53 
56 {
57  return new j2k_struct;
58 }
59 
61 void cpp_init_j2c_data(j2k_struct *j2c, const uint8_t *data, size_t size)
62 {
63  try {
64  j2c->mem_file.open(data, size);
65  j2c->codestream.read_headers(&j2c->mem_file);
66  }
67  catch (const std::exception& e)
68  {
69  const char *p = e.what();
70  if (strncmp(p, "ojph error", 10) != 0)
71  printf("%s\n", p);
72  }
73 }
74 
77 {
78  try {
79  j2c->codestream.set_planar(false);
80  j2c->codestream.create();
81  }
82  catch (const std::exception& e)
83  {
84  const char *p = e.what();
85  if (strncmp(p, "ojph error", 10) != 0)
86  printf("%s\n", p);
87  }
88 }
89 
92 {
93  if (j2c)
94  {
95  delete j2c;
96  }
97 }
98 
100 signed int* cpp_pull_j2c_line(j2k_struct* j2c)
101 {
102  try {
103  ojph::ui32 comp_num;
104  ojph::line_buf *line = j2c->codestream.pull(comp_num);
105  return line->i32;
106  }
107  catch (const std::exception& e)
108  {
109  const char *p = e.what();
110  if (strncmp(p, "ojph error", 10) != 0)
111  printf("%s\n", p);
112  }
113  return NULL;
114 }
115 
118  int skipped_res_for_read,
119  int skipped_res_for_recon)
120 {
121  j2c->codestream.restrict_input_resolution(skipped_res_for_read,
122  skipped_res_for_recon);
123 }
124 
126 extern "C"
127 {
129  EMSCRIPTEN_KEEPALIVE
131  {
132  return cpp_create_j2c_data();
133  }
134 
136  EMSCRIPTEN_KEEPALIVE
137  void init_j2c_data(j2k_struct *j2c, const uint8_t *data, size_t size)
138  {
139  cpp_init_j2c_data(j2c, data, size);
140  }
141 
143  EMSCRIPTEN_KEEPALIVE
144  int get_j2c_width(j2k_struct* j2c, int comp_num)
145  {
147  return siz.get_recon_width(comp_num);
148  }
149 
151  EMSCRIPTEN_KEEPALIVE
152  int get_j2c_height(j2k_struct* j2c, int comp_num)
153  {
155  return siz.get_recon_height(comp_num);
156  }
157 
159  EMSCRIPTEN_KEEPALIVE
160  int get_j2c_bit_depth(j2k_struct* j2c, int comp_num)
161  {
163  if (comp_num >= 0 && comp_num < siz.get_num_components())
164  return siz.get_bit_depth(comp_num);
165  else
166  return -1;
167  }
168 
170  EMSCRIPTEN_KEEPALIVE
171  int get_j2c_is_signed(j2k_struct* j2c, int comp_num)
172  {
174  if (comp_num >= 0 && comp_num < siz.get_num_components())
175  return siz.is_signed(comp_num) ? 1 : 0;
176  else
177  return -1;
178  }
179 
181  EMSCRIPTEN_KEEPALIVE
183  {
185  return siz.get_num_components();
186  }
187 
189  EMSCRIPTEN_KEEPALIVE
190  int get_j2c_downsampling_x(j2k_struct* j2c, int comp_num)
191  {
193  if (comp_num >= 0 && comp_num < siz.get_num_components())
194  { return siz.get_downsampling(comp_num).x; }
195  else
196  { return -1; }
197  }
198 
200  EMSCRIPTEN_KEEPALIVE
201  int get_j2c_downsampling_y(j2k_struct* j2c, int comp_num)
202  {
204  if (comp_num >= 0 && comp_num < siz.get_num_components())
205  { return siz.get_downsampling(comp_num).y; }
206  else
207  { return -1; }
208  }
209 
211  EMSCRIPTEN_KEEPALIVE
213  {
214  cpp_parse_j2c_data(j2c);
215  }
216 
218  EMSCRIPTEN_KEEPALIVE
220  int skipped_res_for_read,
221  int skipped_res_for_recon)
222  {
223  cpp_restrict_input_resolution(j2c, skipped_res_for_read,
224  skipped_res_for_recon);
225  }
226 
228  EMSCRIPTEN_KEEPALIVE
230  {
232  }
233 
235  EMSCRIPTEN_KEEPALIVE
236  signed int* pull_j2c_line(j2k_struct* j2c)
237  {
238  return cpp_pull_j2c_line(j2c);
239  }
240 
242  EMSCRIPTEN_KEEPALIVE
244  {
246  }
247 }
248 
249 #ifdef BUILD_TEST
250 #include <cstdio>
251 int main(int argc, const char* argv[])
252 {
253  const char filename[] = "test.j2c";
254  FILE *f = fopen(filename, "rb");
255  fseek(f, 0, SEEK_END);
256  long int size = ftell(f);
257  ojph::ui8 *compressed_data = (ojph::ui8*)malloc(size);
258  fseek(f, 0, SEEK_SET);
259  fread(compressed_data, 1, size, f);
260  fclose(f);
261 
262  struct j2k_struct* j2c = create_j2c_data();
263  init_j2c_data(j2c, compressed_data, size);
264  int width = get_j2c_width(j2c);
265  int height = get_j2c_height(j2c);
266  int num_components = get_j2c_num_components(j2c);
267  printf("width = %d, height = %d\n", width, height);
268 
269  ojph::ui8* buf = (ojph::ui8*)malloc(width * height * num_components);
270 
271  decode_j2c_data(j2c);
272  int max_val = 255;
273  for (int y = 0; y < height; ++y)
274  {
275  for (int c = 0; c < num_components; ++c)
276  {
277  ojph::si32* sp = pull_j2c_line(j2c);
278  ojph::ui8* dp = buf + c + y * width * num_components;
279  for (int x = 0; x < width; ++x, dp+=num_components)
280  {
281  int val = *sp++;
282  val = val >= 0 ? val : 0;
283  val = val <= max_val ? val : max_val;
284  *dp = (ojph::ui8)val;
285  }
286  }
287  }
288  release_j2c_data(j2c);
289 
290  f = fopen("test_out.ppm", "wb");
291  if (num_components == 1)
292  fprintf(f, "P5 %d %d %d\n", width, height, max_val);
293  else
294  fprintf(f, "P6 %d %d %d\n", width, height, max_val);
295  fwrite(buf, width * height * num_components, 1, f);
296  fclose(f);
297 
298  return 0;
299 }
300 #endif
301 
302 
int main(int argc, char *argv[])
OJPH_EXPORT param_siz access_siz()
OJPH_EXPORT void restrict_input_resolution(ui32 skipped_res_for_data, ui32 skipped_res_for_recon)
OJPH_EXPORT void set_planar(bool planar)
OJPH_EXPORT void enable_resilience()
OJPH_EXPORT void read_headers(infile_base *file)
OJPH_EXPORT void create()
OJPH_EXPORT line_buf * pull(ui32 &comp_num)
OJPH_EXPORT void open(const ui8 *data, size_t size)
Definition: ojph_file.cpp:228
OJPH_EXPORT ui32 get_bit_depth(ui32 comp_num) const
OJPH_EXPORT ui32 get_recon_height(ui32 comp_num) const
OJPH_EXPORT point get_downsampling(ui32 comp_num) const
OJPH_EXPORT ui32 get_recon_width(ui32 comp_num) const
OJPH_EXPORT bool is_signed(ui32 comp_num) const
OJPH_EXPORT ui32 get_num_components() const
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50
j2k_struct * cpp_create_j2c_data(void)
EMSCRIPTEN_KEEPALIVE int get_j2c_num_components(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE int get_j2c_downsampling_y(j2k_struct *j2c, int comp_num)
EMSCRIPTEN_KEEPALIVE void parse_j2c_data(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE void enable_resilience(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE signed int * pull_j2c_line(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE void release_j2c_data(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE int get_j2c_height(j2k_struct *j2c, int comp_num)
signed int * cpp_pull_j2c_line(j2k_struct *j2c)
EMSCRIPTEN_KEEPALIVE void init_j2c_data(j2k_struct *j2c, const uint8_t *data, size_t size)
void cpp_parse_j2c_data(j2k_struct *j2c)
void cpp_init_j2c_data(j2k_struct *j2c, const uint8_t *data, size_t size)
void cpp_restrict_input_resolution(j2k_struct *j2c, int skipped_res_for_read, int skipped_res_for_recon)
EMSCRIPTEN_KEEPALIVE j2k_struct * create_j2c_data(void)
EMSCRIPTEN_KEEPALIVE int get_j2c_is_signed(j2k_struct *j2c, int comp_num)
EMSCRIPTEN_KEEPALIVE int get_j2c_downsampling_x(j2k_struct *j2c, int comp_num)
EMSCRIPTEN_KEEPALIVE int get_j2c_bit_depth(j2k_struct *j2c, int comp_num)
EMSCRIPTEN_KEEPALIVE int get_j2c_width(j2k_struct *j2c, int comp_num)
EMSCRIPTEN_KEEPALIVE void restrict_input_resolution(j2k_struct *j2c, int skipped_res_for_read, int skipped_res_for_recon)
void cpp_release_j2c_data(j2k_struct *j2c)
ojph::mem_infile mem_file
ojph::codestream codestream
si32 * i32
Definition: ojph_mem.h:155