Merge pull request #706 from mayeut/issue135
[openjpeg.git] / tests / test_tile_encoder.c
1 /*
2  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "opj_config.h"
33 #include "openjpeg.h"
34 #include "stdlib.h"
35
36 /* -------------------------------------------------------------------------- */
37
38 /**
39 sample error debug callback expecting no client object
40 */
41 static void error_callback(const char *msg, void *client_data) {
42         (void)client_data;
43         fprintf(stdout, "[ERROR] %s", msg);
44 }
45 /**
46 sample warning debug callback expecting no client object
47 */
48 static void warning_callback(const char *msg, void *client_data) {
49         (void)client_data;
50         fprintf(stdout, "[WARNING] %s", msg);
51 }
52 /**
53 sample debug callback expecting no client object
54 */
55 static void info_callback(const char *msg, void *client_data) {
56         (void)client_data;
57         fprintf(stdout, "[INFO] %s", msg);
58 }
59
60 /* -------------------------------------------------------------------------- */
61
62 #define NUM_COMPS_MAX 4
63 int main (int argc, char *argv[])
64 {
65         opj_cparameters_t l_param;
66         opj_codec_t * l_codec;
67         opj_image_t * l_image;
68         opj_image_cmptparm_t l_params [NUM_COMPS_MAX];
69         opj_stream_t * l_stream;
70         OPJ_UINT32 l_nb_tiles;
71         OPJ_UINT32 l_data_size;
72         unsigned char len;
73
74 #ifdef USING_MCT
75         const OPJ_FLOAT32 l_mct [] =
76         {
77                 1 , 0 , 0 ,
78                 0 , 1 , 0 ,
79                 0 , 0 , 1
80         };
81
82         const OPJ_INT32 l_offsets [] =
83         {
84                 128 , 128 , 128
85         };
86 #endif
87
88         opj_image_cmptparm_t * l_current_param_ptr;
89         OPJ_UINT32 i;
90         OPJ_BYTE *l_data;
91
92   OPJ_UINT32 num_comps;
93   int image_width;
94   int image_height;
95   int tile_width;
96   int tile_height;
97   int comp_prec;
98   int irreversible;
99   char output_file[64];
100
101   /* should be test_tile_encoder 3 2000 2000 1000 1000 8 tte1.j2k */
102   if( argc == 9 )
103     {
104     num_comps = (OPJ_UINT32)atoi( argv[1] );
105     image_width = atoi( argv[2] );
106     image_height = atoi( argv[3] );
107     tile_width = atoi( argv[4] );
108     tile_height = atoi( argv[5] );
109     comp_prec = atoi( argv[6] );
110     irreversible = atoi( argv[7] );
111     strcpy(output_file, argv[8] );
112     }
113   else
114     {
115     num_comps = 3;
116     image_width = 2000;
117     image_height = 2000;
118     tile_width = 1000;
119     tile_height = 1000;
120     comp_prec = 8;
121     irreversible = 1;
122     strcpy(output_file, "test.j2k" );
123     }
124   if( num_comps > NUM_COMPS_MAX )
125     {
126     return 1;
127     }
128         l_nb_tiles = (OPJ_UINT32)(image_width/tile_width) * (OPJ_UINT32)(image_height/tile_height);
129         l_data_size = (OPJ_UINT32)tile_width * (OPJ_UINT32)tile_height * (OPJ_UINT32)num_comps * (OPJ_UINT32)(comp_prec/8);
130
131         l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE));
132         if(l_data == NULL){
133                 return 1;
134         }
135         fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
136         for (i=0;i<l_data_size;++i)     {
137                 l_data[i] = (OPJ_BYTE)i; /*rand();*/
138         }
139
140         opj_set_default_encoder_parameters(&l_param);
141         /** you may here add custom encoding parameters */
142         /* rate specifications */
143         /** number of quality layers in the stream */
144         l_param.tcp_numlayers = 1;
145         l_param.cp_fixed_quality = 1;
146         l_param.tcp_distoratio[0] = 20;
147         /* is using others way of calculation */
148         /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */
149         /* l_param.tcp_rates[0] = ... */
150
151
152         /* tile definitions parameters */
153         /* position of the tile grid aligned with the image */
154         l_param.cp_tx0 = 0;
155         l_param.cp_ty0 = 0;
156         /* tile size, we are using tile based encoding */
157         l_param.tile_size_on = OPJ_TRUE;
158         l_param.cp_tdx = tile_width;
159         l_param.cp_tdy = tile_height;
160
161         /* use irreversible encoding ?*/
162         l_param.irreversible = irreversible;
163
164         /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/
165         /*l_param.cp_rsiz = OPJ_STD_RSIZ;*/
166
167         /* no cinema */
168         /*l_param.cp_cinema = 0;*/
169
170         /* no not bother using SOP or EPH markers, do not use custom size precinct */
171         /* number of precincts to specify */
172         /* l_param.csty = 0;*/
173         /* l_param.res_spec = ... */
174         /* l_param.prch_init[i] = .. */
175         /* l_param.prcw_init[i] = .. */
176
177
178         /* do not use progression order changes */
179         /*l_param.numpocs = 0;*/
180         /* l_param.POC[i].... */
181
182         /* do not restrain the size for a component.*/
183         /* l_param.max_comp_size = 0; */
184
185         /** block encoding style for each component, do not use at the moment */
186         /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */
187         /* l_param.mode = 0;*/
188
189         /** number of resolutions */
190         l_param.numresolution = 6;
191
192         /** progression order to use*/
193         /** OPJ_LRCP, OPJ_RLCP, OPJ_RPCL, PCRL, CPRL */
194         l_param.prog_order = OPJ_LRCP;
195
196         /** no "region" of interest, more precisally component */
197         /* l_param.roi_compno = -1; */
198         /* l_param.roi_shift = 0; */
199
200         /* we are not using multiple tile parts for a tile. */
201         /* l_param.tp_on = 0; */
202         /* l_param.tp_flag = 0; */
203
204         /* if we are using mct */
205 #ifdef USING_MCT
206         opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS);
207 #endif
208
209
210         /* image definition */
211         l_current_param_ptr = l_params;
212         for (i=0;i<num_comps;++i) {
213                 /* do not bother bpp useless */
214                 /*l_current_param_ptr->bpp = COMP_PREC;*/
215                 l_current_param_ptr->dx = 1;
216                 l_current_param_ptr->dy = 1;
217
218                 l_current_param_ptr->h = (OPJ_UINT32)image_height;
219                 l_current_param_ptr->w = (OPJ_UINT32)image_width;
220
221                 l_current_param_ptr->sgnd = 0;
222                 l_current_param_ptr->prec = (OPJ_UINT32)comp_prec;
223
224                 l_current_param_ptr->x0 = 0;
225                 l_current_param_ptr->y0 = 0;
226
227                 ++l_current_param_ptr;
228         }
229
230   /* should we do j2k or jp2 ?*/
231   len = (unsigned char)strlen( output_file );
232   if( strcmp( output_file + len - 4, ".jp2" ) == 0 )
233     {
234     l_codec = opj_create_compress(OPJ_CODEC_JP2);
235     }
236   else
237     {
238     l_codec = opj_create_compress(OPJ_CODEC_J2K);
239     }
240         if (!l_codec) {
241                 free(l_data);
242                 return 1;
243         }
244
245         /* catch events using our callbacks and give a local context */
246         opj_set_info_handler(l_codec, info_callback,00);
247         opj_set_warning_handler(l_codec, warning_callback,00);
248         opj_set_error_handler(l_codec, error_callback,00);
249
250         l_image = opj_image_tile_create(num_comps,l_params,OPJ_CLRSPC_SRGB);
251         if (! l_image) {
252                 free(l_data);
253                 opj_destroy_codec(l_codec);
254                 return 1;
255         }
256
257         l_image->x0 = 0;
258         l_image->y0 = 0;
259         l_image->x1 = (OPJ_UINT32)image_width;
260         l_image->y1 = (OPJ_UINT32)image_height;
261         l_image->color_space = OPJ_CLRSPC_SRGB;
262
263         if (! opj_setup_encoder(l_codec,&l_param,l_image)) {
264                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n");
265                 opj_destroy_codec(l_codec);
266                 opj_image_destroy(l_image);
267                 free(l_data);
268                 return 1;
269         }
270
271     l_stream = opj_stream_create_default_file_stream(output_file, OPJ_FALSE);
272     if (! l_stream) {
273                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file );
274                 opj_destroy_codec(l_codec);
275                 opj_image_destroy(l_image);
276                 free(l_data);
277                 return 1;
278         }
279
280         if (! opj_start_compress(l_codec,l_image,l_stream)) {
281                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n");
282         opj_stream_destroy(l_stream);
283                 opj_destroy_codec(l_codec);
284                 opj_image_destroy(l_image);
285                 free(l_data);
286                 return 1;
287         }
288
289         for (i=0;i<l_nb_tiles;++i) {
290                 if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
291                         fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
292             opj_stream_destroy(l_stream);
293                         opj_destroy_codec(l_codec);
294                         opj_image_destroy(l_image);
295                         free(l_data);
296                         return 1;
297                 }
298         }
299
300         if (! opj_end_compress(l_codec,l_stream)) {
301                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to end compress!\n");
302         opj_stream_destroy(l_stream);
303                 opj_destroy_codec(l_codec);
304                 opj_image_destroy(l_image);
305                 free(l_data);
306                 return 1;
307         }
308
309     opj_stream_destroy(l_stream);
310         opj_destroy_codec(l_codec);
311         opj_image_destroy(l_image);
312
313         free(l_data);
314
315         /* Print profiling*/
316         /*PROFPRINT();*/
317
318         return 0;
319 }
320
321
322
323
324
325