00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043
00044 #include "OSGConfig.h"
00045
00046 #ifdef OSG_WITH_JASPER
00047 #include <jasper/jasper.h>
00048 #endif // OSG_WITH_JASPER
00049
00050 #include "OSGJP2ImageFileType.h"
00051 #include <OSGLog.h>
00052
00053 #include <iostream>
00054
00055 #ifndef OSG_DO_DOC
00056 # ifdef OSG_WITH_JASPER
00057 # define OSG_JP2_ARG(ARG) ARG
00058 # else
00059 # define OSG_JP2_ARG(ARG)
00060 # endif
00061 #else
00062 # define OSG_JP2_ARG(ARG) ARG
00063 #endif
00064
00065 OSG_USING_NAMESPACE
00066
00083 #ifdef OSG_WITH_JASPER
00084
00085
00086 static int readIStream(jas_stream_obj_t *obj, char *buf, int cnt)
00087 {
00088 std::istream *is = reinterpret_cast<std::istream*>(obj);
00089 is->read(buf, cnt);
00090 return is->gcount();
00091 }
00092
00093
00094 static int writeIStream(jas_stream_obj_t *obj, char *buf, int cnt)
00095 {
00096
00097 return 0;
00098 }
00099
00100
00101 static long seekIStream(jas_stream_obj_t *obj, long offset, int origin)
00102 {
00103 std::istream *is = reinterpret_cast<std::istream*>(obj);
00104 std::ios_base::seekdir seekdir;
00105 switch (origin)
00106 {
00107 case SEEK_SET: seekdir = std::ios::beg; break;
00108 case SEEK_CUR: seekdir = std::ios::cur; break;
00109 case SEEK_END: seekdir = std::ios::end; break;
00110 default:
00111 return -1;
00112 }
00113 is->seekg(offset, seekdir);
00114 return is->tellg();
00115 }
00116
00117
00118 static int closeIStream(jas_stream_obj_t *obj)
00119 {
00120
00121 return 0;
00122 }
00123
00124 static jas_stream_ops_t istream_ops =
00125 {
00126 readIStream,
00127 writeIStream,
00128 seekIStream,
00129 closeIStream
00130 };
00131
00132 #endif // OSG_WITH_JASPER
00133
00134
00135
00136
00137
00138
00139
00140
00141 static const Char8 *suffixArray[] = { "jp2" };
00142
00143 JP2ImageFileType JP2ImageFileType:: _the("image/jpeg2000",
00144 suffixArray, sizeof(suffixArray),
00145 OSG_READ_SUPPORTED);
00146
00147
00148
00149
00150
00151
00152
00156 JP2ImageFileType& JP2ImageFileType::the (void)
00157 {
00158 return _the;
00159 }
00160
00161
00162
00163
00164
00165
00170 bool JP2ImageFileType::read(ImagePtr &OSG_JP2_ARG(image), std::istream &OSG_JP2_ARG(is),
00171 const std::string &OSG_JP2_ARG(mimetype))
00172 {
00173 #ifdef OSG_WITH_JASPER
00174
00175
00176 if (jas_init() != 0)
00177 return false;
00178
00179
00180
00181 jas_stream_t *instream = reinterpret_cast<jas_stream_t*>(jas_malloc(sizeof(jas_stream_t)));
00182 if (instream == 0)
00183 {
00184 jas_cleanup();
00185 return false;
00186 }
00187 instream->openmode_ = JAS_STREAM_READ | JAS_STREAM_BINARY;
00188 instream->bufmode_ = JAS_STREAM_UNBUF;
00189 instream->flags_ = 0;
00190 instream->bufbase_ = instream->tinybuf_;
00191 instream->bufstart_ = &instream->bufbase_[JAS_STREAM_MAXPUTBACK];
00192 instream->bufsize_ = 1;
00193 instream->ptr_ = instream->bufstart_;
00194 instream->cnt_ = 0;
00195 instream->ops_ = &istream_ops;
00196 instream->obj_ = &is;
00197 instream->rwcnt_ = 0;
00198 instream->rwlimit_ = -1;
00199
00200
00201 jas_image_t *jasimage = jas_image_decode(instream, -1, 0);
00202
00203
00204 jas_stream_close(instream);
00205
00206
00207 if (jasimage == 0)
00208 {
00209 jas_cleanup();
00210 return false;
00211 }
00212
00213
00214 jas_cmprof_t *outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB);
00215 if (outprof == 0)
00216 {
00217 jas_image_destroy(jasimage);
00218 jas_cleanup();
00219 return false;
00220 }
00221 jas_image_t *altimage = jas_image_chclrspc(jasimage, outprof, JAS_CMXFORM_INTENT_PER);
00222
00223
00224 jas_image_destroy(jasimage);
00225 jas_cmprof_destroy(outprof);
00226
00227
00228 if (altimage == 0)
00229 {
00230 jas_cleanup();
00231 return false;
00232 }
00233
00234
00235 int cmptlut[3];
00236 cmptlut[0] = jas_image_getcmptbytype(altimage, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
00237 cmptlut[1] = jas_image_getcmptbytype(altimage, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
00238 cmptlut[2] = jas_image_getcmptbytype(altimage, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
00239 if ((cmptlut[0] < 0) || (cmptlut[0] < 0) || (cmptlut[0] < 0))
00240 {
00241 jas_image_destroy(altimage);
00242 jas_cleanup();
00243 return false;
00244 }
00245
00246
00247 jas_image_coord_t width = jas_image_cmptwidth(altimage, cmptlut[0]);
00248 jas_image_coord_t height = jas_image_cmptheight(altimage, cmptlut[0]);
00249 if ((jas_image_cmptwidth(altimage, cmptlut[1]) != width) ||
00250 (jas_image_cmptwidth(altimage, cmptlut[2]) != width) ||
00251 (jas_image_cmptheight(altimage, cmptlut[1]) != height) ||
00252 (jas_image_cmptheight(altimage, cmptlut[2]) != height))
00253 {
00254 jas_image_destroy(altimage);
00255 jas_cleanup();
00256 return false;
00257 }
00258
00259
00260 if (image->set(Image::OSG_RGB_PF, width, height) == false)
00261 {
00262 jas_image_destroy(altimage);
00263 jas_cleanup();
00264 return false;
00265 }
00266
00267
00268
00269 unsigned char *scanline = image->getData() + image->getSize();
00270 int row_stride = width * 3;
00271 for (jas_image_coord_t y = 0; y < height; ++y)
00272 {
00273 scanline -= row_stride;
00274 unsigned char *dst = scanline;
00275 for (jas_image_coord_t x = 0; x < width; ++x)
00276 {
00277 for (int c = 0; c < 3; ++c)
00278 {
00279 int sample = jas_image_readcmptsample(altimage, cmptlut[c], x, y);
00280 sample <<= 16 - jas_image_cmptprec(altimage, cmptlut[c]);
00281 if (sample < 0)
00282 sample = 0;
00283 else if (sample > 65535)
00284 sample = 65535;
00285 *dst++ = sample >> 8;
00286 }
00287 }
00288 }
00289
00290
00291 jas_image_destroy(altimage);
00292
00293
00294 jas_cleanup();
00295
00296 return true;
00297
00298 #else
00299
00300 SWARNING <<
00301 getMimeType() <<
00302 " read is not compiled into the current binary " <<
00303 std::endl;
00304 return false;
00305
00306 #endif
00307 }
00308
00309
00315 std::string JP2ImageFileType::determineMimetypeFromStream(std::istream &is)
00316 {
00317 char filecode[8];
00318 is.read(filecode, 8);
00319 is.seekg(-8, std::ios::cur);
00320 return strncmp(filecode + 4, "jP ", 4) == 0 ?
00321 std::string(getMimeType()) : std::string();
00322 }
00323
00324
00328 JP2ImageFileType::JP2ImageFileType( const Char8 *mimeType,
00329 const Char8 *suffixArray[],
00330 UInt16 suffixByteCount,
00331 UInt32 flags) :
00332 ImageFileType(mimeType, suffixArray, suffixByteCount, flags)
00333 {}
00334
00335
00339 JP2ImageFileType::~JP2ImageFileType(void) {}