From: DRC Date: Fri, 23 Mar 2012 19:47:57 +0000 (+0000) Subject: Emulate colorspace extensions if they are not present in the libjpeg API. This allow... X-Git-Tag: 1.2.90~66 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=afc06929e0a9cf64bdf50da30326076235df7b4f;p=libjpeg-turbo Emulate colorspace extensions if they are not present in the libjpeg API. This allows the TurboJPEG wrapper to be used with libjpeg rather than libjpeg-turbo. Not useful within the context of our project, but other projects prefer to include the TurboJPEG wrapper in-tree, and this allows them to be linked against either libjpeg-turbo or libjpeg. git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@825 632fc199-4ca6-4c93-a231-07263d6284db --- diff --git a/turbojpeg.c b/turbojpeg.c index c2c441a..a663509 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -172,12 +172,17 @@ static int setCompDefaults(struct jpeg_compress_struct *cinfo, cinfo->in_color_space=JCS_EXT_XBGR; break; #else case TJPF_RGB: - if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3) - { - cinfo->in_color_space=JCS_RGB; break; - } - default: - _throw("Unsupported pixel format"); + case TJPF_BGR: + case TJPF_RGBX: + case TJPF_BGRX: + case TJPF_XRGB: + case TJPF_XBGR: + case TJPF_RGBA: + case TJPF_BGRA: + case TJPF_ARGB: + case TJPF_ABGR: + cinfo->in_color_space=JCS_RGB; pixelFormat=TJPF_RGB; + break; #endif } @@ -201,9 +206,6 @@ static int setCompDefaults(struct jpeg_compress_struct *cinfo, cinfo->comp_info[1].v_samp_factor=1; cinfo->comp_info[2].v_samp_factor=1; - #if JCS_EXTENSIONS!=1 - bailout: - #endif return retval; } @@ -241,10 +243,16 @@ static int setDecompDefaults(struct jpeg_decompress_struct *dinfo, #endif #else case TJPF_RGB: - if(RGB_RED==0 && RGB_GREEN==1 && RGB_BLUE==2 && RGB_PIXELSIZE==3) - { - dinfo->out_color_space=JCS_RGB; break; - } + case TJPF_BGR: + case TJPF_RGBX: + case TJPF_BGRX: + case TJPF_XRGB: + case TJPF_XBGR: + case TJPF_RGBA: + case TJPF_BGRA: + case TJPF_ARGB: + case TJPF_ABGR: + dinfo->out_color_space=JCS_RGB; break; #endif default: _throw("Unsupported pixel format"); @@ -283,6 +291,149 @@ static int getSubsamp(j_decompress_ptr dinfo) } +#ifndef JCS_EXTENSIONS + +/* Conversion functions to emulate the colorspace extensions. This allows the + TurboJPEG wrapper to be used with libjpeg */ + +#define TORGB(PS, ROFFSET, GOFFSET, BOFFSET) { \ + int rowPad=pitch-width*PS; \ + while(height--) \ + { \ + unsigned char *endOfRow=src+width*PS; \ + while(srcinit&COMPRESS)==0) @@ -442,6 +596,16 @@ DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; + #ifndef JCS_EXTENSIONS + if(pixelFormat!=TJPF_GRAY) + { + rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE); + if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure"); + srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf); + pitch=width*RGB_PIXELSIZE; + } + #endif + cinfo->image_width=width; cinfo->image_height=height; @@ -474,6 +638,9 @@ DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf, bailout: if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); + #ifndef JCS_EXTENSIONS + if(rgbBuf && rgbBuf!=srcBuf) free(rgbBuf); + #endif if(row_pointer) free(row_pointer); return retval; } @@ -510,8 +677,11 @@ DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, JSAMPROW *outbuf[MAX_COMPONENTS]; int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS]; JSAMPLE *ptr=dstBuf; - unsigned long yuvsize=0; + unsigned long yuvsize=0; jpeg_component_info *compptr; + #ifndef JCS_EXTENSIONS + unsigned char *rgbBuf=NULL; + #endif getinstance(handle); if((this->init&COMPRESS)==0) @@ -537,6 +707,16 @@ DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, if(pitch==0) pitch=width*tjPixelSize[pixelFormat]; + #ifndef JCS_EXTENSIONS + if(pixelFormat!=TJPF_GRAY) + { + rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE); + if(!rgbBuf) _throw("tjEncodeYUV2(): Memory allocation failure"); + srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf); + pitch=width*RGB_PIXELSIZE; + } + #endif + cinfo->image_width=width; cinfo->image_height=height; @@ -619,6 +799,9 @@ DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf, bailout: if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo); + #ifndef JCS_EXTENSIONS + if(rgbBuf && rgbBuf!=srcBuf) free(rgbBuf); + #endif if(row_pointer) free(row_pointer); for(i=0; iinit&DECOMPRESS)==0) @@ -793,6 +980,21 @@ DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf, jpeg_start_decompress(dinfo); if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat]; + + #ifndef JCS_EXTENSIONS + if(pixelFormat!=TJPF_GRAY && + (RGB_RED!=tjRedOffset[pixelFormat] || + RGB_GREEN!=tjGreenOffset[pixelFormat] || + RGB_BLUE!=tjBlueOffset[pixelFormat] || + RGB_PIXELSIZE!=tjPixelSize[pixelFormat])) + { + rgbBuf=(unsigned char *)malloc(width*height*3); + if(!rgbBuf) _throw("tjDecompress2(): Memory allocation failure"); + _pitch=pitch; pitch=width*3; + _dstBuf=dstBuf; dstBuf=rgbBuf; + } + #endif + if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW) *dinfo->output_height))==NULL) _throw("tjDecompress2(): Memory allocation failure"); @@ -809,8 +1011,15 @@ DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf, } jpeg_finish_decompress(dinfo); + #ifndef JCS_EXTENSIONS + fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat); + #endif + bailout: if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo); + #ifndef JCS_EXTENSIONS + if(rgbBuf && rgbBuf!=dstBuf) free(rgbBuf); + #endif if(row_pointer) free(row_pointer); return retval; }