Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15002
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 47
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-26 10:07
文章分类

全部博文(7)

分类: C/C++

2014-02-26 14:26:48

记录下来 备忘

点击(此处)折叠或打开

  1. /*
  2.  * Copyright (C)2009-2012 D. R. Commander. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  * - Redistributions of source code must retain the above copyright notice,
  8.  * this list of conditions and the following disclaimer.
  9.  * - Redistributions in binary form must reproduce the above copyright notice,
  10.  * this list of conditions and the following disclaimer in the documentation
  11.  * and/or other materials provided with the distribution.
  12.  * - Neither the name of the libjpeg-turbo Project nor the names of its
  13.  * contributors may be used to endorse or promote products derived from this
  14.  * software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
  17.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  20.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26.  * POSSIBILITY OF SUCH DAMAGE.
  27.  */

  28. /* TurboJPEG/OSS: this implements the TurboJPEG API using libjpeg-turbo */

  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <jinclude.h>
  32. #define JPEG_INTERNALS
  33. #include <jpeglib.h>
  34. #include <jerror.h>
  35. #include <setjmp.h>
  36. #include "./turbojpeg.h"
  37. #include "./tjutil.h"
  38. #include "transupp.h"

  39. extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **,
  40.     unsigned long *, boolean);
  41. extern void jpeg_mem_src_tj(j_decompress_ptr, unsigned char *, unsigned long);

  42. #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))


  43. /* Error handling (based on example in example.c) */

  44. static char errStr[JMSG_LENGTH_MAX]="No error";

  45. struct my_error_mgr
  46. {
  47.     struct jpeg_error_mgr pub;
  48.     jmp_buf setjmp_buffer;
  49. };
  50. typedef struct my_error_mgr *my_error_ptr;

  51. static void my_error_exit(j_common_ptr cinfo)
  52. {
  53.     my_error_ptr myerr=(my_error_ptr)cinfo->err;
  54.     (*cinfo->err->output_message)(cinfo);
  55.     longjmp(myerr->setjmp_buffer, 1);
  56. }

  57. /* Based on output_message() in jerror.c */

  58. static void my_output_message(j_common_ptr cinfo)
  59. {
  60.     (*cinfo->err->format_message)(cinfo, errStr);
  61. }


  62. /* Global structures, macros, etc. */

  63. enum {COMPRESS=1, DECOMPRESS=2};

  64. typedef struct _tjinstance
  65. {
  66.     struct jpeg_compress_struct cinfo;
  67.     struct jpeg_decompress_struct dinfo;
  68.     struct my_error_mgr jerr;
  69.     int init;
  70. } tjinstance;

  71. static const int pixelsize[TJ_NUMSAMP]={3, 3, 3, 1, 3};

  72. static const JXFORM_CODE xformtypes[TJ_NUMXOP]=
  73. {
  74.     JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
  75.     JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
  76. };

  77. #define NUMSF 16
  78. static const tjscalingfactor sf[NUMSF]={
  79.     {2, 1},
  80.     {15, 8},
  81.     {7, 4},
  82.     {13, 8},
  83.     {3, 2},
  84.     {11, 8},
  85.     {5, 4},
  86.     {9, 8},
  87.     {1, 1},
  88.     {7, 8},
  89.     {3, 4},
  90.     {5, 8},
  91.     {1, 2},
  92.     {3, 8},
  93.     {1, 4},
  94.     {1, 8}
  95. };

  96. #define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
  97.     retval=-1; goto bailout;}
  98. #define getinstance(handle) tjinstance *this=(tjinstance *)handle; \
  99.     j_compress_ptr cinfo=NULL; j_decompress_ptr dinfo=NULL; \
  100.     if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
  101.         return -1;} \
  102.     cinfo=&this->cinfo; dinfo=&this->dinfo;

  103. static int getPixelFormat(int pixelSize, int flags)
  104. {
  105.     if(pixelSize==1) return TJPF_GRAY;
  106.     if(pixelSize==3)
  107.     {
  108.         if(flags&TJ_BGR) return TJPF_BGR;
  109.         else return TJPF_RGB;
  110.     }
  111.     if(pixelSize==4)
  112.     {
  113.         if(flags&TJ_ALPHAFIRST)
  114.         {
  115.             if(flags&TJ_BGR) return TJPF_XBGR;
  116.             else return TJPF_XRGB;
  117.         }
  118.         else
  119.         {
  120.             if(flags&TJ_BGR) return TJPF_BGRX;
  121.             else return TJPF_RGBX;
  122.         }
  123.     }
  124.     return -1;
  125. }

  126. static int setCompDefaults(struct jpeg_compress_struct *cinfo,
  127.     int pixelFormat, int subsamp, int jpegQual, int flags)
  128. {
  129.     int retval=0;

  130.     switch(pixelFormat)
  131.     {
  132.         case TJPF_GRAY:
  133.             cinfo->in_color_space=JCS_GRAYSCALE; break;
  134.         #if JCS_EXTENSIONS==1
  135.         case TJPF_RGB:
  136.             cinfo->in_color_space=JCS_EXT_RGB; break;
  137.         case TJPF_BGR:
  138.             cinfo->in_color_space=JCS_EXT_BGR; break;
  139.         case TJPF_RGBX:
  140.         case TJPF_RGBA:
  141.             cinfo->in_color_space=JCS_EXT_RGBX; break;
  142.         case TJPF_BGRX:
  143.         case TJPF_BGRA:
  144.             cinfo->in_color_space=JCS_EXT_BGRX; break;
  145.         case TJPF_XRGB:
  146.         case TJPF_ARGB:
  147.             cinfo->in_color_space=JCS_EXT_XRGB; break;
  148.         case TJPF_XBGR:
  149.         case TJPF_ABGR:
  150.             cinfo->in_color_space=JCS_EXT_XBGR; break;
  151.         #else
  152.         case TJPF_RGB:
  153.         case TJPF_BGR:
  154.         case TJPF_RGBX:
  155.         case TJPF_BGRX:
  156.         case TJPF_XRGB:
  157.         case TJPF_XBGR:
  158.         case TJPF_RGBA:
  159.         case TJPF_BGRA:
  160.         case TJPF_ARGB:
  161.         case TJPF_ABGR:
  162.             cinfo->in_color_space=JCS_RGB; pixelFormat=TJPF_RGB;
  163.             break;
  164.         #endif
  165.     }

  166.     cinfo->input_components=tjPixelSize[pixelFormat];
  167.     jpeg_set_defaults(cinfo);
  168.     if(jpegQual>=0)
  169.     {
  170.         jpeg_set_quality(cinfo, jpegQual, TRUE);
  171.         if(jpegQual>=96 || flags&TJFLAG_ACCURATEDCT) cinfo->dct_method=JDCT_ISLOW;
  172.         else cinfo->dct_method=JDCT_FASTEST;
  173.     }
  174.     if(subsamp==TJSAMP_GRAY)
  175.         jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  176.     else
  177.         jpeg_set_colorspace(cinfo, JCS_YCbCr);

  178.     cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8;
  179.     cinfo->comp_info[1].h_samp_factor=1;
  180.     cinfo->comp_info[2].h_samp_factor=1;
  181.     cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8;
  182.     cinfo->comp_info[1].v_samp_factor=1;
  183.     cinfo->comp_info[2].v_samp_factor=1;

  184.     return retval;
  185. }

  186. static int setDecompDefaults(struct jpeg_decompress_struct *dinfo,
  187.     int pixelFormat, int flags)
  188. {
  189.     int retval=0;

  190.     switch(pixelFormat)
  191.     {
  192.         case TJPF_GRAY:
  193.             dinfo->out_color_space=JCS_GRAYSCALE; break;
  194.         #if JCS_EXTENSIONS==1
  195.         case TJPF_RGB:
  196.             dinfo->out_color_space=JCS_EXT_RGB; break;
  197.         case TJPF_BGR:
  198.             dinfo->out_color_space=JCS_EXT_BGR; break;
  199.         case TJPF_RGBX:
  200.             dinfo->out_color_space=JCS_EXT_RGBX; break;
  201.         case TJPF_BGRX:
  202.             dinfo->out_color_space=JCS_EXT_BGRX; break;
  203.         case TJPF_XRGB:
  204.             dinfo->out_color_space=JCS_EXT_XRGB; break;
  205.         case TJPF_XBGR:
  206.             dinfo->out_color_space=JCS_EXT_XBGR; break;
  207.         #if JCS_ALPHA_EXTENSIONS==1
  208.         case TJPF_RGBA:
  209.             dinfo->out_color_space=JCS_EXT_RGBA; break;
  210.         case TJPF_BGRA:
  211.             dinfo->out_color_space=JCS_EXT_BGRA; break;
  212.         case TJPF_ARGB:
  213.             dinfo->out_color_space=JCS_EXT_ARGB; break;
  214.         case TJPF_ABGR:
  215.             dinfo->out_color_space=JCS_EXT_ABGR; break;
  216.         #endif
  217.         #else
  218.         case TJPF_RGB:
  219.         case TJPF_BGR:
  220.         case TJPF_RGBX:
  221.         case TJPF_BGRX:
  222.         case TJPF_XRGB:
  223.         case TJPF_XBGR:
  224.         case TJPF_RGBA:
  225.         case TJPF_BGRA:
  226.         case TJPF_ARGB:
  227.         case TJPF_ABGR:
  228.             dinfo->out_color_space=JCS_RGB; break;
  229.         #endif
  230.         default:
  231.             _throw("Unsupported pixel format");
  232.     }

  233.     if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;

  234.     bailout:
  235.     return retval;
  236. }


  237. static int getSubsamp(j_decompress_ptr dinfo)
  238. {
  239.     int retval=-1, i, k;
  240.     for(i=0; i<NUMSUBOPT; i++)
  241.     {
  242.         if(dinfo->num_components==pixelsize[i])
  243.         {
  244.             if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8
  245.                 && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8)
  246.             {
  247.                 int match=0;
  248.                 for(k=1; k<dinfo->num_components; k++)
  249.                 {
  250.                     if(dinfo->comp_info[k].h_samp_factor==1
  251.                         && dinfo->comp_info[k].v_samp_factor==1)
  252.                         match++;
  253.                 }
  254.                 if(match==dinfo->num_components-1)
  255.                 {
  256.                     retval=i; break;
  257.                 }
  258.             }
  259.         }
  260.     }
  261.     return retval;
  262. }


  263. #ifndef JCS_EXTENSIONS

  264. /* Conversion functions to emulate the colorspace extensions. This allows the
  265.    TurboJPEG wrapper to be used with libjpeg */

  266. #define TORGB(PS, ROFFSET, GOFFSET, BOFFSET) { \
  267.     int rowPad=pitch-width*PS; \
  268.     while(height--) \
  269.     { \
  270.         unsigned char *endOfRow=src+width*PS; \
  271.         while(src<endOfRow) \
  272.         { \
  273.             dst[RGB_RED]=src[ROFFSET]; \
  274.             dst[RGB_GREEN]=src[GOFFSET]; \
  275.             dst[RGB_BLUE]=src[BOFFSET]; \
  276.             dst+=RGB_PIXELSIZE; src+=PS; \
  277.         } \
  278.         src+=rowPad; \
  279.     } \
  280. }

  281. static unsigned char *toRGB(unsigned char *src, int width, int pitch,
  282.     int height, int pixelFormat, unsigned char *dst)
  283. {
  284.     unsigned char *retval=src;
  285.     switch(pixelFormat)
  286.     {
  287.         case TJPF_RGB:
  288.             #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  289.             retval=dst; TORGB(3, 0, 1, 2);
  290.             #endif
  291.             break;
  292.         case TJPF_BGR:
  293.             #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  294.             retval=dst; TORGB(3, 2, 1, 0);
  295.             #endif
  296.             break;
  297.         case TJPF_RGBX:
  298.         case TJPF_RGBA:
  299.             #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  300.             retval=dst; TORGB(4, 0, 1, 2);
  301.             #endif
  302.             break;
  303.         case TJPF_BGRX:
  304.         case TJPF_BGRA:
  305.             #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  306.             retval=dst; TORGB(4, 2, 1, 0);
  307.             #endif
  308.             break;
  309.         case TJPF_XRGB:
  310.         case TJPF_ARGB:
  311.             #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  312.             retval=dst; TORGB(4, 1, 2, 3);
  313.             #endif
  314.             break;
  315.         case TJPF_XBGR:
  316.         case TJPF_ABGR:
  317.             #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  318.             retval=dst; TORGB(4, 3, 2, 1);
  319.             #endif
  320.             break;
  321.     }
  322.     return retval;
  323. }

  324. #define FROMRGB(PS, ROFFSET, GOFFSET, BOFFSET, SETALPHA) { \
  325.     int rowPad=pitch-width*PS; \
  326.     while(height--) \
  327.     { \
  328.         unsigned char *endOfRow=dst+width*PS; \
  329.         while(dst<endOfRow) \
  330.         { \
  331.             dst[ROFFSET]=src[RGB_RED]; \
  332.             dst[GOFFSET]=src[RGB_GREEN]; \
  333.             dst[BOFFSET]=src[RGB_BLUE]; \
  334.             SETALPHA \
  335.             dst+=PS; src+=RGB_PIXELSIZE; \
  336.         } \
  337.         dst+=rowPad; \
  338.     } \
  339. }

  340. static void fromRGB(unsigned char *src, unsigned char *dst, int width,
  341.     int pitch, int height, int pixelFormat)
  342. {
  343.     switch(pixelFormat)
  344.     {
  345.         case TJPF_RGB:
  346.             #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  347.             FROMRGB(3, 0, 1, 2,);
  348.             #endif
  349.             break;
  350.         case TJPF_BGR:
  351.             #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  352.             FROMRGB(3, 2, 1, 0,);
  353.             #endif
  354.             break;
  355.         case TJPF_RGBX:
  356.             #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  357.             FROMRGB(4, 0, 1, 2,);
  358.             #endif
  359.             break;
  360.         case TJPF_RGBA:
  361.             #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  362.             FROMRGB(4, 0, 1, 2, dst[3]=0xFF;);
  363.             #endif
  364.             break;
  365.         case TJPF_BGRX:
  366.             #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  367.             FROMRGB(4, 2, 1, 0,);
  368.             #endif
  369.             break;
  370.         case TJPF_BGRA:
  371.             #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  372.             FROMRGB(4, 2, 1, 0, dst[3]=0xFF;); return;
  373.             #endif
  374.             break;
  375.         case TJPF_XRGB:
  376.             #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  377.             FROMRGB(4, 1, 2, 3,); return;
  378.             #endif
  379.             break;
  380.         case TJPF_ARGB:
  381.             #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  382.             FROMRGB(4, 1, 2, 3, dst[0]=0xFF;); return;
  383.             #endif
  384.             break;
  385.         case TJPF_XBGR:
  386.             #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  387.             FROMRGB(4, 3, 2, 1,); return;
  388.             #endif
  389.             break;
  390.         case TJPF_ABGR:
  391.             #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  392.             FROMRGB(4, 3, 2, 1, dst[0]=0xFF;); return;
  393.             #endif
  394.             break;
  395.     }
  396. }

  397. #endif


  398. /* General API functions */

  399. DLLEXPORT char* DLLCALL tjGetErrorStr(void)
  400. {
  401.     return errStr;
  402. }


  403. DLLEXPORT int DLLCALL tjDestroy(tjhandle handle)
  404. {
  405.     getinstance(handle);
  406.     if(setjmp(this->jerr.setjmp_buffer)) return -1;
  407.     if(this->init&COMPRESS) jpeg_destroy_compress(cinfo);
  408.     if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo);
  409.     free(this);
  410.     return 0;
  411. }


  412. /* These are exposed mainly because Windows can't malloc() and free() across
  413.    DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL
  414.    with turbojpeg.dll for compatibility reasons. However, these functions
  415.    can potentially be used for other purposes by different implementations. */

  416. DLLEXPORT void DLLCALL tjFree(unsigned char *buf)
  417. {
  418.     if(buf) free(buf);
  419. }


  420. DLLEXPORT unsigned char *DLLCALL tjAlloc(int bytes)
  421. {
  422.     return (unsigned char *)malloc(bytes);
  423. }


  424. /* Compressor */

  425. static tjhandle _tjInitCompress(tjinstance *this)
  426. {
  427.     unsigned char buffer[1], *buf=buffer; unsigned long size=1;

  428.     /* This is also straight out of example.c */
  429.     this->cinfo.err=jpeg_std_error(&this->jerr.pub);
  430.     this->jerr.pub.error_exit=my_error_exit;
  431.     this->jerr.pub.output_message=my_output_message;

  432.     if(setjmp(this->jerr.setjmp_buffer))
  433.     {
  434.         /* If we get here, the JPEG code has signaled an error. */
  435.         if(this) free(this); return NULL;
  436.     }

  437.     jpeg_create_compress(&this->cinfo);
  438.     /* Make an initial call so it will create the destination manager */
  439.     jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);

  440.     this->init|=COMPRESS;
  441.     return (tjhandle)this;
  442. }

  443. DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
  444. {
  445.     tjinstance *this=NULL;
  446.     if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  447.     {
  448.         snprintf(errStr, JMSG_LENGTH_MAX,
  449.             "tjInitCompress(): Memory allocation failure");
  450.         return NULL;
  451.     }
  452.     MEMZERO(this, sizeof(tjinstance));
  453.     return _tjInitCompress(this);
  454. }


  455. DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
  456.     int jpegSubsamp)
  457. {
  458.     unsigned long retval=0; int mcuw, mcuh, chromasf;
  459.     if(width<1 || height<1 || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT)
  460.         _throw("tjBufSize(): Invalid argument");

  461.     // This allows for rare corner cases in which a JPEG image can actually be
  462.     // larger than the uncompressed input (we wouldn't mention it if it hadn't
  463.     // happened before.)
  464.     mcuw=tjMCUWidth[jpegSubsamp];
  465.     mcuh=tjMCUHeight[jpegSubsamp];
  466.     chromasf=jpegSubsamp==TJSAMP_GRAY? 0: 4*64/(mcuw*mcuh);
  467.     retval=PAD(width, mcuw) * PAD(height, mcuh) * (2 + chromasf) + 2048;

  468.     bailout:
  469.     return retval;
  470. }


  471. DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
  472. {
  473.     unsigned long retval=0;
  474.     if(width<1 || height<1)
  475.         _throw("TJBUFSIZE(): Invalid argument");

  476.     // This allows for rare corner cases in which a JPEG image can actually be
  477.     // larger than the uncompressed input (we wouldn't mention it if it hadn't
  478.     // happened before.)
  479.     retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048;

  480.     bailout:
  481.     return retval;
  482. }


  483. DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
  484.     int subsamp)
  485. {
  486.     unsigned long retval=0;
  487.     int pw, ph, cw, ch;
  488.     if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT)
  489.         _throw("tjBufSizeYUV(): Invalid argument");
  490.     pw=PAD(width, tjMCUWidth[subsamp]/8);
  491.     ph=PAD(height, tjMCUHeight[subsamp]/8);
  492.     cw=pw*8/tjMCUWidth[subsamp]; ch=ph*8/tjMCUHeight[subsamp];
  493.     retval=PAD(pw, 4)*ph + (subsamp==TJSAMP_GRAY? 0:PAD(cw, 4)*ch*2);

  494.     bailout:
  495.     return retval;
  496. }


  497. DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
  498.     int subsamp)
  499. {
  500.     return tjBufSizeYUV(width, height, subsamp);
  501. }


  502. DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
  503.     int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
  504.     unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  505. {
  506.     int i, retval=0, alloc=1; JSAMPROW *row_pointer=NULL;
  507.     #ifndef JCS_EXTENSIONS
  508.     unsigned char *rgbBuf=NULL;
  509.     #endif

  510.     getinstance(handle)
  511.     if((this->init&COMPRESS)==0)
  512.         _throw("tjCompress2(): Instance has not been initialized for compression");

  513.     if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  514.         || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL
  515.         || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100)
  516.         _throw("tjCompress2(): Invalid argument");

  517.     if(setjmp(this->jerr.setjmp_buffer))
  518.     {
  519.         /* If we get here, the JPEG code has signaled an error. */
  520.         retval=-1;
  521.         goto bailout;
  522.     }

  523.     if(pitch==0) pitch=width*tjPixelSize[pixelFormat];

  524.     #ifndef JCS_EXTENSIONS
  525.     if(pixelFormat!=TJPF_GRAY)
  526.     {
  527.         rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  528.         if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure");
  529.         srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  530.         pitch=width*RGB_PIXELSIZE;
  531.     }
  532.     #endif

  533.     cinfo->image_width=width;
  534.     cinfo->image_height=height;

  535.     if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  536.     else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  537.     else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

  538.     if(flags&TJFLAG_NOREALLOC)
  539.     {
  540.         alloc=0; *jpegSize=tjBufSize(width, height, jpegSubsamp);
  541.     }
  542.     jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
  543.     if(setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags)==-1)
  544.         return -1;

  545.     jpeg_start_compress(cinfo, TRUE);
  546.     if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
  547.         _throw("tjCompress2(): Memory allocation failure");
  548.     for(i=0; i<height; i++)
  549.     {
  550.         if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
  551.         else row_pointer[i]=&srcBuf[i*pitch];
  552.     }
  553.     while(cinfo->next_scanline<cinfo->image_height)
  554.     {
  555.         jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
  556.             cinfo->image_height-cinfo->next_scanline);
  557.     }
  558.     jpeg_finish_compress(cinfo);

  559.     bailout:
  560.     if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  561.     #ifndef JCS_EXTENSIONS
  562.     if(rgbBuf) free(rgbBuf);
  563.     #endif
  564.     if(row_pointer) free(row_pointer);
  565.     return retval;
  566. }

  567. DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
  568.     int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf,
  569.     unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  570. {
  571.     int retval=0; unsigned long size;
  572.     if(flags&TJ_YUV)
  573.     {
  574.         size=tjBufSizeYUV(width, height, jpegSubsamp);
  575.         retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  576.             getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags);
  577.     }
  578.     else
  579.     {
  580.         retval=tjCompress2(handle, srcBuf, width, pitch, height,
  581.             getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual,
  582.             flags|TJFLAG_NOREALLOC);
  583.     }
  584.     *jpegSize=size;
  585.     return retval;
  586. }


  587. DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf,
  588.     int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf,
  589.     int subsamp, int flags)
  590. {
  591.     int i, retval=0; JSAMPROW *row_pointer=NULL;
  592.     JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
  593.     JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
  594.     JSAMPROW *outbuf[MAX_COMPONENTS];
  595.     int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS];
  596.     JSAMPLE *ptr=dstBuf;
  597.     unsigned long yuvsize=0;
  598.     jpeg_component_info *compptr;
  599.     #ifndef JCS_EXTENSIONS
  600.     unsigned char *rgbBuf=NULL;
  601.     #endif

  602.     getinstance(handle);
  603.     if((this->init&COMPRESS)==0)
  604.         _throw("tjEncodeYUV2(): Instance has not been initialized for compression");

  605.     for(i=0; i<MAX_COMPONENTS; i++)
  606.     {
  607.         tmpbuf[i]=NULL; _tmpbuf[i]=NULL;
  608.         tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL;
  609.     }

  610.     if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  611.         || pixelFormat>=TJ_NUMPF || dstBuf==NULL || subsamp<0
  612.         || subsamp>=NUMSUBOPT)
  613.         _throw("tjEncodeYUV2(): Invalid argument");

  614.     if(setjmp(this->jerr.setjmp_buffer))
  615.     {
  616.         /* If we get here, the JPEG code has signaled an error. */
  617.         retval=-1;
  618.         goto bailout;
  619.     }

  620.     if(pitch==0) pitch=width*tjPixelSize[pixelFormat];

  621.     #ifndef JCS_EXTENSIONS
  622.     if(pixelFormat!=TJPF_GRAY)
  623.     {
  624.         rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  625.         if(!rgbBuf) _throw("tjEncodeYUV2(): Memory allocation failure");
  626.         srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  627.         pitch=width*RGB_PIXELSIZE;
  628.     }
  629.     #endif

  630.     cinfo->image_width=width;
  631.     cinfo->image_height=height;

  632.     if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  633.     else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  634.     else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

  635.     yuvsize=tjBufSizeYUV(width, height, subsamp);
  636.     jpeg_mem_dest_tj(cinfo, &dstBuf, &yuvsize, 0);
  637.     if(setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags)==-1) return -1;

  638.     jpeg_start_compress(cinfo, TRUE);
  639.     pw=PAD(width, cinfo->max_h_samp_factor);
  640.     ph=PAD(height, cinfo->max_v_samp_factor);

  641.     if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL)
  642.         _throw("tjEncodeYUV2(): Memory allocation failure");
  643.     for(i=0; i<height; i++)
  644.     {
  645.         if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
  646.         else row_pointer[i]=&srcBuf[i*pitch];
  647.     }
  648.     if(height<ph)
  649.         for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1];

  650.     for(i=0; i<cinfo->num_components; i++)
  651.     {
  652.         compptr=&cinfo->comp_info[i];
  653.         _tmpbuf[i]=(JSAMPLE *)malloc(
  654.             PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  655.                 /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16);
  656.         if(!_tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  657.         tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor);
  658.         if(!tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  659.         for(row=0; row<cinfo->max_v_samp_factor; row++)
  660.         {
  661.             unsigned char *_tmpbuf_aligned=
  662.                 (unsigned char *)PAD((size_t)_tmpbuf[i], 16);
  663.             tmpbuf[i][row]=&_tmpbuf_aligned[
  664.                 PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  665.                     /compptr->h_samp_factor, 16) * row];
  666.         }
  667.         _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16)
  668.             * compptr->v_samp_factor + 16);
  669.         if(!_tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  670.         tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor);
  671.         if(!tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  672.         for(row=0; row<compptr->v_samp_factor; row++)
  673.         {
  674.             unsigned char *_tmpbuf2_aligned=
  675.                 (unsigned char *)PAD((size_t)_tmpbuf2[i], 16);
  676.             tmpbuf2[i][row]=&_tmpbuf2_aligned[
  677.                 PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
  678.         }
  679.         cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor;
  680.         ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor;
  681.         outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]);
  682.         if(!outbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  683.         for(row=0; row<ch[i]; row++)
  684.         {
  685.             outbuf[i][row]=ptr;
  686.             ptr+=PAD(cw[i], 4);
  687.         }
  688.     }
  689.     if(yuvsize!=(unsigned long)(ptr-dstBuf))
  690.         _throw("tjEncodeYUV2(): Generated image is not the correct size");

  691.     for(row=0; row<ph; row+=cinfo->max_v_samp_factor)
  692.     {
  693.         (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0,
  694.             cinfo->max_v_samp_factor);
  695.         (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0);
  696.         for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++)
  697.             jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
  698.                 row*compptr->v_samp_factor/cinfo->max_v_samp_factor,
  699.                 compptr->v_samp_factor, cw[i]);
  700.     }
  701.     cinfo->next_scanline+=height;
  702.     jpeg_abort_compress(cinfo);

  703.     bailout:
  704.     if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  705.     #ifndef JCS_EXTENSIONS
  706.     if(rgbBuf) free(rgbBuf);
  707.     #endif
  708.     if(row_pointer) free(row_pointer);
  709.     for(i=0; i<MAX_COMPONENTS; i++)
  710.     {
  711.         if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
  712.         if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
  713.         if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
  714.         if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
  715.         if(outbuf[i]!=NULL) free(outbuf[i]);
  716.     }
  717.     return retval;
  718. }

  719. DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf,
  720.     int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
  721.     int subsamp, int flags)
  722. {
  723.     return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  724.         getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags);
  725. }


  726. /* Decompressor */

  727. static tjhandle _tjInitDecompress(tjinstance *this)
  728. {
  729.     unsigned char buffer[1];

  730.     /* This is also straight out of example.c */
  731.     this->dinfo.err=jpeg_std_error(&this->jerr.pub);
  732.     this->jerr.pub.error_exit=my_error_exit;
  733.     this->jerr.pub.output_message=my_output_message;

  734.     if(setjmp(this->jerr.setjmp_buffer))
  735.     {
  736.         /* If we get here, the JPEG code has signaled an error. */
  737.         if(this) free(this); return NULL;
  738.     }

  739.     jpeg_create_decompress(&this->dinfo);
  740.     /* Make an initial call so it will create the source manager */
  741.     jpeg_mem_src_tj(&this->dinfo, buffer, 1);

  742.     this->init|=DECOMPRESS;
  743.     return (tjhandle)this;
  744. }

  745. DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
  746. {
  747.     tjinstance *this;
  748.     if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  749.     {
  750.         snprintf(errStr, JMSG_LENGTH_MAX,
  751.             "tjInitDecompress(): Memory allocation failure");
  752.         return NULL;
  753.     }
  754.     MEMZERO(this, sizeof(tjinstance));
  755.     return _tjInitDecompress(this);
  756. }


  757. DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
  758.     unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
  759.     int *jpegSubsamp)
  760. {
  761.     int retval=0;

  762.     getinstance(handle);
  763.     if((this->init&DECOMPRESS)==0)
  764.         _throw("tjDecompressHeader2(): Instance has not been initialized for decompression");

  765.     if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL
  766.         || jpegSubsamp==NULL)
  767.         _throw("tjDecompressHeader2(): Invalid argument");

  768.     if(setjmp(this->jerr.setjmp_buffer))
  769.     {
  770.         /* If we get here, the JPEG code has signaled an error. */
  771.         return -1;
  772.     }

  773.     jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  774.     jpeg_read_header(dinfo, TRUE);

  775.     *width=dinfo->image_width;
  776.     *height=dinfo->image_height;
  777.     *jpegSubsamp=getSubsamp(dinfo);

  778.     jpeg_abort_decompress(dinfo);

  779.     if(*jpegSubsamp<0)
  780.         _throw("tjDecompressHeader2(): Could not determine subsampling type for JPEG image");
  781.     if(*width<1 || *height<1)
  782.         _throw("tjDecompressHeader2(): Invalid data returned in header");

  783.     bailout:
  784.     return retval;
  785. }

  786. DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
  787.     unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height)
  788. {
  789.     int jpegSubsamp;
  790.     return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
  791.         &jpegSubsamp);
  792. }


  793. DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors)
  794. {
  795.     if(numscalingfactors==NULL)
  796.     {
  797.         snprintf(errStr, JMSG_LENGTH_MAX,
  798.             "tjGetScalingFactors(): Invalid argument");
  799.         return NULL;
  800.     }

  801.     *numscalingfactors=NUMSF;
  802.     return (tjscalingfactor *)sf;
  803. }


  804. DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf,
  805.     unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
  806.     int height, int pixelFormat, int flags)
  807. {
  808.     int i, retval=0; JSAMPROW *row_pointer=NULL;
  809.     int jpegwidth, jpegheight, scaledw, scaledh;
  810.     #ifndef JCS_EXTENSIONS
  811.     unsigned char *rgbBuf=NULL;
  812.     unsigned char *_dstBuf=NULL; int _pitch=0;
  813.     #endif

  814.     getinstance(handle);
  815.     if((this->init&DECOMPRESS)==0)
  816.         _throw("tjDecompress2(): Instance has not been initialized for decompression");

  817.     if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0
  818.         || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF)
  819.         _throw("tjDecompress2(): Invalid argument");

  820.     if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  821.     else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  822.     else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

  823.     if(setjmp(this->jerr.setjmp_buffer))
  824.     {
  825.         /* If we get here, the JPEG code has signaled an error. */
  826.         retval=-1;
  827.         goto bailout;
  828.     }

  829.     jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  830.     jpeg_read_header(dinfo, TRUE);
  831.     if(setDecompDefaults(dinfo, pixelFormat, flags)==-1)
  832.     {
  833.         retval=-1; goto bailout;
  834.     }

  835.     if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;

  836.     jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
  837.     if(width==0) width=jpegwidth;
  838.     if(height==0) height=jpegheight;
  839.     for(i=0; i<NUMSF; i++)
  840.     {
  841.         scaledw=TJSCALED(jpegwidth, sf[i]);
  842.         scaledh=TJSCALED(jpegheight, sf[i]);
  843.         if(scaledw<=width && scaledh<=height)
  844.                 break;
  845.     }
  846.     if(scaledw>width || scaledh>height)
  847.         _throw("tjDecompress2(): Could not scale down to desired image dimensions");
  848.     width=scaledw; height=scaledh;
  849.     dinfo->scale_num=sf[i].num;
  850.     dinfo->scale_denom=sf[i].denom;

  851.     jpeg_start_decompress(dinfo);
  852.     if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];

  853.     #ifndef JCS_EXTENSIONS
  854.     if(pixelFormat!=TJPF_GRAY &&
  855.         (RGB_RED!=tjRedOffset[pixelFormat] ||
  856.             RGB_GREEN!=tjGreenOffset[pixelFormat] ||
  857.             RGB_BLUE!=tjBlueOffset[pixelFormat] ||
  858.             RGB_PIXELSIZE!=tjPixelSize[pixelFormat]))
  859.     {
  860.         rgbBuf=(unsigned char *)malloc(width*height*3);
  861.         if(!rgbBuf) _throw("tjDecompress2(): Memory allocation failure");
  862.         _pitch=pitch; pitch=width*3;
  863.         _dstBuf=dstBuf; dstBuf=rgbBuf;
  864.     }
  865.     #endif

  866.     if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)
  867.         *dinfo->output_height))==NULL)
  868.         _throw("tjDecompress2(): Memory allocation failure");
  869.     for(i=0; i<(int)dinfo->output_height; i++)
  870.     {
  871.         if(flags&TJFLAG_BOTTOMUP)
  872.             row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch];
  873.         else row_pointer[i]=&dstBuf[i*pitch];
  874.     }
  875.     while(dinfo->output_scanline<dinfo->output_height)
  876.     {
  877.         jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
  878.             dinfo->output_height-dinfo->output_scanline);
  879.     }
  880.     jpeg_finish_decompress(dinfo);

  881.     #ifndef JCS_EXTENSIONS
  882.     fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat);
  883.     #endif

  884.     bailout:
  885.     if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  886.     #ifndef JCS_EXTENSIONS
  887.     if(rgbBuf) free(rgbBuf);
  888.     #endif
  889.     if(row_pointer) free(row_pointer);
  890.     return retval;
  891. }

  892. DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf,
  893.     unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
  894.     int height, int pixelSize, int flags)
  895. {
  896.     if(flags&TJ_YUV)
  897.         return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
  898.     else
  899.         return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
  900.             height, getPixelFormat(pixelSize, flags), flags);
  901. }


  902. DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
  903.     unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  904.     int flags)
  905. {
  906.     int i, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS];
  907.     int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS],
  908.         tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
  909.     JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf; JSAMPROW *tmpbuf[MAX_COMPONENTS];

  910.     getinstance(handle);
  911.     if((this->init&DECOMPRESS)==0)
  912.         _throw("tjDecompressToYUV(): Instance has not been initialized for decompression");

  913.     for(i=0; i<MAX_COMPONENTS; i++)
  914.     {
  915.         tmpbuf[i]=NULL; outbuf[i]=NULL;
  916.     }

  917.     if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL)
  918.         _throw("tjDecompressToYUV(): Invalid argument");

  919.     if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  920.     else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  921.     else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

  922.     if(setjmp(this->jerr.setjmp_buffer))
  923.     {
  924.         /* If we get here, the JPEG code has signaled an error. */
  925.         retval=-1;
  926.         goto bailout;
  927.     }

  928.     jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  929.     jpeg_read_header(dinfo, TRUE);

  930.     for(i=0; i<dinfo->num_components; i++)
  931.     {
  932.         jpeg_component_info *compptr=&dinfo->comp_info[i];
  933.         int ih;
  934.         iw[i]=compptr->width_in_blocks*DCTSIZE;
  935.         ih=compptr->height_in_blocks*DCTSIZE;
  936.         cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor)
  937.             *compptr->h_samp_factor/dinfo->max_h_samp_factor;
  938.         ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor)
  939.             *compptr->v_samp_factor/dinfo->max_v_samp_factor;
  940.         if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1;
  941.         th[i]=compptr->v_samp_factor*DCTSIZE;
  942.         tmpbufsize+=iw[i]*th[i];
  943.         if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL)
  944.             _throw("tjDecompressToYUV(): Memory allocation failure");
  945.         for(row=0; row<ch[i]; row++)
  946.         {
  947.             outbuf[i][row]=ptr;
  948.             ptr+=PAD(cw[i], 4);
  949.         }
  950.     }
  951.     if(usetmpbuf)
  952.     {
  953.         if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
  954.             _throw("tjDecompressToYUV(): Memory allocation failure");
  955.         ptr=_tmpbuf;
  956.         for(i=0; i<dinfo->num_components; i++)
  957.         {
  958.             if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
  959.                 _throw("tjDecompressToYUV(): Memory allocation failure");
  960.             for(row=0; row<th[i]; row++)
  961.             {
  962.                 tmpbuf[i][row]=ptr;
  963.                 ptr+=iw[i];
  964.             }
  965.         }
  966.     }

  967.     if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
  968.     if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;
  969.     dinfo->raw_data_out=TRUE;

  970.     jpeg_start_decompress(dinfo);
  971.     for(row=0; row<(int)dinfo->output_height;
  972.         row+=dinfo->max_v_samp_factor*DCTSIZE)
  973.     {
  974.         JSAMPARRAY yuvptr[MAX_COMPONENTS];
  975.         int crow[MAX_COMPONENTS];
  976.         for(i=0; i<dinfo->num_components; i++)
  977.         {
  978.             jpeg_component_info *compptr=&dinfo->comp_info[i];
  979.             crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor;
  980.             if(usetmpbuf) yuvptr[i]=tmpbuf[i];
  981.             else yuvptr[i]=&outbuf[i][crow[i]];
  982.         }
  983.         jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE);
  984.         if(usetmpbuf)
  985.         {
  986.             int j;
  987.             for(i=0; i<dinfo->num_components; i++)
  988.             {
  989.                 for(j=0; j<min(th[i], ch[i]-crow[i]); j++)
  990.                 {
  991.                     memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]);
  992.                 }
  993.             }
  994.         }
  995.     }
  996.     jpeg_finish_decompress(dinfo);

  997.     bailout:
  998.     if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  999.     for(i=0; i<MAX_COMPONENTS; i++)
  1000.     {
  1001.         if(tmpbuf[i]) free(tmpbuf[i]);
  1002.         if(outbuf[i]) free(outbuf[i]);
  1003.     }
  1004.     if(_tmpbuf) free(_tmpbuf);
  1005.     return retval;
  1006. }


  1007. /* Transformer */

  1008. DLLEXPORT tjhandle DLLCALL tjInitTransform(void)
  1009. {
  1010.     tjinstance *this=NULL; tjhandle handle=NULL;
  1011.     if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  1012.     {
  1013.         snprintf(errStr, JMSG_LENGTH_MAX,
  1014.             "tjInitTransform(): Memory allocation failure");
  1015.         return NULL;
  1016.     }
  1017.     MEMZERO(this, sizeof(tjinstance));
  1018.     handle=_tjInitCompress(this);
  1019.     if(!handle) return NULL;
  1020.     handle=_tjInitDecompress(this);
  1021.     return handle;
  1022. }


  1023. DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
  1024.     unsigned long jpegSize, int n, unsigned char **dstBufs,
  1025.     unsigned long *dstSizes, tjtransform *t, int flags)
  1026. {
  1027.     jpeg_transform_info *xinfo=NULL;
  1028.     jvirt_barray_ptr *srccoefs, *dstcoefs;
  1029.     int retval=0, i, jpegSubsamp;

  1030.     getinstance(handle);
  1031.     if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0)
  1032.         _throw("tjTransform(): Instance has not been initialized for transformation");

  1033.     if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL
  1034.         || t==NULL || flags<0)
  1035.         _throw("tjTransform(): Invalid argument");

  1036.     if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1037.     else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1038.     else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

  1039.     if(setjmp(this->jerr.setjmp_buffer))
  1040.     {
  1041.         /* If we get here, the JPEG code has signaled an error. */
  1042.         retval=-1;
  1043.         goto bailout;
  1044.     }

  1045.     jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);

  1046.     if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n))
  1047.         ==NULL)
  1048.         _throw("tjTransform(): Memory allocation failure");
  1049.     MEMZERO(xinfo, sizeof(jpeg_transform_info)*n);

  1050.     for(i=0; i<n; i++)
  1051.     {
  1052.         xinfo[i].transform=xformtypes[t[i].op];
  1053.         xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0;
  1054.         xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0;
  1055.         xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0;
  1056.         xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0;
  1057.         if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1;
  1058.         else xinfo[i].slow_hflip=0;

  1059.         if(xinfo[i].crop)
  1060.         {
  1061.             xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS;
  1062.             xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS;
  1063.             if(t[i].r.w!=0)
  1064.             {
  1065.                 xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS;
  1066.             }
  1067.             else xinfo[i].crop_width=JCROP_UNSET;
  1068.             if(t[i].r.h!=0)
  1069.             {
  1070.                 xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS;
  1071.             }
  1072.             else xinfo[i].crop_height=JCROP_UNSET;
  1073.         }
  1074.     }

  1075.     jcopy_markers_setup(dinfo, JCOPYOPT_ALL);
  1076.     jpeg_read_header(dinfo, TRUE);
  1077.     jpegSubsamp=getSubsamp(dinfo);
  1078.     if(jpegSubsamp<0)
  1079.         _throw("tjTransform(): Could not determine subsampling type for JPEG image");

  1080.     for(i=0; i<n; i++)
  1081.     {
  1082.         if(!jtransform_request_workspace(dinfo, &xinfo[i]))
  1083.             _throw("tjTransform(): Transform is not perfect");

  1084.         if(xinfo[i].crop)
  1085.         {
  1086.             if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0
  1087.                 || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0)
  1088.             {
  1089.                 snprintf(errStr, JMSG_LENGTH_MAX,
  1090.                     "To crop this JPEG image, x must be a multiple of %d\n"
  1091.                     "and y must be a multiple of %d.\n",
  1092.                     xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height);
  1093.                 retval=-1; goto bailout;
  1094.             }
  1095.         }
  1096.     }

  1097.     srccoefs=jpeg_read_coefficients(dinfo);

  1098.     for(i=0; i<n; i++)
  1099.     {
  1100.         int w, h, alloc=1;
  1101.         if(!xinfo[i].crop)
  1102.         {
  1103.             w=dinfo->image_width; h=dinfo->image_height;
  1104.         }
  1105.         else
  1106.         {
  1107.             w=xinfo[i].crop_width; h=xinfo[i].crop_height;
  1108.         }
  1109.         if(flags&TJFLAG_NOREALLOC)
  1110.         {
  1111.             alloc=0; dstSizes[i]=tjBufSize(w, h, jpegSubsamp);
  1112.         }
  1113.         if(!(t[i].options&TJXOPT_NOOUTPUT))
  1114.             jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc);
  1115.         jpeg_copy_critical_parameters(dinfo, cinfo);
  1116.         dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs,
  1117.             &xinfo[i]);
  1118.         if(!(t[i].options&TJXOPT_NOOUTPUT))
  1119.         {
  1120.             jpeg_write_coefficients(cinfo, dstcoefs);
  1121.             jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL);
  1122.         }
  1123.         else jinit_c_master_control(cinfo, TRUE);
  1124.         jtransform_execute_transformation(dinfo, cinfo, srccoefs,
  1125.             &xinfo[i]);
  1126.         if(t[i].customFilter)
  1127.         {
  1128.             int ci, y; JDIMENSION by;
  1129.             for(ci=0; ci<cinfo->num_components; ci++)
  1130.             {
  1131.                 jpeg_component_info *compptr=&cinfo->comp_info[ci];
  1132.                 tjregion arrayRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1133.                     DCTSIZE};
  1134.                 tjregion planeRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1135.                     compptr->height_in_blocks*DCTSIZE};
  1136.                 for(by=0; by<compptr->height_in_blocks; by+=compptr->v_samp_factor)
  1137.                 {
  1138.                     JBLOCKARRAY barray=(dinfo->mem->access_virt_barray)
  1139.                         ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor,
  1140.                         TRUE);
  1141.                     for(y=0; y<compptr->v_samp_factor; y++)
  1142.                     {
  1143.                         if(t[i].customFilter(barray[y][0], arrayRegion, planeRegion,
  1144.                             ci, i, &t[i])==-1)
  1145.                             _throw("tjTransform(): Error in custom filter");
  1146.                         arrayRegion.y+=DCTSIZE;
  1147.                     }
  1148.                 }
  1149.             }
  1150.         }
  1151.         if(!(t[i].options&TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo);
  1152.     }

  1153.     jpeg_finish_decompress(dinfo);

  1154.     bailout:
  1155.     if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  1156.     if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1157.     if(xinfo) free(xinfo);
  1158.     return retval;
  1159. }

阅读(2598) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

啦哆A梦2014-02-27 09:38:08