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