Show
Ignore:
Timestamp:
07/17/09 09:47:48 (5 years ago)
Author:
robert
Message:

From J.P. Delport, "attached a modified jpeg plugin that allows writing of grayscale images."

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/jpeg/ReaderWriterJPEG.cpp

    r9637 r10496  
    620620class ReaderWriterJPEG : public osgDB::ReaderWriter 
    621621{ 
    622      
    623         WriteResult::WriteStatus write_JPEG_file (std::ostream &fout,int image_width,int image_height,JSAMPLE* image_buffer,int quality = 100) const 
    624         { 
     622 
     623        WriteResult::WriteStatus write_JPEG_file (std::ostream &fout, const osg::Image &img, int quality = 100) const 
     624        { 
     625            int image_width = img.s(); 
     626            int image_height = img.t(); 
    625627            if ( (image_width == 0) || (image_height == 0) ) 
    626628                return WriteResult::ERROR_IN_WRITING_FILE; 
     629 
     630            J_COLOR_SPACE image_color_space = JCS_RGB; 
     631            int image_components = 3; 
     632            // Only cater for gray, alpha and RGB for now 
     633            switch(img.getPixelFormat()) { 
     634              case(GL_LUMINANCE): 
     635              case(GL_ALPHA): { 
     636                  image_color_space = JCS_GRAYSCALE; 
     637                  image_components = 1; 
     638                  break; 
     639              } 
     640              case(GL_RGB): { 
     641                  image_color_space = JCS_RGB; 
     642                  image_components = 3; 
     643                  break; 
     644              } 
     645              default: return WriteResult::ERROR_IN_WRITING_FILE; break;               
     646            } 
     647 
     648            JSAMPLE* image_buffer = (JSAMPLE*)(img.data()); 
    627649 
    628650            /* This struct contains the JPEG compression parameters and pointers to 
     
    681703            cinfo.image_width = image_width;     /* image width and height, in pixels */ 
    682704            cinfo.image_height = image_height; 
    683             cinfo.input_components = 3;        /* # of color components per pixel */ 
    684             cinfo.in_color_space = JCS_RGB;     /* colorspace of input image */ 
     705            cinfo.input_components = image_components;        /* # of color components per pixel */ 
     706            cinfo.in_color_space = image_color_space;     /* colorspace of input image */ 
    685707            /* Now use the library's routine to set default compression parameters. 
    686708            * (You must set at least cinfo.in_color_space before calling this, 
     
    708730            * more if you wish, though. 
    709731            */ 
    710             row_stride = image_width * 3;    /* JSAMPLEs per row in image_buffer */ 
     732            row_stride = image_width * image_components;    /* JSAMPLEs per row in image_buffer */ 
    711733 
    712734            while (cinfo.next_scanline < cinfo.image_height) 
     
    834856            osg::ref_ptr<osg::Image> tmp_img = new osg::Image(img); 
    835857            tmp_img->flipVertical(); 
    836             WriteResult::WriteStatus ws = write_JPEG_file(fout,img.s(),img.t(),(JSAMPLE*)(tmp_img->data()),getQuality(options)); 
     858            WriteResult::WriteStatus ws = write_JPEG_file(fout, *(tmp_img.get()), getQuality(options)); 
    837859            return ws; 
    838860        }