2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2017, IntoPix SA <contact@intopix.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
38 #include <sys/resource.h>
39 #include <sys/times.h>
42 OPJ_INT32 getValue(OPJ_UINT32 i)
44 return ((OPJ_INT32)i % 511) - 256;
47 void init_tilec(opj_tcd_tilecomp_t * l_tilec,
52 OPJ_UINT32 numresolutions)
54 opj_tcd_resolution_t* l_res;
55 OPJ_UINT32 resno, l_level_no;
58 memset(l_tilec, 0, sizeof(*l_tilec));
63 nValues = (size_t)(l_tilec->x1 - l_tilec->x0) *
64 (size_t)(l_tilec->y1 - l_tilec->y0);
65 l_tilec->data = (OPJ_INT32*) opj_malloc(sizeof(OPJ_INT32) * nValues);
66 for (i = 0; i < nValues; i++) {
67 l_tilec->data[i] = getValue((OPJ_UINT32)i);
69 l_tilec->numresolutions = numresolutions;
70 l_tilec->resolutions = (opj_tcd_resolution_t*) opj_calloc(
71 l_tilec->numresolutions,
72 sizeof(opj_tcd_resolution_t));
74 l_level_no = l_tilec->numresolutions;
75 l_res = l_tilec->resolutions;
77 /* Adapted from opj_tcd_init_tile() */
78 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
82 /* border for each resolution level (global) */
83 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
84 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
85 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
86 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
92 void free_tilec(opj_tcd_tilecomp_t * l_tilec)
94 opj_free(l_tilec->data);
95 opj_free(l_tilec->resolutions);
101 "bench_dwt [-size value] [-check] [-display] [-num_resolutions val]\n");
103 " [-offset x y] [-num_threads val]\n");
108 OPJ_FLOAT64 opj_clock(void)
111 /* _WIN32: use QueryPerformance (very accurate) */
112 LARGE_INTEGER freq, t ;
113 /* freq is the clock speed of the CPU */
114 QueryPerformanceFrequency(&freq) ;
115 /* cout << "freq = " << ((double) freq.QuadPart) << endl; */
116 /* t is the high resolution performance counter (see MSDN) */
117 QueryPerformanceCounter(& t) ;
118 return freq.QuadPart ? (t.QuadPart / (OPJ_FLOAT64) freq.QuadPart) : 0 ;
120 /* Unix or Linux: use resource usage */
122 OPJ_FLOAT64 procTime;
123 /* (1) Get the rusage data structure at this moment (man getrusage) */
125 /* (2) What is the elapsed time ? - CPU time = User time + System time */
126 /* (2a) Get the seconds */
127 procTime = (OPJ_FLOAT64)(t.ru_utime.tv_sec + t.ru_stime.tv_sec);
128 /* (2b) More precisely! Get the microseconds part ! */
129 return (procTime + (OPJ_FLOAT64)(t.ru_utime.tv_usec + t.ru_stime.tv_usec) *
134 int main(int argc, char** argv)
138 opj_tcd_image_t tcd_image;
139 opj_tcd_tile_t tcd_tile;
140 opj_tcd_tilecomp_t tilec;
142 opj_image_comp_t image_comp;
143 opj_thread_pool_t* tp;
145 OPJ_BOOL display = OPJ_FALSE;
146 OPJ_BOOL check = OPJ_FALSE;
147 OPJ_INT32 size = 16384 - 1;
148 OPJ_FLOAT64 start, stop;
149 OPJ_UINT32 offset_x = ((OPJ_UINT32)size + 1) / 2 - 1;
150 OPJ_UINT32 offset_y = ((OPJ_UINT32)size + 1) / 2 - 1;
151 OPJ_UINT32 num_resolutions = 6;
153 for (i = 1; i < argc; i++) {
154 if (strcmp(argv[i], "-display") == 0) {
157 } else if (strcmp(argv[i], "-check") == 0) {
159 } else if (strcmp(argv[i], "-size") == 0 && i + 1 < argc) {
160 size = atoi(argv[i + 1]);
162 } else if (strcmp(argv[i], "-num_threads") == 0 && i + 1 < argc) {
163 num_threads = atoi(argv[i + 1]);
165 } else if (strcmp(argv[i], "-num_resolutions") == 0 && i + 1 < argc) {
166 num_resolutions = (OPJ_UINT32)atoi(argv[i + 1]);
167 if (num_resolutions == 0 || num_resolutions > 32) {
169 "Invalid value for num_resolutions. Should be >= 1 and <= 32\n");
173 } else if (strcmp(argv[i], "-offset") == 0 && i + 2 < argc) {
174 offset_x = (OPJ_UINT32)atoi(argv[i + 1]);
175 offset_y = (OPJ_UINT32)atoi(argv[i + 2]);
182 tp = opj_thread_pool_create(num_threads);
184 init_tilec(&tilec, (OPJ_INT32)offset_x, (OPJ_INT32)offset_y,
185 (OPJ_INT32)offset_x + size, (OPJ_INT32)offset_y + size,
191 for (j = 0; j < tilec.y1 - tilec.y0; j++) {
192 for (i = 0; i < tilec.x1 - tilec.x0; i++) {
193 printf("%d ", tilec.data[k]);
200 memset(&tcd, 0, sizeof(tcd));
201 tcd.thread_pool = tp;
202 tcd.whole_tile_decoding = OPJ_TRUE;
203 tcd.win_x0 = (OPJ_UINT32)tilec.x0;
204 tcd.win_y0 = (OPJ_UINT32)tilec.y0;
205 tcd.win_x1 = (OPJ_UINT32)tilec.x1;
206 tcd.win_y1 = (OPJ_UINT32)tilec.y1;
207 tcd.tcd_image = &tcd_image;
208 memset(&tcd_image, 0, sizeof(tcd_image));
209 tcd_image.tiles = &tcd_tile;
210 memset(&tcd_tile, 0, sizeof(tcd_tile));
211 tcd_tile.x0 = tilec.x0;
212 tcd_tile.y0 = tilec.y0;
213 tcd_tile.x1 = tilec.x1;
214 tcd_tile.y1 = tilec.y1;
215 tcd_tile.numcomps = 1;
216 tcd_tile.comps = &tilec;
218 memset(&image, 0, sizeof(image));
220 image.comps = &image_comp;
221 memset(&image_comp, 0, sizeof(image_comp));
226 opj_dwt_decode(&tcd, &tilec, tilec.numresolutions);
228 printf("time for dwt_decode: %.03f s\n", stop - start);
230 if (display || check) {
232 printf("After IDWT\n");
234 for (j = 0; j < tilec.y1 - tilec.y0; j++) {
235 for (i = 0; i < tilec.x1 - tilec.x0; i++) {
236 printf("%d ", tilec.data[k]);
243 opj_dwt_encode(&tilec);
245 printf("After FDWT\n");
247 for (j = 0; j < tilec.y1 - tilec.y0; j++) {
248 for (i = 0; i < tilec.x1 - tilec.x0; i++) {
249 printf("%d ", tilec.data[k]);
258 size_t nValues = (size_t)(tilec.x1 - tilec.x0) *
259 (size_t)(tilec.y1 - tilec.y0);
260 for (idx = 0; idx < nValues; idx++) {
261 if (tilec.data[idx] != getValue((OPJ_UINT32)idx)) {
262 printf("Difference found at idx = %u\n", (OPJ_UINT32)idx);
271 opj_thread_pool_destroy(tp);