Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmJpeg2000.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/11/28 15:20:33 $
00007   Version:   $Revision: 1.34 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 #include "gdcmFileHelper.h"
00019 #include "gdcmDebug.h"
00020 
00021 #include <iostream>
00022 #include <fstream>
00023 
00024 extern "C" {
00025   #include <openjpeg.h>
00026 }
00027 
00028 namespace gdcm 
00029 {
00030 //-----------------------------------------------------------------------------
00039 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
00040 {
00041    j2k_image_t img;
00042    j2k_cp_t cp;
00043  
00044    // default blindly copied
00045    cp.layer=0;
00046    cp.reduce=0;
00047    cp.decod_format=-1;
00048    cp.cod_format=-1;
00049  
00050    cp.cod_format=J2K_CFMT;
00051    cp.decod_format = PGX_DFMT;
00052    int len = inputlength;
00053    unsigned char *src = (unsigned char*)inputdata;
00054  
00055    // Decompression
00056    if (!j2k_decode(src, len, &img, &cp))
00057    {
00058       gdcmStaticErrorMacro( "ERROR -> j2k_to_image: failed to decode image!" );
00059       return false;
00060    }
00061  
00062    // Copy buffer
00063    for (int compno = 0; compno < img.numcomps; compno++)
00064    {
00065       j2k_comp_t *comp = &img.comps[compno];
00066   
00067       int w = img.comps[compno].w;
00068       int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
00069   
00070       //int h = img.comps[compno].h;
00071       int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
00072   
00073       if (comp->prec <= 8)
00074       {
00075          uint8_t *data8 = (uint8_t*)raw;
00076          for (int i = 0; i < wr * hr; i++) 
00077          {
00078             int v = img.comps[compno].data[i / wr * w + i % wr];
00079             *data8++ = (uint8_t)v;
00080          }
00081       }
00082       else if (comp->prec <= 16)
00083       {
00084          uint16_t *data16 = (uint16_t*)raw;
00085          for (int i = 0; i < wr * hr; i++) 
00086          {
00087             int v = img.comps[compno].data[i / wr * w + i % wr];
00088             *data16++ = (uint16_t)v;
00089          }
00090       }
00091       else
00092       {
00093          uint32_t *data32 = (uint32_t*)raw;
00094          for (int i = 0; i < wr * hr; i++) 
00095          {
00096             int v = img.comps[compno].data[i / wr * w + i % wr];
00097             *data32++ = (uint32_t)v;
00098          }
00099       }
00100       free(img.comps[compno].data);
00101    }
00102  
00103    // Free remaining structures
00104    j2k_dec_release();
00105    // FIXME
00106    delete[] inputdata;
00107  
00108    return true;
00109 }
00110 
00111 #if 0
00112 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
00113 {
00114 #if 0
00115   std::cerr << "Inputlenght=" << inputlength << std::endl;
00116   std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
00117   out.write((char*)inputdata,inputlength);
00118   out.close();
00119 #endif
00120   jas_init(); //important...
00121   jas_stream_t *jasStream = 
00122     jas_stream_memopen((char *)inputdata, inputlength);
00123     
00124   int fmtid;
00125   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
00126     {
00127     gdcmErrorMacro("unknown image format");
00128     return false;
00129     }
00130 
00131   // Decode the image. 
00132   jas_image_t *jasImage /* = NULL*/; // Useless assignation
00133   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
00134     {
00135     gdcmErrorMacro("cannot decode image");
00136     return false;
00137     }
00138 
00139   // close the stream. 
00140   jas_stream_close(jasStream);
00141   int numcmpts = jas_image_numcmpts(jasImage);
00142   int width = jas_image_cmptwidth(jasImage, 0);
00143   int height = jas_image_cmptheight(jasImage, 0);
00144   int prec = jas_image_cmptprec(jasImage, 0);
00145   int i, j, k;
00146 
00147   // The following should serioulsy be rewritten I cannot believe we need to
00148   // do a per pixel decompression, there should be a way to read a full
00149   // scanline...
00150   if (prec == 8)
00151     {
00152     uint8_t *data8 = (uint8_t*)raw;
00153     for ( i = 0; i < height; i++)
00154       for ( j = 0; j < width; j++)
00155         for ( k= 0; k < numcmpts; k++)
00156           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00157     }
00158   else if (prec <= 16)
00159     {
00160     uint16_t *data16 = (uint16_t*)raw;
00161     for ( i = 0; i < height; i++) 
00162       for ( j = 0; j < width; j++) 
00163         for ( k= 0; k < numcmpts; k++)
00164           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00165     }
00166   else if (prec <= 32)
00167     {
00168     uint32_t *data32 = (uint32_t*)raw;
00169     for ( i = 0; i < height; i++) 
00170       for ( j = 0; j < width; j++) 
00171         for ( k= 0; k < numcmpts; k++)
00172           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00173     }
00174 
00175   jas_image_destroy(jasImage);
00176   jas_image_clearfmts();
00177 
00178   //FIXME
00179   //delete the jpeg temp buffer
00180 #if 0
00181   std::ofstream rawout("/tmp/jpeg2000.raw");
00182   rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
00183   rawout.close();
00184 #endif
00185   delete[] inputdata;
00186 
00187   return true;
00188 }
00189 #endif
00190 
00191 //-----------------------------------------------------------------------------
00192 } // end namespace gdcm
00193 

Generated on Fri Jan 20 10:14:25 2006 for gdcm by  doxygen 1.4.4