]> Creatis software - gdcm.git/commitdiff
ENH: Adding initial implementation for jasper/jpeg2000 support
authormalaterre <malaterre>
Sun, 22 May 2005 18:38:52 +0000 (18:38 +0000)
committermalaterre <malaterre>
Sun, 22 May 2005 18:38:52 +0000 (18:38 +0000)
src/CMakeLists.txt
src/gdcmJpeg2000.cxx
src/gdcmPixelReadConvert.cxx
src/gdcmmpeg2/CMakeLists.txt

index 0ff6cfb63366a1bf791d4486683e465ac4fe81b3..20d433f4a22c67cc2c7a071dac96dc7ee255b3a2 100644 (file)
@@ -13,6 +13,7 @@ ENDIF (WIN32)
 SUBDIRS(
   gdcmjpeg
   gdcmmpeg2
+  gdcmjasper
   )
 
 # "jpeglib.h" is defined here:
@@ -77,6 +78,7 @@ TARGET_LINK_LIBRARIES(gdcm
   gdcmjpeg12
   gdcmjpeg16
   gdcmmpeg2
+  gdcmjasper
 )
 IF(WIN32)
   IF(NOT BORLAND)
index 69f334b5ad2c86e1cc8afcaec349c154b19a898e..aa73b48a93344f32642e75e6ac345877e1f2b0a4 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/05 01:37:09 $
-  Version:   $Revision: 1.19 $
+  Date:      $Date: 2005/05/22 18:38:52 $
+  Version:   $Revision: 1.20 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -20,6 +20,7 @@
 
 #include <iostream>
 #include <fstream>
+#include <jasper/jasper.h>
 
 namespace gdcm 
 {
@@ -33,10 +34,80 @@ namespace gdcm
  * @warning : not yet made
  */
 
-bool gdcm_read_JPEG2000_file (std::ifstream* , void* )
+bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* raw, size_t inputlength)
 {
-   gdcmWarningMacro( "Sorry JPEG 2000 File not yet taken into account" );
-   return false;
+  jas_init(); //important...
+  // FIXME this is really ugly but it seems I have to load the complete
+  // jpeg2000 stream to use jasper:
+  uint8_t *inputdata = new uint8_t[inputlength];
+  //fp is already 'seek' to proper pos
+  fp->read((char*)inputdata, inputlength);
+  jas_stream_t *jasStream = jas_stream_memopen((char *)inputdata, inputlength);
+    
+  int fmtid;
+  if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
+    {
+    gdcmErrorMacro("unknown image format");
+    return false;
+    }
+
+  // Decode the image. 
+  jas_image_t *jasImage = NULL;
+  if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
+    {
+    gdcmErrorMacro("cannot decode image");
+    return false;
+    }
+
+  // close the stream. 
+  jas_stream_close(jasStream);
+  int numcmpts = jas_image_numcmpts(jasImage);
+  int width = jas_image_cmptwidth(jasImage, 0);
+  int height = jas_image_cmptheight(jasImage, 0);
+  int prec = jas_image_cmptprec(jasImage, 0);
+  int i, j, k;
+  char *fmtname = jas_image_fmttostr(fmtid);
+  printf("%s %d %d %d %d %ld\n", fmtname, numcmpts, width, height, prec, (long) jas_image_rawsize(jasImage));
+
+  // The following should serioulsy be rewritten I cannot belive we need to
+  // do a per pixel decompression, there should be a way to read a full
+  // scanline...
+  if (prec == 8)
+    {
+    uint8_t *data8 = (uint8_t*)raw;
+    for ( i = 0; i < height; i++)
+      for ( j = 0; j < width; j++)
+        for ( k= 0; k < numcmpts; k++)
+          *data8++ =
+            (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
+    }
+  else if (prec <= 16)
+    {
+    uint16_t *data16 = (uint16_t*)raw;
+    for ( i = 0; i < height; i++) 
+      for ( j = 0; j < width; j++) 
+        for ( k= 0; k < numcmpts; k++)
+          *data16++ = 
+            (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
+    }
+  else if (prec <= 32)
+    {
+    uint32_t *data32 = (uint32_t*)raw;
+    for ( i = 0; i < height; i++) 
+      for ( j = 0; j < width; j++) 
+        for ( k= 0; k < numcmpts; k++)
+          *data32++ = 
+            (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
+    }
+
+  jas_image_destroy(jasImage);
+  jas_image_clearfmts();
+
+  //FIXME
+  //delete the jpeg temp buffer
+  delete[] inputdata;
+
+  return true;
 }
 
 //-----------------------------------------------------------------------------
index 66bcbfdeaad67b93616528281824a9b9730fbe7a..38955fe7b2438ef2f2df9635ff42847c973e0879 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmPixelReadConvert.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/05/21 18:43:53 $
-  Version:   $Revision: 1.56 $
+  Date:      $Date: 2005/05/22 18:38:52 $
+  Version:   $Revision: 1.57 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -31,7 +31,8 @@
 namespace gdcm
 {
 
-extern bool ReadMPEGFile (std::ifstream *fp, void *image_buffer);
+bool ReadMPEGFile (std::ifstream *fp, void *image_buffer);
+bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* raw, size_t inputlength);
 //-----------------------------------------------------------------------------
 #define str2num(str, typeNum) *((typeNum *)(str))
 
@@ -396,9 +397,9 @@ bool PixelReadConvert::ReadAndDecompressJPEGFile( std::ifstream *fp )
 {
    if ( IsJPEG2000 )
    {
-      gdcmWarningMacro( "Sorry, JPEG2000 not yet taken into account" );
+//      gdcmWarningMacro( "Sorry, JPEG2000 not yet taken into account" );
       fp->seekg( JPEGInfo->GetFirstFragment()->GetOffset(), std::ios::beg);
-//    if ( ! gdcm_read_JPEG2000_file( fp,Raw ) )
+    if ( ! gdcm_read_JPEG2000_file( fp,Raw, JPEGInfo->GetFirstFragment()->GetLength() ) )
           return false;
    }
 
index 82feb09872a4aacdd8c48f9887b85763cde3d6cc..448420b4fa639dad1b8ce2faa15a0155f37135b1 100644 (file)
@@ -1 +1,3 @@
+PROJECT(MPEG2)
+
 SUBDIRS(src)