00001 00002 #ifndef __JBXL_CPP_JPEG2K_TOOl_H_ 00003 #define __JBXL_CPP_JPEG2K_TOOl_H_ 00004 00005 00022 #include "Gdata.h" 00023 #include "xtools.h" 00024 00025 00026 #ifndef HAVE_OPENJPEG_H 00027 #ifndef DISABLE_OPENJPEG 00028 #define DISABLE_OPENJPEG 00029 #endif 00030 #endif 00031 00032 #ifdef DISABLE_OPENJPEG 00033 #undef ENABLE_OPENJPEG 00034 #endif 00035 00036 00037 #ifdef ENABLE_OPENJPEG 00038 00039 #ifndef OPENJPEG_VER 00040 #define OPENJPEG_VER 12 00041 #endif 00042 00043 #define JP2K_VER_12 12 00044 #define JP2K_VER_14 14 00045 #define JP2K_VER_15 15 00046 #define JP2K_VER_20 20 00047 #define JP2K_VER_21 21 00048 00049 #undef HAVE_STDLIB_H 00050 00051 #if OPENJPEG_VER <= JP2K_VER_15 00052 #undef OFF 00053 #ifdef WIN32 00054 #ifdef _DEBUG 00055 #pragma comment(lib, "OpenJPEGd.lib") // DLLを使用 00056 #else 00057 #pragma comment(lib, "OpenJPEG.lib") // DLLを使用 00058 #endif 00059 #endif 00060 #elif OPENJPEG_VER == JP2K_VER_21 00061 #ifdef WIN32 00062 #pragma comment(lib, "openjp2.lib") // DLLを使用 00063 #endif 00064 #else // default 00065 #ifdef WIN32 00066 #pragma comment(lib, "openjp2.lib") // DLLを使用 00067 #endif 00068 #endif 00069 00070 00071 #include <openjpeg.h> 00072 00073 00074 #define JP2K_MAGIC_RFC3745_JP2 "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a" 00075 #define JP2K_MAGIC_JP2 "\x0d\x0a\x87\x0a" 00076 #define JP2K_MAGIC_J2K "\xff\x4f\xff\x51" 00077 00078 #define JP2K_FMT_NONE 0 00079 #define JP2K_FMT_JP2 1 00080 #define JP2K_FMT_J2K 2 00081 #define JP2K_FMT_JPT 3 00082 00083 00084 00085 // 00086 namespace jbxl { 00087 00088 00090 00091 class JPEG2KImage 00092 { 00093 public: 00094 int xs; 00095 int ys; 00096 int ws; 00097 int hs; 00098 int col; 00099 int cmode; 00100 int state; 00101 00102 opj_image_t* image; 00103 00104 public: 00105 JPEG2KImage(void) { init();} 00106 virtual ~JPEG2KImage(void) {} 00107 00108 void init(void); 00109 bool isNull(void); 00110 void clear(void) { fill(0);} 00111 void fill(int v=0); 00112 void free(void); 00113 00114 void setup_image(void); 00115 00116 int& point(int x, int y, int c) { return image->comps->data[c*xs*ys+y*xs+x];} // 高速化のため,チェックなし. 00117 00118 }; 00119 00120 00121 00123 00124 JPEG2KImage readJPEG2KFile(const char* fname); 00125 00126 #if OPENJPEG_VER <= JP2K_VER_15 00127 JPEG2KImage readJPEG2KData(unsigned char* data, int len, int format); 00128 #elif OPENJPEG_VER == JP2K_VER_21 00129 JPEG2KImage readJPEG2KData(const char* fname, int format); 00130 #else //JP2K_VER_20 00131 JPEG2KImage readJPEG2KData(FILE* fp, int format); 00132 #endif 00133 00134 //int writeJPEG2KFile(const char* fname, JPEG2KImage jp); 00135 //int writeJPEG2KData(FILE* fp, JPEG2KImage jp); 00136 00137 int isJPEG2KHeader(Buffer buf); 00138 00139 //CmnHead JPEG2KImage2CmnHead(JPEG2KImage jp); 00140 //JPEG2KImage CmnHead2JPEGImage(CmnHead hd); 00141 00142 // template <typename T> MSGraph<T> JPEG2KImage2MSGraph(JPEG2KImage jp) 00143 // template <typename T> JPEG2KImage MSGraph2JPEG2KImage(MSGraph<T> vp) 00144 00145 00157 template <typename T> MSGraph<T> JPEG2KImage2MSGraph(JPEG2KImage jp) 00158 { 00159 MSGraph<T> vp; 00160 00161 if (jp.isNull()) { 00162 vp.state = JBXL_GRAPH_NODATA_ERROR; 00163 return vp; 00164 } 00165 00166 vp.set(jp.ws, jp.hs, jp.col); 00167 if (vp.isNull()) return vp; 00168 vp.color = jp.cmode; 00169 00170 for (int k=0; k<jp.col; k++) { 00171 int ps = k*jp.ws*jp.hs; 00172 for (int j=0; j<jp.hs; j++) { 00173 int kk = j*jp.ws + ps; 00174 int yy = j*jp.xs; 00175 for (int i=0; i<jp.ws; i++) { 00176 vp.gp[kk + i] = (T)(jp.image->comps[k].data[yy + i]); // as Byte境界 00177 } 00178 } 00179 } 00180 00181 return vp; 00182 } 00183 00184 00196 /* 00197 template <typename T> JPEG2KImage MSGraph2JPEG2KImage(MSGraph<T> vp) 00198 { 00199 // image の初期化とメモリ確保が良く分らん. 00200 00201 JPEG2KImage jp; 00202 00203 if (vp.isNull()) { 00204 jp.state = GRAPH_NODATA_ERROR; 00205 return jp; 00206 } 00207 00208 for (int k=0; k<jp.col; k++) { 00209 int zp = k*jp.xs*jp.ys; 00210 for (int j=0; j<jp.ys; j++) { 00211 int yp = j*jp.xs; 00212 int yz = yp + zp; 00213 for (int i=0; i<jp.xs; i++) { 00214 jp.image->comps[k].data[yp + i] = (int)vp.gp[yz + i]; 00215 } 00216 } 00217 } 00218 00219 return jp; 00220 } 00221 */ 00222 00223 00224 } // namespace 00225 00226 00227 00228 #endif // ENABLE_OPENJPEG 00229 00230 #endif // __JBXL_CPP_JPEG2K_TOOl_H_ 00231 00232 00233 00234 00235 /* 00237 // Data Structure reffered from opnejpeg.h 00239 00240 OpenJpeg-1.5 00241 typedef struct opj_image 00242 { 00243 int x0; // XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area 00244 int y0; // YOsiz: vertical offset from the origin of the reference grid to the top side of the image area 00245 int x1; // Xsiz: width of the reference grid 00246 int y1; // Ysiz: height of the reference grid 00247 int numcomps; // number of components in the image 00248 OPJ_COLOR_SPACE color_space;// color space: sRGB, Greyscale or YUV 00249 opj_image_comp_t *comps; // image components 00250 unsigned char *icc_profile_buf; // 'restricted' ICC profile 00251 int icc_profile_len; // size of ICC profile 00252 } opj_image_t; 00253 00254 00255 OpenJpeg-2.x 00256 typedef struct opj_image 00257 { 00258 OPJ_UINT32 x0; // XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area 00259 OPJ_UINT32 y0; // YOsiz: vertical offset from the origin of the reference grid to the top side of the image area 00260 OPJ_UINT32 x1; // Xsiz: width of the reference grid 00261 OPJ_UINT32 y1; // Ysiz: height of the reference grid 00262 OPJ_UINT32 numcomps; // number of components in the image 00263 OPJ_COLOR_SPACE color_space;// color space: sRGB, Greyscale or YUV 00264 opj_image_comp_t *comps; // image components 00265 OPJ_BYTE *icc_profile_buf; // 'restricted' ICC profile 00266 OPJ_UINT32 icc_profile_len; // size of ICC profile 00267 } opj_image_t; 00268 00269 00270 OpenJpeg-1.5 00271 typedef struct opj_image_comp 00272 { 00273 int dx; // XRsiz: horizontal separation of a sample of ith component with respect to the reference grid 00274 int dy; // YRsiz: vertical separation of a sample of ith component with respect to the reference grid 00275 int w; // data width 00276 int h; // data height 00277 int x0; // x component offset compared to the whole image 00278 int y0; // y component offset compared to the whole image 00279 int prec; // precision 00280 int bpp; // image depth in bits 00281 int sgnd; // signed (1) / unsigned (0) 00282 int resno_decoded; // number of decoded resolution 00283 int factor; // number of division by 2 of the out image compared to the original size of image 00284 int *data; // image component data 00285 } opj_image_comp_t; 00286 00287 00288 OpenJpeg-2.x 00289 typedef struct opj_image_comp 00290 { 00291 OPJ_UINT32 dx; // XRsiz: horizontal separation of a sample of ith component with respect to the reference grid 00292 OPJ_UINT32 dy; // YRsiz: vertical separation of a sample of ith component with respect to the reference grid 00293 OPJ_UINT32 w; // data width 00294 OPJ_UINT32 h; // data height 00295 OPJ_UINT32 x0; // x component offset compared to the whole image 00296 OPJ_UINT32 y0; // y component offset compared to the whole image 00297 OPJ_UINT32 prec; // precision 00298 OPJ_UINT32 bpp; // image depth in bits 00299 OPJ_UINT32 sgnd; // signed (1) / unsigned (0) 00300 OPJ_UINT32 resno_decoded; // number of decoded resolution 00301 OPJ_UINT32 factor; // number of division by 2 of the out image compared to the original size of image 00302 OPJ_INT32 *data; // image component data 00303 } opj_image_comp_t; 00304 00305 00306 OpenJpeg-1.5 00307 typedef enum COLOR_SPACE 00308 { 00309 CLRSPC_UNKNOWN = -1, // not supported by the library 00310 CLRSPC_UNSPECIFIED = 0, // not specified in the codestream 00311 CLRSPC_SRGB = 1, // sRGB 00312 CLRSPC_GRAY = 2, // grayscale 00313 CLRSPC_SYCC = 3 // YUV 00314 } OPJ_COLOR_SPACE; 00315 00316 00317 OpenJpeg-2.x 00318 typedef enum COLOR_SPACE 00319 { 00320 OPJ_CLRSPC_UNKNOWN = -1, // not supported by the library 00321 OPJ_CLRSPC_UNSPECIFIED = 0, // not specified in the codestream 00322 OPJ_CLRSPC_SRGB = 1, // sRGB 00323 OPJ_CLRSPC_GRAY = 2, // grayscale 00324 OPJ_CLRSPC_SYCC = 3 // YUV 00325 } OPJ_COLOR_SPACE; 00326 00327 */ 00328