diff options
author | Jacek Antonelli | 2008-08-15 23:44:50 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:50 -0500 |
commit | 89fe5dab825a62a0e3fd8d248cbc91c65eb2a426 (patch) | |
tree | bcff14b7888d04a2fec799c59369f6095224bd08 /linden/indra/llimagej2coj/llimagej2coj.cpp | |
parent | Second Life viewer sources 1.13.3.2 (diff) | |
download | meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.zip meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.gz meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.bz2 meta-impy-89fe5dab825a62a0e3fd8d248cbc91c65eb2a426.tar.xz |
Second Life viewer sources 1.14.0.0
Diffstat (limited to 'linden/indra/llimagej2coj/llimagej2coj.cpp')
-rw-r--r-- | linden/indra/llimagej2coj/llimagej2coj.cpp | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/linden/indra/llimagej2coj/llimagej2coj.cpp b/linden/indra/llimagej2coj/llimagej2coj.cpp index f292da9..307f952 100644 --- a/linden/indra/llimagej2coj/llimagej2coj.cpp +++ b/linden/indra/llimagej2coj/llimagej2coj.cpp | |||
@@ -28,6 +28,8 @@ | |||
28 | #include "linden_common.h" | 28 | #include "linden_common.h" |
29 | #include "llimagej2coj.h" | 29 | #include "llimagej2coj.h" |
30 | 30 | ||
31 | // this is defined so that we get static linking. | ||
32 | #define OPJ_STATIC | ||
31 | #include "openjpeg/openjpeg.h" | 33 | #include "openjpeg/openjpeg.h" |
32 | 34 | ||
33 | #include "lltimer.h" | 35 | #include "lltimer.h" |
@@ -122,46 +124,61 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod | |||
122 | 124 | ||
123 | /* decode the stream and fill the image structure */ | 125 | /* decode the stream and fill the image structure */ |
124 | image = opj_decode(dinfo, cio); | 126 | image = opj_decode(dinfo, cio); |
125 | if(!image) | ||
126 | { | ||
127 | fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); | ||
128 | opj_destroy_decompress(dinfo); | ||
129 | opj_cio_close(cio); | ||
130 | return 1; | ||
131 | } | ||
132 | 127 | ||
133 | /* close the byte stream */ | 128 | /* close the byte stream */ |
134 | opj_cio_close(cio); | 129 | opj_cio_close(cio); |
135 | 130 | ||
136 | |||
137 | /* free remaining structures */ | 131 | /* free remaining structures */ |
138 | if(dinfo) { | 132 | if(dinfo) |
133 | { | ||
139 | opj_destroy_decompress(dinfo); | 134 | opj_destroy_decompress(dinfo); |
140 | } | 135 | } |
141 | 136 | ||
137 | // The image decode failed if the return was NULL or the component | ||
138 | // count was zero. The latter is just a sanity check before we | ||
139 | // dereference the array. | ||
140 | if(!image || !image->numcomps) | ||
141 | { | ||
142 | fprintf(stderr, "ERROR -> decodeImpl: failed to decode image!\n"); | ||
143 | if (image) | ||
144 | opj_image_destroy(image); | ||
145 | |||
146 | return TRUE; // done | ||
147 | } | ||
148 | |||
142 | // Copy image data into our raw image format (instead of the separate channel format | 149 | // Copy image data into our raw image format (instead of the separate channel format |
143 | S32 width = 0; | ||
144 | S32 height = 0; | ||
145 | 150 | ||
146 | S32 img_components = image->numcomps; | 151 | S32 img_components = image->numcomps; |
147 | S32 channels = img_components - first_channel; | 152 | S32 channels = img_components - first_channel; |
148 | if( channels > max_channel_count ) | 153 | if( channels > max_channel_count ) |
149 | { | ||
150 | channels = max_channel_count; | 154 | channels = max_channel_count; |
151 | } | 155 | |
152 | width = image->x1 - image->x0; | 156 | // Component buffers are allocated in an image width by height buffer. |
153 | height = image->y1 - image->y0; | 157 | // The image placed in that buffer is ceil(width/2^factor) by |
158 | // ceil(height/2^factor) and if the factor isn't zero it will be at the | ||
159 | // top left of the buffer with black filled in the rest of the pixels. | ||
160 | // It is integer math so the formula is written in ceildivpo2. | ||
161 | // (Assuming all the components have the same width, height and | ||
162 | // factor.) | ||
163 | S32 comp_width = image->comps[0].w; | ||
164 | S32 f=image->comps[0].factor; | ||
165 | S32 width = ceildivpow2(image->x1 - image->x0, f); | ||
166 | S32 height = ceildivpow2(image->y1 - image->y0, f); | ||
154 | raw_image.resize(width, height, channels); | 167 | raw_image.resize(width, height, channels); |
155 | U8 *rawp = raw_image.getData(); | 168 | U8 *rawp = raw_image.getData(); |
156 | 169 | ||
157 | for (S32 comp = first_channel; comp < first_channel + channels; comp++) | 170 | // first_channel is what channel to start copying from |
171 | // dest is what channel to copy to. first_channel comes from the | ||
172 | // argument, dest always starts writing at channel zero. | ||
173 | for (S32 comp = first_channel, dest=0; comp < first_channel + channels; | ||
174 | comp++, dest++) | ||
158 | { | 175 | { |
159 | S32 offset = comp; | 176 | S32 offset = dest; |
160 | for (S32 y = (height - 1); y >= 0; y--) | 177 | for (S32 y = (height - 1); y >= 0; y--) |
161 | { | 178 | { |
162 | for (S32 x = 0; x < width; x++) | 179 | for (S32 x = 0; x < width; x++) |
163 | { | 180 | { |
164 | rawp[offset] = image->comps[comp].data[y*width + x]; | 181 | rawp[offset] = image->comps[comp].data[y*comp_width + x]; |
165 | offset += channels; | 182 | offset += channels; |
166 | } | 183 | } |
167 | } | 184 | } |
@@ -170,8 +187,7 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod | |||
170 | /* free image data structure */ | 187 | /* free image data structure */ |
171 | opj_image_destroy(image); | 188 | opj_image_destroy(image); |
172 | 189 | ||
173 | base.setDecodingDone(); | 190 | return TRUE; // done |
174 | return TRUE; | ||
175 | } | 191 | } |
176 | 192 | ||
177 | 193 | ||
@@ -281,6 +297,7 @@ BOOL LLImageJ2COJ::encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, con | |||
281 | codestream_length = cio_tell(cio); | 297 | codestream_length = cio_tell(cio); |
282 | 298 | ||
283 | base.copyData(cio->buffer, codestream_length); | 299 | base.copyData(cio->buffer, codestream_length); |
300 | base.updateData(); // set width, height | ||
284 | 301 | ||
285 | /* close and free the byte stream */ | 302 | /* close and free the byte stream */ |
286 | opj_cio_close(cio); | 303 | opj_cio_close(cio); |
@@ -323,6 +340,9 @@ BOOL LLImageJ2COJ::getMetadata(LLImageJ2C &base) | |||
323 | /* set decoding parameters to default values */ | 340 | /* set decoding parameters to default values */ |
324 | opj_set_default_decoder_parameters(¶meters); | 341 | opj_set_default_decoder_parameters(¶meters); |
325 | 342 | ||
343 | // Only decode what's required to get the size data. | ||
344 | parameters.cp_limit_decoding=LIMIT_TO_MAIN_HEADER; | ||
345 | |||
326 | //parameters.cp_reduce = mRawDiscardLevel; | 346 | //parameters.cp_reduce = mRawDiscardLevel; |
327 | 347 | ||
328 | /* decode the code-stream */ | 348 | /* decode the code-stream */ |
@@ -344,23 +364,22 @@ BOOL LLImageJ2COJ::getMetadata(LLImageJ2C &base) | |||
344 | 364 | ||
345 | /* decode the stream and fill the image structure */ | 365 | /* decode the stream and fill the image structure */ |
346 | image = opj_decode(dinfo, cio); | 366 | image = opj_decode(dinfo, cio); |
347 | if(!image) | ||
348 | { | ||
349 | fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); | ||
350 | opj_destroy_decompress(dinfo); | ||
351 | opj_cio_close(cio); | ||
352 | return 1; | ||
353 | } | ||
354 | 367 | ||
355 | /* close the byte stream */ | 368 | /* close the byte stream */ |
356 | opj_cio_close(cio); | 369 | opj_cio_close(cio); |
357 | 370 | ||
358 | |||
359 | /* free remaining structures */ | 371 | /* free remaining structures */ |
360 | if(dinfo) { | 372 | if(dinfo) |
373 | { | ||
361 | opj_destroy_decompress(dinfo); | 374 | opj_destroy_decompress(dinfo); |
362 | } | 375 | } |
363 | 376 | ||
377 | if(!image) | ||
378 | { | ||
379 | fprintf(stderr, "ERROR -> getMetadata: failed to decode image!\n"); | ||
380 | return FALSE; | ||
381 | } | ||
382 | |||
364 | // Copy image data into our raw image format (instead of the separate channel format | 383 | // Copy image data into our raw image format (instead of the separate channel format |
365 | S32 width = 0; | 384 | S32 width = 0; |
366 | S32 height = 0; | 385 | S32 height = 0; |
@@ -371,5 +390,6 @@ BOOL LLImageJ2COJ::getMetadata(LLImageJ2C &base) | |||
371 | base.setSize(width, height, img_components); | 390 | base.setSize(width, height, img_components); |
372 | 391 | ||
373 | /* free image data structure */ | 392 | /* free image data structure */ |
374 | opj_image_destroy(image); return TRUE; | 393 | opj_image_destroy(image); |
394 | return TRUE; | ||
375 | } | 395 | } |