From ab777634db25fac59a4c96b1941067c7fceb1ad7 Mon Sep 17 00:00:00 2001 From: Steve Herrell Date: Wed, 1 Dec 2010 20:04:29 -0500 Subject: [PATCH] Added support to quick thumbnail code to load PPM embedded thumbnails. --- rtengine/imageio.cc | 25 +++++++++++++++++++++++++ rtengine/imageio.h | 1 + rtengine/rawimage.cc | 20 ++++++++++++++++++++ rtengine/rawimage.h | 5 +++++ rtengine/rtthumbnail.cc | 34 ++++++++++++++++++++++++++++++---- rtengine/rtthumbnail.h | 3 ++- 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/rtengine/imageio.cc b/rtengine/imageio.cc index 961362e95..12da83048 100644 --- a/rtengine/imageio.cc +++ b/rtengine/imageio.cc @@ -476,6 +476,31 @@ int ImageIO::loadTIFF (Glib::ustring fname) { return IMIO_SUCCESS; } +int ImageIO::loadPPMFromMemory(const char* buffer, int width, int height, bool swap, int bps) +{ + allocate (width, height); + + int line_length(width * 3 * (bps/8)); + + if ( swap && bps > 8 ) + { + char swapped[line_length]; + for ( int row = 0; row < height; ++row ) + { + ::swab(((char*)buffer) + (row * line_length),swapped,line_length); + setScanline(row,(unsigned char*)&swapped[0],bps); + } + } + else + { + for ( int row = 0; row < height; ++row ) + { + setScanline(row,((unsigned char*)buffer) + (row * line_length),bps); + } + } + + return IMIO_SUCCESS; +} int ImageIO::savePNG (Glib::ustring fname, int compression, int bps) { diff --git a/rtengine/imageio.h b/rtengine/imageio.h index a036060b0..df0414165 100644 --- a/rtengine/imageio.h +++ b/rtengine/imageio.h @@ -73,6 +73,7 @@ class ImageIO { int loadTIFF (Glib::ustring fname); int loadJPEGFromMemory (const char* buffer, int bufsize); + int loadPPMFromMemory(const char* buffer,int width,int height, bool swap, int bps); int savePNG (Glib::ustring fname, int compression = -1, int bps = -1); int saveJPEG (Glib::ustring fname, int quality = 100); diff --git a/rtengine/rawimage.cc b/rtengine/rawimage.cc index 6c2158464..3c8561f60 100644 --- a/rtengine/rawimage.cc +++ b/rtengine/rawimage.cc @@ -8,6 +8,11 @@ #include #include #include +#ifdef WIN32 +#include +#else +#include +#endif namespace rtengine{ @@ -242,4 +247,19 @@ unsigned short** RawImage::compress_image() return data; } +bool +RawImage::is_supportedThumb() const +{ + return ( (thumb_width * thumb_height) > 0 && + ( write_thumb == &rtengine::RawImage::jpeg_thumb || + write_thumb == &rtengine::RawImage::ppm_thumb || + thumb_load_raw == &rtengine::RawImage::kodak_thumb_load_raw )); +} + +bool +RawImage::get_thumbSwap() const +{ + return ((order == 0x4949) == (ntohs(0x1234) == 0x1234)) ? true : false; +} + }; //namespace rtengine diff --git a/rtengine/rawimage.h b/rtengine/rawimage.h index a95178298..05837b741 100644 --- a/rtengine/rawimage.h +++ b/rtengine/rawimage.h @@ -88,7 +88,12 @@ public: int get_profileLen() const {return profile_length;} char* get_profile() const { return profile_data;} IMFILE *get_file() { return ifp; } + bool is_supportedThumb() const ; int get_thumbOffset(){ return int(thumb_offset);} + int get_thumbWidth(){ return int(thumb_width);} + int get_thumbHeight(){ return int(thumb_height);} + int get_thumbBPS(){ return thumb_load_raw ? 16 : 8; } + bool get_thumbSwap() const; unsigned get_thumbLength(){ return thumb_length;} public: // dcraw functions diff --git a/rtengine/rtthumbnail.cc b/rtengine/rtthumbnail.cc index 7df6ff108..a26b12cbd 100644 --- a/rtengine/rtthumbnail.cc +++ b/rtengine/rtthumbnail.cc @@ -49,9 +49,20 @@ my_jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile); namespace rtengine { -Thumbnail* Thumbnail::loadFromMemory (const char* image, int length, int &w, int &h, int fixwh) { +Thumbnail* Thumbnail::loadFromMemory (const char* image, int length, int &w, int &h, int fixwh, + bool swap_order, int bps) { Image16* img = new Image16 (); - int err = img->loadJPEGFromMemory(image,length); + int err = 1; + + if ( (unsigned char)image[1] == 0xd8 ) + { + err = img->loadJPEGFromMemory(image,length); + } + else + { + err = img->loadPPMFromMemory(image,w,h,swap_order,bps); + } + if (err) { printf("loadfromMemory: error\n"); delete img; @@ -201,16 +212,31 @@ Thumbnail* Thumbnail::loadQuickFromRaw (const Glib::ustring& fname, RawMetaDataL { RawImage *ri= new RawImage(fname); int r = ri->loadRaw(false,false); - if( r ) return NULL; + if( r ) + { + delete ri; + return NULL; + } rml.exifBase = ri->get_exifBase(); rml.ciffBase = ri->get_ciffBase(); rml.ciffLength = ri->get_ciffLen(); - Thumbnail* tpp = Thumbnail::loadFromMemory((const char*)fdata(ri->get_thumbOffset(),ri->get_file()),ri->get_thumbLength(),w,h,fixwh); + rtengine::Thumbnail* tpp = 0; + + // see if it is something we support + if ( ri->is_supportedThumb() ) + { + w = ri->get_thumbWidth(); + h = ri->get_thumbHeight(); + tpp = rtengine::Thumbnail::loadFromMemory((const char*)fdata(ri->get_thumbOffset(),ri->get_file()),ri->get_thumbLength(), + w,h,fixwh, + ri->get_thumbSwap(),ri->get_thumbBPS()); + } if ( tpp == 0 ) { + delete ri; printf("DCRAW: failed4\n"); return NULL; } diff --git a/rtengine/rtthumbnail.h b/rtengine/rtthumbnail.h index 438fe2e04..8dcc709a5 100644 --- a/rtengine/rtthumbnail.h +++ b/rtengine/rtthumbnail.h @@ -75,7 +75,8 @@ namespace rtengine { void getFinalSize (const rtengine::procparams::ProcParams& pparams, int& w, int& h); static Thumbnail* loadQuickFromRaw (const Glib::ustring& fname, rtengine::RawMetaDataLocation& rml, int &w, int &h, int fixwh); - static Thumbnail* loadFromMemory (const char* image, int length, int &w, int &h, int fixwh); + static Thumbnail* loadFromMemory (const char* image, int length, int &w, int &h, int fixwh, + bool swap_order, int bps); static Thumbnail* loadFromRaw (const Glib::ustring& fname, RawMetaDataLocation& rml, int &w, int &h, int fixwh); static Thumbnail* loadFromImage (const Glib::ustring& fname, int &w, int &h, int fixwh);