[trunk] Start activating tile tests
[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
33 #ifdef _WIN32
34 #include <windows.h>
35 #else
36 #include <strings.h>
37 #define _stricmp strcasecmp
38 #define _strnicmp strncasecmp
39 #endif /* _WIN32 */
40
41 #include "opj_config.h"
42 #include "openjpeg.h"
43 #include "stdlib.h"
44
45 /* -------------------------------------------------------------------------- */
46
47 /**
48 sample error callback expecting a FILE* client object
49 */
50 void error_callback_file(const char *msg, void *client_data) {
51         FILE *stream = (FILE*)client_data;
52         fprintf(stream, "[ERROR] %s", msg);
53 }
54 /**
55 sample warning callback expecting a FILE* client object
56 */
57 void warning_callback_file(const char *msg, void *client_data) {
58         FILE *stream = (FILE*)client_data;
59         fprintf(stream, "[WARNING] %s", msg);
60 }
61 /**
62 sample error debug callback expecting no client object
63 */
64 void error_callback(const char *msg, void *client_data) {
65         (void)client_data;
66         fprintf(stdout, "[ERROR] %s", msg);
67 }
68 /**
69 sample warning debug callback expecting no client object
70 */
71 void warning_callback(const char *msg, void *client_data) {
72         (void)client_data;
73         fprintf(stdout, "[WARNING] %s", msg);
74 }
75 /**
76 sample debug callback expecting no client object
77 */
78 void info_callback(const char *msg, void *client_data) {
79         (void)client_data;
80         fprintf(stdout, "[INFO] %s", msg);
81 }
82
83 /* -------------------------------------------------------------------------- */
84
85 #define NUM_COMPS_MAX 4
86 int main (int argc, char *argv[])
87 {
88         opj_cparameters_t l_param;
89         opj_codec_t * l_codec;
90         opj_image_t * l_image;
91         opj_image_cmptparm_t l_params [NUM_COMPS_MAX];
92         FILE * l_file;
93         opj_stream_t * l_stream;
94         OPJ_UINT32 l_nb_tiles;
95         OPJ_UINT32 l_data_size;
96
97 #ifdef USING_MCT
98         const OPJ_FLOAT32 l_mct [] =
99         {
100                 1 , 0 , 0 ,
101                 0 , 1 , 0 ,
102                 0 , 0 , 1
103         };
104
105         const OPJ_INT32 l_offsets [] =
106         {
107                 128 , 128 , 128
108         };
109 #endif
110
111         opj_image_cmptparm_t * l_current_param_ptr;
112         OPJ_UINT32 i;
113         OPJ_BYTE *l_data;
114
115   OPJ_UINT32 num_comps;
116   int image_width;
117   int image_height;
118   int tile_width;
119   int tile_height;
120   int comp_prec;
121   char output_file[64];
122
123   /* should be test_tile_encoder 3 2000 2000 1000 1000 8 tte1.j2k */
124   if( argc == 8 )
125     {
126     num_comps = atoi( argv[1] );
127     image_width = atoi( argv[2] );
128     image_height = atoi( argv[3] );
129     tile_width = atoi( argv[4] );
130     tile_height = atoi( argv[5] );
131     comp_prec = atoi( argv[6] );
132     strcpy(output_file, argv[7] );
133     }
134   else
135     {
136     num_comps = 3;
137     image_width = 2000;
138     image_height = 2000;
139     tile_width = 1000;
140     tile_height = 1000;
141     comp_prec = 8;
142     strcpy(output_file, "test.j2k" );
143     }
144   if( num_comps > NUM_COMPS_MAX )
145     {
146     return 1;
147     }
148         l_nb_tiles = (image_width/tile_width) * (image_height/tile_height);
149         l_data_size = tile_width * tile_height * num_comps * (comp_prec/8);
150
151         l_data = (OPJ_BYTE*) malloc(tile_width * tile_height * num_comps * (comp_prec/8) * sizeof(OPJ_BYTE));
152
153         fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
154         for (i=0;i<l_data_size;++i)     {
155                 l_data[i] = i; //rand();
156         }
157
158         opj_set_default_encoder_parameters(&l_param);
159         /** you may here add custom encoding parameters */
160         /* rate specifications */
161         /** number of quality layers in the stream */
162         l_param.tcp_numlayers = 1;
163         l_param.cp_fixed_quality = 1;
164         l_param.tcp_distoratio[0] = 20;
165         /* is using others way of calculation */
166         /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */
167         /* l_param.tcp_rates[0] = ... */
168
169
170         /* tile definitions parameters */
171         /* position of the tile grid aligned with the image */
172         l_param.cp_tx0 = 0;
173         l_param.cp_ty0 = 0;
174         /* tile size, we are using tile based encoding */
175         l_param.tile_size_on = OPJ_TRUE;
176         l_param.cp_tdx = tile_width;
177         l_param.cp_tdy = tile_height;
178
179         /* use irreversible encoding ?*/
180         l_param.irreversible = 1;
181
182         /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/
183         /*l_param.cp_rsiz = STD_RSIZ;*/
184
185         /* no cinema */
186         /*l_param.cp_cinema = 0;*/
187
188         /* no not bother using SOP or EPH markers, do not use custom size precinct */
189         /* number of precincts to specify */
190         /* l_param.csty = 0;*/
191         /* l_param.res_spec = ... */
192         /* l_param.prch_init[i] = .. */
193         /* l_param.prcw_init[i] = .. */
194
195
196         /* do not use progression order changes */
197         /*l_param.numpocs = 0;*/
198         /* l_param.POC[i].... */
199
200         /* do not restrain the size for a component.*/
201         /* l_param.max_comp_size = 0; */
202
203         /** block encoding style for each component, do not use at the moment */
204         /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */
205         /* l_param.mode = 0;*/
206
207         /** number of resolutions */
208         l_param.numresolution = 6;
209
210         /** progression order to use*/
211         /** LRCP, RLCP, RPCL, PCRL, CPRL */
212         l_param.prog_order = LRCP;
213
214         /** no "region" of interest, more precisally component */
215         /* l_param.roi_compno = -1; */
216         /* l_param.roi_shift = 0; */
217
218         /* we are not using multiple tile parts for a tile. */
219         /* l_param.tp_on = 0; */
220         /* l_param.tp_flag = 0; */
221
222         /* if we are using mct */
223 #ifdef USING_MCT
224         opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS);
225 #endif
226
227
228         /* image definition */
229         l_current_param_ptr = l_params;
230         for (i=0;i<num_comps;++i) {
231                 /* do not bother bpp useless */
232                 /*l_current_param_ptr->bpp = COMP_PREC;*/
233                 l_current_param_ptr->dx = 1;
234                 l_current_param_ptr->dy = 1;
235
236                 l_current_param_ptr->h = image_height;
237                 l_current_param_ptr->w = image_width;
238
239                 l_current_param_ptr->sgnd = 0;
240                 l_current_param_ptr->prec = comp_prec;
241
242                 l_current_param_ptr->x0 = 0;
243                 l_current_param_ptr->y0 = 0;
244
245                 ++l_current_param_ptr;
246         }
247
248   // should we do j2k or jp2 ?
249   size_t len = strlen( output_file );
250   if( strcmp( output_file + len - 4, ".jp2" ) == 0 )
251     {
252     l_codec = opj_create_compress_v2(CODEC_JP2);
253     }
254   else
255     {
256     l_codec = opj_create_compress_v2(CODEC_J2K);
257     }
258         if (!l_codec) {
259                 return 1;
260         }
261
262         /* catch events using our callbacks and give a local context */
263         opj_set_info_handler(l_codec, info_callback,00);
264         opj_set_warning_handler(l_codec, warning_callback,00);
265         opj_set_error_handler(l_codec, error_callback,00);
266
267         l_image = opj_image_tile_create(num_comps,l_params,CLRSPC_SRGB);
268         if (! l_image) {
269                 opj_destroy_codec(l_codec);
270                 return 1;
271         }
272
273         l_image->x0 = 0;
274         l_image->y0 = 0;
275         l_image->x1 = image_width;
276         l_image->y1 = image_height;
277         l_image->color_space = CLRSPC_SRGB;
278
279         if (! opj_setup_encoder_v2(l_codec,&l_param,l_image)) {
280                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n");
281                 opj_destroy_codec(l_codec);
282                 opj_image_destroy(l_image);
283                 return 1;
284         }
285
286         l_file = fopen(output_file,"wb");
287         if (! l_file) {
288                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the output file!\n");
289                 opj_destroy_codec(l_codec);
290                 opj_image_destroy(l_image);
291                 return 1;
292         }
293
294         l_stream = opj_stream_create_default_file_stream(l_file, OPJ_FALSE);
295
296         if (! opj_start_compress(l_codec,l_image,l_stream)) {
297                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n");
298                 opj_stream_destroy(l_stream);
299                 fclose(l_file);
300                 opj_destroy_codec(l_codec);
301                 opj_image_destroy(l_image);
302                 return 1;
303         }
304
305         for (i=0;i<l_nb_tiles;++i) {
306                 if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
307                         fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
308                         opj_stream_destroy(l_stream);
309                         fclose(l_file);
310                         opj_destroy_codec(l_codec);
311                         opj_image_destroy(l_image);
312                         return 1;
313                 }
314         }
315
316         if (! opj_end_compress(l_codec,l_stream)) {
317                 fprintf(stderr, "ERROR -> test_tile_encoder: failed to end compress!\n");
318                 opj_stream_destroy(l_stream);
319                 fclose(l_file);
320                 opj_destroy_codec(l_codec);
321                 opj_image_destroy(l_image);
322                 return 1;
323         }
324
325         opj_stream_destroy(l_stream);
326         fclose(l_file);
327         opj_destroy_codec(l_codec);
328         opj_image_destroy(l_image);
329
330         // Print profiling
331         //PROFPRINT();
332
333         return 0;
334 }
335
336
337
338
339
340