ximajas.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 7k
Category:

Graph program

Development Platform:

Visual C++

  1. /*
  2.  * File: ximajas.cpp
  3.  * Purpose: Platform Independent JasPer Image Class Loader and Writer
  4.  * 12/Apr/2003 Davide Pizzolato - www.xdp.it
  5.  * CxImage version 5.99a 08/Feb/2004
  6.  */
  7. #include "ximajas.h"
  8. #if CXIMAGE_SUPPORT_JASPER
  9. ////////////////////////////////////////////////////////////////////////////////
  10. bool CxImageJAS::Decode(CxFile *hFile, DWORD imagetype)
  11. {
  12. if (hFile == NULL) return false;
  13. jas_image_t *image=0;
  14. jas_stream_t *in=0;
  15. jas_matrix_t **bufs=0;
  16. long i,error=0;
  17. //jas_setdbglevel(0);
  18.   try
  19.   {
  20. if (jas_init())
  21. throw "cannot initialize jasper";
  22. if (!(in = jas_stream_fdopen(0, "rb")))
  23. throw "error: cannot open standard input";
  24. CxFileJas src(hFile,in);
  25. if (!(image = jas_image_decode(in, -1, 0))) 
  26. throw "error: cannot load image data";
  27. long x,y,w,h,depth,cmptno;
  28. w = jas_image_cmptwidth(image,0);
  29. h = jas_image_cmptheight(image,0);
  30. depth = jas_image_cmptprec(image,0);
  31. if (image->numcmpts_ > 64 || image->numcmpts_ < 0)
  32. throw "error: too much components";
  33. if (depth!=1 && depth!=4 && depth!=8){
  34. jas_image_t *newimage;
  35. jas_cmprof_t *outprof;
  36. //jas_eprintf("forcing conversion to sRGBn");
  37. if (!(outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) {
  38. throw "cannot create sRGB profile";
  39. }
  40. if (!(newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER))) {
  41. throw "cannot convert to sRGB";
  42. }
  43. jas_image_destroy(image);
  44. jas_cmprof_destroy(outprof);
  45. image = newimage;
  46. }
  47. bufs = (jas_matrix_t **)calloc(image->numcmpts_, sizeof(jas_matrix_t**));
  48. for (i = 0; i < image->numcmpts_; ++i) {
  49. if (!(bufs[i] = jas_matrix_create(1, w))) {
  50. throw "error: cannot allocate memory";
  51. }
  52. }
  53. if (image->numcmpts_==3 &&
  54. image->cmpts_[0]->width_ == image->cmpts_[1]->width_ &&
  55. image->cmpts_[1]->width_ == image->cmpts_[2]->width_ &&
  56. image->cmpts_[0]->height_ == image->cmpts_[1]->height_ &&
  57. image->cmpts_[1]->height_ == image->cmpts_[2]->height_ &&
  58. image->cmpts_[0]->prec_  == image->cmpts_[1]->prec_ &&
  59. image->cmpts_[1]->prec_ == image->cmpts_[2]->prec_ )
  60. {
  61. if(!Create(w,h,24,imagetype))
  62. throw "Can't allocate memory";
  63. RGBQUAD c;
  64.         for (y=0; y<h; y++) {
  65. for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
  66. jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[cmptno]);
  67. }
  68. for (x=0; x<w; x++){
  69. c.rgbRed   = (jas_matrix_getv(bufs[0], x));
  70. c.rgbGreen = (jas_matrix_getv(bufs[1], x));
  71. c.rgbBlue  = (jas_matrix_getv(bufs[2], x));
  72. SetPixelColor(x,h-1-y,c);
  73. }
  74. }
  75. } else {
  76. info.nNumFrames = image->numcmpts_;
  77. if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){
  78. throw "wrong frame!";
  79. }
  80. for (cmptno=0; cmptno<=info.nFrame; cmptno++) {
  81. w = jas_image_cmptwidth(image,cmptno);
  82. h = jas_image_cmptheight(image,cmptno);
  83. depth = jas_image_cmptprec(image,cmptno);
  84. if (depth>8) depth=8;
  85. if(!Create(w,h,depth,imagetype))
  86. throw "Can't allocate memory";
  87. SetGrayPalette();
  88. for (y=0; y<h; y++) {
  89. jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[0]);
  90. for (x=0; x<w; x++){
  91. SetPixelIndex(x,h-1-y,(jas_matrix_getv(bufs[0], x)));
  92. }
  93. }
  94. }
  95. }
  96.   } catch (char *message) {
  97. strncpy(info.szLastError,message,255);
  98. error = 1;
  99.   }
  100. if (bufs) {
  101. for (i = 0; i < image->numcmpts_; ++i){ if (bufs[i]) jas_matrix_destroy(bufs[i]);}
  102. free(bufs);
  103. }
  104. jas_cleanup();
  105. if (image) jas_image_destroy(image);
  106. if (in) jas_stream_close(in);
  107. return (error==0);
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. #if CXIMAGE_SUPPORT_ENCODE
  111. ////////////////////////////////////////////////////////////////////////////////
  112. bool CxImageJAS::Encode(CxFile * hFile, DWORD imagetype)
  113. {
  114. if (EncodeSafeCheck(hFile)) return false;
  115. if (head.biClrUsed!=0 && !IsGrayScale()){
  116. strcpy(info.szLastError,"JasPer can save only RGB or GrayScale images");
  117. return false;
  118. }
  119. jas_image_t *image=0;
  120. jas_stream_t *out=0;
  121. jas_matrix_t *cmpts[3];
  122. long x,y,yflip,error=0;
  123. uint_fast16_t cmptno, numcmpts;
  124. jas_image_cmptparm_t cmptparms[3], *cmptparm;
  125.   try
  126.   {
  127. if (jas_init())
  128. throw "cannot initialize jasper";
  129. if (!(out = jas_stream_fdopen(0, "wb")))
  130. throw "error: cannot open standard output";
  131. CxFileJas src(hFile,out);
  132. numcmpts = head.biClrUsed==0 ? 3 : 1;
  133. for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) {
  134. cmptparm->tlx = 0;
  135. cmptparm->tly = 0;
  136. cmptparm->hstep = 1;
  137. cmptparm->vstep = 1;
  138. cmptparm->width = head.biWidth;
  139. cmptparm->height = head.biHeight;
  140. cmptparm->prec = 8;
  141. cmptparm->sgnd = false;
  142. }
  143. /* Create image object. */
  144. if (!(image = jas_image_create(numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN)))
  145. throw "error : jas_image_create";
  146. if (numcmpts == 3) {
  147. jas_image_setclrspc(image, JAS_CLRSPC_SRGB);
  148. jas_image_setcmpttype(image, 0,
  149.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
  150. jas_image_setcmpttype(image, 1,
  151.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
  152. jas_image_setcmpttype(image, 2,
  153.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
  154. } else {
  155. jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
  156. jas_image_setcmpttype(image, 0,
  157.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
  158. }
  159. for (x = 0; x < numcmpts; ++x) { cmpts[x] = 0; }
  160. /* Create temporary matrices to hold component data. */
  161. for (x = 0; x < numcmpts; ++x) {
  162. if (!(cmpts[x] = jas_matrix_create(1, head.biWidth))) {
  163. throw "error : can't allocate memory";
  164. }
  165. }
  166. RGBQUAD c;
  167. for (y = 0; y < head.biHeight; ++y) {
  168. for (x = 0; x < head.biWidth; ++x) {
  169. if (head.biClrUsed==0){
  170. c = GetPixelColor(x,y);
  171. jas_matrix_setv(cmpts[0], x, c.rgbRed);
  172. jas_matrix_setv(cmpts[1], x, c.rgbGreen);
  173. jas_matrix_setv(cmpts[2], x, c.rgbBlue);
  174. } else {
  175. jas_matrix_setv(cmpts[0], x, GetPixelIndex(x,y));
  176. }
  177. }
  178. yflip = head.biHeight - 1 - y;
  179. for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
  180. if (jas_image_writecmpt(image, cmptno, 0, yflip, head.biWidth, 1, cmpts[cmptno])) {
  181. throw "error : jas_image_writecmpt";
  182. }
  183. }
  184. }
  185.  char szfmt[4];
  186. *szfmt = '';
  187. #if CXIMAGE_SUPPORT_JP2
  188. if (imagetype == CXIMAGE_FORMAT_JP2) strcpy(szfmt,"jp2");
  189. #endif
  190. #if CXIMAGE_SUPPORT_JPC
  191. if (imagetype == CXIMAGE_FORMAT_JPC) strcpy(szfmt,"jpc");
  192. #endif
  193. #if CXIMAGE_SUPPORT_RAS
  194. if (imagetype == CXIMAGE_FORMAT_RAS) strcpy(szfmt,"ras");
  195. #endif
  196. #if CXIMAGE_SUPPORT_PNM
  197. if (imagetype == CXIMAGE_FORMAT_PNM) strcpy(szfmt,"pnm");
  198. #endif
  199. #if CXIMAGE_SUPPORT_PGX
  200. if (imagetype == CXIMAGE_FORMAT_PGX){
  201. strcpy(szfmt,"pgx");
  202. if (head.biClrUsed==0) throw "PGX can save only GrayScale images";
  203. }
  204. #endif
  205. int outfmt = jas_image_strtofmt(szfmt);
  206. char szoutopts[16];
  207. sprintf(szoutopts,"rate=%.3f", info.nQuality/100.0f);
  208. if (jas_image_encode(image, out, outfmt, szoutopts)) {
  209. throw "error: cannot encode imagen";
  210. }
  211. jas_stream_flush(out);
  212.   } catch (char *message) {
  213. strncpy(info.szLastError,message,255);
  214. error = 1;
  215.   }
  216. for (x = 0; x < numcmpts; ++x) { if (cmpts[x]) { jas_matrix_destroy(cmpts[x]); } }
  217. jas_cleanup();
  218. if (image) jas_image_destroy(image);
  219. if (out) jas_stream_close(out);
  220. return (error==0);
  221. }
  222. ////////////////////////////////////////////////////////////////////////////////
  223. #endif // CXIMAGE_SUPPORT_ENCODE
  224. ////////////////////////////////////////////////////////////////////////////////
  225. #endif // CXIMAGE_SUPPORT_JASPER