turbojpeg.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Copyright (C)2009-2012, 2014 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/LJT: this implements the TurboJPEG API using libjpeg or
  29. libjpeg-turbo */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <jinclude.h>
  33. #define JPEG_INTERNALS
  34. #include <jpeglib.h>
  35. #include <jerror.h>
  36. #include <setjmp.h>
  37. #include "./turbojpeg.h"
  38. #include "./tjutil.h"
  39. #include "transupp.h"
  40. extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **,
  41. unsigned long *, boolean);
  42. extern void jpeg_mem_src_tj(j_decompress_ptr, unsigned char *, unsigned long);
  43. #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
  44. /* Error handling (based on example in example.c) */
  45. static char errStr[JMSG_LENGTH_MAX]="No error";
  46. struct my_error_mgr
  47. {
  48. struct jpeg_error_mgr pub;
  49. jmp_buf setjmp_buffer;
  50. };
  51. typedef struct my_error_mgr *my_error_ptr;
  52. static void my_error_exit(j_common_ptr cinfo)
  53. {
  54. my_error_ptr myerr=(my_error_ptr)cinfo->err;
  55. (*cinfo->err->output_message)(cinfo);
  56. longjmp(myerr->setjmp_buffer, 1);
  57. }
  58. /* Based on output_message() in jerror.c */
  59. static void my_output_message(j_common_ptr cinfo)
  60. {
  61. (*cinfo->err->format_message)(cinfo, errStr);
  62. }
  63. /* Global structures, macros, etc. */
  64. enum {COMPRESS=1, DECOMPRESS=2};
  65. typedef struct _tjinstance
  66. {
  67. struct jpeg_compress_struct cinfo;
  68. struct jpeg_decompress_struct dinfo;
  69. struct my_error_mgr jerr;
  70. int init;
  71. } tjinstance;
  72. static const int pixelsize[TJ_NUMSAMP]={3, 3, 3, 1, 3};
  73. static const JXFORM_CODE xformtypes[TJ_NUMXOP]=
  74. {
  75. JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
  76. JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
  77. };
  78. #define NUMSF 16
  79. static const tjscalingfactor sf[NUMSF]={
  80. {2, 1},
  81. {15, 8},
  82. {7, 4},
  83. {13, 8},
  84. {3, 2},
  85. {11, 8},
  86. {5, 4},
  87. {9, 8},
  88. {1, 1},
  89. {7, 8},
  90. {3, 4},
  91. {5, 8},
  92. {1, 2},
  93. {3, 8},
  94. {1, 4},
  95. {1, 8}
  96. };
  97. #define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
  98. retval=-1; goto bailout;}
  99. #define getinstance(handle) tjinstance *this=(tjinstance *)handle; \
  100. j_compress_ptr cinfo=NULL; j_decompress_ptr dinfo=NULL; \
  101. if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
  102. return -1;} \
  103. cinfo=&this->cinfo; dinfo=&this->dinfo;
  104. static int getPixelFormat(int pixelSize, int flags)
  105. {
  106. if(pixelSize==1) return TJPF_GRAY;
  107. if(pixelSize==3)
  108. {
  109. if(flags&TJ_BGR) return TJPF_BGR;
  110. else return TJPF_RGB;
  111. }
  112. if(pixelSize==4)
  113. {
  114. if(flags&TJ_ALPHAFIRST)
  115. {
  116. if(flags&TJ_BGR) return TJPF_XBGR;
  117. else return TJPF_XRGB;
  118. }
  119. else
  120. {
  121. if(flags&TJ_BGR) return TJPF_BGRX;
  122. else return TJPF_RGBX;
  123. }
  124. }
  125. return -1;
  126. }
  127. static int setCompDefaults(struct jpeg_compress_struct *cinfo,
  128. int pixelFormat, int subsamp, int jpegQual, int flags)
  129. {
  130. int retval=0;
  131. switch(pixelFormat)
  132. {
  133. case TJPF_GRAY:
  134. cinfo->in_color_space=JCS_GRAYSCALE; break;
  135. #if JCS_EXTENSIONS==1
  136. case TJPF_RGB:
  137. cinfo->in_color_space=JCS_EXT_RGB; break;
  138. case TJPF_BGR:
  139. cinfo->in_color_space=JCS_EXT_BGR; break;
  140. case TJPF_RGBX:
  141. case TJPF_RGBA:
  142. cinfo->in_color_space=JCS_EXT_RGBX; break;
  143. case TJPF_BGRX:
  144. case TJPF_BGRA:
  145. cinfo->in_color_space=JCS_EXT_BGRX; break;
  146. case TJPF_XRGB:
  147. case TJPF_ARGB:
  148. cinfo->in_color_space=JCS_EXT_XRGB; break;
  149. case TJPF_XBGR:
  150. case TJPF_ABGR:
  151. cinfo->in_color_space=JCS_EXT_XBGR; break;
  152. #else
  153. case TJPF_RGB:
  154. case TJPF_BGR:
  155. case TJPF_RGBX:
  156. case TJPF_BGRX:
  157. case TJPF_XRGB:
  158. case TJPF_XBGR:
  159. case TJPF_RGBA:
  160. case TJPF_BGRA:
  161. case TJPF_ARGB:
  162. case TJPF_ABGR:
  163. cinfo->in_color_space=JCS_RGB; pixelFormat=TJPF_RGB;
  164. break;
  165. #endif
  166. }
  167. cinfo->input_components=tjPixelSize[pixelFormat];
  168. jpeg_set_defaults(cinfo);
  169. if(jpegQual>=0)
  170. {
  171. jpeg_set_quality(cinfo, jpegQual, TRUE);
  172. if(jpegQual>=96 || flags&TJFLAG_ACCURATEDCT) cinfo->dct_method=JDCT_ISLOW;
  173. else cinfo->dct_method=JDCT_FASTEST;
  174. }
  175. if(subsamp==TJSAMP_GRAY)
  176. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  177. else
  178. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  179. cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8;
  180. cinfo->comp_info[1].h_samp_factor=1;
  181. cinfo->comp_info[2].h_samp_factor=1;
  182. cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8;
  183. cinfo->comp_info[1].v_samp_factor=1;
  184. cinfo->comp_info[2].v_samp_factor=1;
  185. return retval;
  186. }
  187. static int setDecompDefaults(struct jpeg_decompress_struct *dinfo,
  188. int pixelFormat, int flags)
  189. {
  190. int retval=0;
  191. switch(pixelFormat)
  192. {
  193. case TJPF_GRAY:
  194. dinfo->out_color_space=JCS_GRAYSCALE; break;
  195. #if JCS_EXTENSIONS==1
  196. case TJPF_RGB:
  197. dinfo->out_color_space=JCS_EXT_RGB; break;
  198. case TJPF_BGR:
  199. dinfo->out_color_space=JCS_EXT_BGR; break;
  200. case TJPF_RGBX:
  201. dinfo->out_color_space=JCS_EXT_RGBX; break;
  202. case TJPF_BGRX:
  203. dinfo->out_color_space=JCS_EXT_BGRX; break;
  204. case TJPF_XRGB:
  205. dinfo->out_color_space=JCS_EXT_XRGB; break;
  206. case TJPF_XBGR:
  207. dinfo->out_color_space=JCS_EXT_XBGR; break;
  208. #if JCS_ALPHA_EXTENSIONS==1
  209. case TJPF_RGBA:
  210. dinfo->out_color_space=JCS_EXT_RGBA; break;
  211. case TJPF_BGRA:
  212. dinfo->out_color_space=JCS_EXT_BGRA; break;
  213. case TJPF_ARGB:
  214. dinfo->out_color_space=JCS_EXT_ARGB; break;
  215. case TJPF_ABGR:
  216. dinfo->out_color_space=JCS_EXT_ABGR; break;
  217. #endif
  218. #else
  219. case TJPF_RGB:
  220. case TJPF_BGR:
  221. case TJPF_RGBX:
  222. case TJPF_BGRX:
  223. case TJPF_XRGB:
  224. case TJPF_XBGR:
  225. case TJPF_RGBA:
  226. case TJPF_BGRA:
  227. case TJPF_ARGB:
  228. case TJPF_ABGR:
  229. dinfo->out_color_space=JCS_RGB; break;
  230. #endif
  231. default:
  232. _throw("Unsupported pixel format");
  233. }
  234. if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;
  235. bailout:
  236. return retval;
  237. }
  238. static int getSubsamp(j_decompress_ptr dinfo)
  239. {
  240. int retval=-1, i, k;
  241. for(i=0; i<NUMSUBOPT; i++)
  242. {
  243. if(dinfo->num_components==pixelsize[i])
  244. {
  245. if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8
  246. && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8)
  247. {
  248. int match=0;
  249. for(k=1; k<dinfo->num_components; k++)
  250. {
  251. if(dinfo->comp_info[k].h_samp_factor==1
  252. && dinfo->comp_info[k].v_samp_factor==1)
  253. match++;
  254. }
  255. if(match==dinfo->num_components-1)
  256. {
  257. retval=i; break;
  258. }
  259. }
  260. }
  261. }
  262. return retval;
  263. }
  264. #ifndef JCS_EXTENSIONS
  265. /* Conversion functions to emulate the colorspace extensions. This allows the
  266. TurboJPEG wrapper to be used with libjpeg */
  267. #define TORGB(PS, ROFFSET, GOFFSET, BOFFSET) { \
  268. int rowPad=pitch-width*PS; \
  269. while(height--) \
  270. { \
  271. unsigned char *endOfRow=src+width*PS; \
  272. while(src<endOfRow) \
  273. { \
  274. dst[RGB_RED]=src[ROFFSET]; \
  275. dst[RGB_GREEN]=src[GOFFSET]; \
  276. dst[RGB_BLUE]=src[BOFFSET]; \
  277. dst+=RGB_PIXELSIZE; src+=PS; \
  278. } \
  279. src+=rowPad; \
  280. } \
  281. }
  282. static unsigned char *toRGB(unsigned char *src, int width, int pitch,
  283. int height, int pixelFormat, unsigned char *dst)
  284. {
  285. unsigned char *retval=src;
  286. switch(pixelFormat)
  287. {
  288. case TJPF_RGB:
  289. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  290. retval=dst; TORGB(3, 0, 1, 2);
  291. #endif
  292. break;
  293. case TJPF_BGR:
  294. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  295. retval=dst; TORGB(3, 2, 1, 0);
  296. #endif
  297. break;
  298. case TJPF_RGBX:
  299. case TJPF_RGBA:
  300. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  301. retval=dst; TORGB(4, 0, 1, 2);
  302. #endif
  303. break;
  304. case TJPF_BGRX:
  305. case TJPF_BGRA:
  306. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  307. retval=dst; TORGB(4, 2, 1, 0);
  308. #endif
  309. break;
  310. case TJPF_XRGB:
  311. case TJPF_ARGB:
  312. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  313. retval=dst; TORGB(4, 1, 2, 3);
  314. #endif
  315. break;
  316. case TJPF_XBGR:
  317. case TJPF_ABGR:
  318. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  319. retval=dst; TORGB(4, 3, 2, 1);
  320. #endif
  321. break;
  322. }
  323. return retval;
  324. }
  325. #define FROMRGB(PS, ROFFSET, GOFFSET, BOFFSET, SETALPHA) { \
  326. int rowPad=pitch-width*PS; \
  327. while(height--) \
  328. { \
  329. unsigned char *endOfRow=dst+width*PS; \
  330. while(dst<endOfRow) \
  331. { \
  332. dst[ROFFSET]=src[RGB_RED]; \
  333. dst[GOFFSET]=src[RGB_GREEN]; \
  334. dst[BOFFSET]=src[RGB_BLUE]; \
  335. SETALPHA \
  336. dst+=PS; src+=RGB_PIXELSIZE; \
  337. } \
  338. dst+=rowPad; \
  339. } \
  340. }
  341. static void fromRGB(unsigned char *src, unsigned char *dst, int width,
  342. int pitch, int height, int pixelFormat)
  343. {
  344. switch(pixelFormat)
  345. {
  346. case TJPF_RGB:
  347. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  348. FROMRGB(3, 0, 1, 2,);
  349. #endif
  350. break;
  351. case TJPF_BGR:
  352. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  353. FROMRGB(3, 2, 1, 0,);
  354. #endif
  355. break;
  356. case TJPF_RGBX:
  357. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  358. FROMRGB(4, 0, 1, 2,);
  359. #endif
  360. break;
  361. case TJPF_RGBA:
  362. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  363. FROMRGB(4, 0, 1, 2, dst[3]=0xFF;);
  364. #endif
  365. break;
  366. case TJPF_BGRX:
  367. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  368. FROMRGB(4, 2, 1, 0,);
  369. #endif
  370. break;
  371. case TJPF_BGRA:
  372. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  373. FROMRGB(4, 2, 1, 0, dst[3]=0xFF;); return;
  374. #endif
  375. break;
  376. case TJPF_XRGB:
  377. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  378. FROMRGB(4, 1, 2, 3,); return;
  379. #endif
  380. break;
  381. case TJPF_ARGB:
  382. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  383. FROMRGB(4, 1, 2, 3, dst[0]=0xFF;); return;
  384. #endif
  385. break;
  386. case TJPF_XBGR:
  387. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  388. FROMRGB(4, 3, 2, 1,); return;
  389. #endif
  390. break;
  391. case TJPF_ABGR:
  392. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  393. FROMRGB(4, 3, 2, 1, dst[0]=0xFF;); return;
  394. #endif
  395. break;
  396. }
  397. }
  398. #endif
  399. /* General API functions */
  400. DLLEXPORT char* DLLCALL tjGetErrorStr(void)
  401. {
  402. return errStr;
  403. }
  404. DLLEXPORT int DLLCALL tjDestroy(tjhandle handle)
  405. {
  406. getinstance(handle);
  407. if(setjmp(this->jerr.setjmp_buffer)) return -1;
  408. if(this->init&COMPRESS) jpeg_destroy_compress(cinfo);
  409. if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo);
  410. free(this);
  411. return 0;
  412. }
  413. /* These are exposed mainly because Windows can't malloc() and free() across
  414. DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL
  415. with turbojpeg.dll for compatibility reasons. However, these functions
  416. can potentially be used for other purposes by different implementations. */
  417. DLLEXPORT void DLLCALL tjFree(unsigned char *buf)
  418. {
  419. if(buf) free(buf);
  420. }
  421. DLLEXPORT unsigned char *DLLCALL tjAlloc(int bytes)
  422. {
  423. return (unsigned char *)malloc(bytes);
  424. }
  425. /* Compressor */
  426. static tjhandle _tjInitCompress(tjinstance *this)
  427. {
  428. unsigned char buffer[1], *buf=buffer; unsigned long size=1;
  429. /* This is also straight out of example.c */
  430. this->cinfo.err=jpeg_std_error(&this->jerr.pub);
  431. this->jerr.pub.error_exit=my_error_exit;
  432. this->jerr.pub.output_message=my_output_message;
  433. if(setjmp(this->jerr.setjmp_buffer))
  434. {
  435. /* If we get here, the JPEG code has signaled an error. */
  436. if(this) free(this); return NULL;
  437. }
  438. jpeg_create_compress(&this->cinfo);
  439. /* Make an initial call so it will create the destination manager */
  440. jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);
  441. this->init|=COMPRESS;
  442. return (tjhandle)this;
  443. }
  444. DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
  445. {
  446. tjinstance *this=NULL;
  447. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  448. {
  449. snprintf(errStr, JMSG_LENGTH_MAX,
  450. "tjInitCompress(): Memory allocation failure");
  451. return NULL;
  452. }
  453. MEMZERO(this, sizeof(tjinstance));
  454. return _tjInitCompress(this);
  455. }
  456. DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
  457. int jpegSubsamp)
  458. {
  459. unsigned long retval=0; int mcuw, mcuh, chromasf;
  460. if(width<1 || height<1 || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT)
  461. _throw("tjBufSize(): Invalid argument");
  462. /* This allows for rare corner cases in which a JPEG image can actually be
  463. larger than the uncompressed input (we wouldn't mention it if it hadn't
  464. happened before.) */
  465. mcuw=tjMCUWidth[jpegSubsamp];
  466. mcuh=tjMCUHeight[jpegSubsamp];
  467. chromasf=jpegSubsamp==TJSAMP_GRAY? 0: 4*64/(mcuw*mcuh);
  468. retval=PAD(width, mcuw) * PAD(height, mcuh) * (2 + chromasf) + 2048;
  469. bailout:
  470. return retval;
  471. }
  472. DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
  473. {
  474. unsigned long retval=0;
  475. if(width<1 || height<1)
  476. _throw("TJBUFSIZE(): Invalid argument");
  477. /* This allows for rare corner cases in which a JPEG image can actually be
  478. larger than the uncompressed input (we wouldn't mention it if it hadn't
  479. happened before.) */
  480. retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048;
  481. bailout:
  482. return retval;
  483. }
  484. DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
  485. int subsamp)
  486. {
  487. unsigned long retval=0;
  488. int pw, ph, cw, ch;
  489. if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT)
  490. _throw("tjBufSizeYUV(): Invalid argument");
  491. pw=PAD(width, tjMCUWidth[subsamp]/8);
  492. ph=PAD(height, tjMCUHeight[subsamp]/8);
  493. cw=pw*8/tjMCUWidth[subsamp]; ch=ph*8/tjMCUHeight[subsamp];
  494. retval=PAD(pw, 4)*ph + (subsamp==TJSAMP_GRAY? 0:PAD(cw, 4)*ch*2);
  495. bailout:
  496. return retval;
  497. }
  498. DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
  499. int subsamp)
  500. {
  501. return tjBufSizeYUV(width, height, subsamp);
  502. }
  503. DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, unsigned char *srcBuf,
  504. int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
  505. unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  506. {
  507. int i, retval=0, alloc=1; JSAMPROW *row_pointer=NULL;
  508. #ifndef JCS_EXTENSIONS
  509. unsigned char *rgbBuf=NULL;
  510. #endif
  511. getinstance(handle)
  512. if((this->init&COMPRESS)==0)
  513. _throw("tjCompress2(): Instance has not been initialized for compression");
  514. if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  515. || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL
  516. || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100)
  517. _throw("tjCompress2(): Invalid argument");
  518. if(setjmp(this->jerr.setjmp_buffer))
  519. {
  520. /* If we get here, the JPEG code has signaled an error. */
  521. retval=-1;
  522. goto bailout;
  523. }
  524. if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
  525. #ifndef JCS_EXTENSIONS
  526. if(pixelFormat!=TJPF_GRAY)
  527. {
  528. rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  529. if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure");
  530. srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  531. pitch=width*RGB_PIXELSIZE;
  532. }
  533. #endif
  534. cinfo->image_width=width;
  535. cinfo->image_height=height;
  536. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  537. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  538. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  539. if(flags&TJFLAG_NOREALLOC)
  540. {
  541. alloc=0; *jpegSize=tjBufSize(width, height, jpegSubsamp);
  542. }
  543. jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
  544. if(setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags)==-1)
  545. return -1;
  546. jpeg_start_compress(cinfo, TRUE);
  547. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
  548. _throw("tjCompress2(): Memory allocation failure");
  549. for(i=0; i<height; i++)
  550. {
  551. if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
  552. else row_pointer[i]=&srcBuf[i*pitch];
  553. }
  554. while(cinfo->next_scanline<cinfo->image_height)
  555. {
  556. jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
  557. cinfo->image_height-cinfo->next_scanline);
  558. }
  559. jpeg_finish_compress(cinfo);
  560. bailout:
  561. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  562. #ifndef JCS_EXTENSIONS
  563. if(rgbBuf) free(rgbBuf);
  564. #endif
  565. if(row_pointer) free(row_pointer);
  566. return retval;
  567. }
  568. DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
  569. int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf,
  570. unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  571. {
  572. int retval=0; unsigned long size;
  573. if(flags&TJ_YUV)
  574. {
  575. size=tjBufSizeYUV(width, height, jpegSubsamp);
  576. retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  577. getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags);
  578. }
  579. else
  580. {
  581. retval=tjCompress2(handle, srcBuf, width, pitch, height,
  582. getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual,
  583. flags|TJFLAG_NOREALLOC);
  584. }
  585. *jpegSize=size;
  586. return retval;
  587. }
  588. DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf,
  589. int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf,
  590. int subsamp, int flags)
  591. {
  592. int i, retval=0; JSAMPROW *row_pointer=NULL;
  593. JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
  594. JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
  595. JSAMPROW *outbuf[MAX_COMPONENTS];
  596. int row, pw, ph, cw[MAX_COMPONENTS], ch[MAX_COMPONENTS];
  597. JSAMPLE *ptr=dstBuf;
  598. unsigned long yuvsize=0;
  599. jpeg_component_info *compptr;
  600. #ifndef JCS_EXTENSIONS
  601. unsigned char *rgbBuf=NULL;
  602. #endif
  603. getinstance(handle);
  604. for(i=0; i<MAX_COMPONENTS; i++)
  605. {
  606. tmpbuf[i]=NULL; _tmpbuf[i]=NULL;
  607. tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL;
  608. }
  609. if((this->init&COMPRESS)==0)
  610. _throw("tjEncodeYUV2(): Instance has not been initialized for compression");
  611. if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  612. || pixelFormat>=TJ_NUMPF || dstBuf==NULL || subsamp<0
  613. || subsamp>=NUMSUBOPT)
  614. _throw("tjEncodeYUV2(): Invalid argument");
  615. if(setjmp(this->jerr.setjmp_buffer))
  616. {
  617. /* If we get here, the JPEG code has signaled an error. */
  618. retval=-1;
  619. goto bailout;
  620. }
  621. if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
  622. #ifndef JCS_EXTENSIONS
  623. if(pixelFormat!=TJPF_GRAY)
  624. {
  625. rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  626. if(!rgbBuf) _throw("tjEncodeYUV2(): Memory allocation failure");
  627. srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  628. pitch=width*RGB_PIXELSIZE;
  629. }
  630. #endif
  631. cinfo->image_width=width;
  632. cinfo->image_height=height;
  633. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  634. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  635. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  636. yuvsize=tjBufSizeYUV(width, height, subsamp);
  637. if(setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags)==-1) return -1;
  638. /* Execute only the parts of jpeg_start_compress() that we need. If we
  639. were to call the whole jpeg_start_compress() function, then it would try
  640. to write the file headers, which could overflow the output buffer if the
  641. YUV image were very small. */
  642. if(cinfo->global_state!=CSTATE_START)
  643. _throw("tjEncodeYUV3(): libjpeg API is in the wrong state");
  644. (*cinfo->err->reset_error_mgr)((j_common_ptr)cinfo);
  645. jinit_c_master_control(cinfo, FALSE);
  646. jinit_color_converter(cinfo);
  647. jinit_downsampler(cinfo);
  648. (*cinfo->cconvert->start_pass)(cinfo);
  649. pw=PAD(width, cinfo->max_h_samp_factor);
  650. ph=PAD(height, cinfo->max_v_samp_factor);
  651. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph))==NULL)
  652. _throw("tjEncodeYUV2(): Memory allocation failure");
  653. for(i=0; i<height; i++)
  654. {
  655. if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
  656. else row_pointer[i]=&srcBuf[i*pitch];
  657. }
  658. if(height<ph)
  659. for(i=height; i<ph; i++) row_pointer[i]=row_pointer[height-1];
  660. for(i=0; i<cinfo->num_components; i++)
  661. {
  662. compptr=&cinfo->comp_info[i];
  663. _tmpbuf[i]=(JSAMPLE *)malloc(
  664. PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  665. /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16);
  666. if(!_tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  667. tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor);
  668. if(!tmpbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  669. for(row=0; row<cinfo->max_v_samp_factor; row++)
  670. {
  671. unsigned char *_tmpbuf_aligned=
  672. (unsigned char *)PAD((size_t)_tmpbuf[i], 16);
  673. tmpbuf[i][row]=&_tmpbuf_aligned[
  674. PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  675. /compptr->h_samp_factor, 16) * row];
  676. }
  677. _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16)
  678. * compptr->v_samp_factor + 16);
  679. if(!_tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  680. tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor);
  681. if(!tmpbuf2[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  682. for(row=0; row<compptr->v_samp_factor; row++)
  683. {
  684. unsigned char *_tmpbuf2_aligned=
  685. (unsigned char *)PAD((size_t)_tmpbuf2[i], 16);
  686. tmpbuf2[i][row]=&_tmpbuf2_aligned[
  687. PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
  688. }
  689. cw[i]=pw*compptr->h_samp_factor/cinfo->max_h_samp_factor;
  690. ch[i]=ph*compptr->v_samp_factor/cinfo->max_v_samp_factor;
  691. outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]);
  692. if(!outbuf[i]) _throw("tjEncodeYUV2(): Memory allocation failure");
  693. for(row=0; row<ch[i]; row++)
  694. {
  695. outbuf[i][row]=ptr;
  696. ptr+=PAD(cw[i], 4);
  697. }
  698. }
  699. if(yuvsize!=(unsigned long)(ptr-dstBuf))
  700. _throw("tjEncodeYUV2(): Generated image is not the correct size");
  701. for(row=0; row<ph; row+=cinfo->max_v_samp_factor)
  702. {
  703. (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0,
  704. cinfo->max_v_samp_factor);
  705. (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0);
  706. for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++)
  707. jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
  708. row*compptr->v_samp_factor/cinfo->max_v_samp_factor,
  709. compptr->v_samp_factor, cw[i]);
  710. }
  711. cinfo->next_scanline+=height;
  712. jpeg_abort_compress(cinfo);
  713. bailout:
  714. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  715. #ifndef JCS_EXTENSIONS
  716. if(rgbBuf) free(rgbBuf);
  717. #endif
  718. if(row_pointer) free(row_pointer);
  719. for(i=0; i<MAX_COMPONENTS; i++)
  720. {
  721. if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
  722. if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
  723. if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
  724. if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
  725. if(outbuf[i]!=NULL) free(outbuf[i]);
  726. }
  727. return retval;
  728. }
  729. DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf,
  730. int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
  731. int subsamp, int flags)
  732. {
  733. return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  734. getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags);
  735. }
  736. /* Decompressor */
  737. static tjhandle _tjInitDecompress(tjinstance *this)
  738. {
  739. unsigned char buffer[1];
  740. /* This is also straight out of example.c */
  741. this->dinfo.err=jpeg_std_error(&this->jerr.pub);
  742. this->jerr.pub.error_exit=my_error_exit;
  743. this->jerr.pub.output_message=my_output_message;
  744. if(setjmp(this->jerr.setjmp_buffer))
  745. {
  746. /* If we get here, the JPEG code has signaled an error. */
  747. if(this) free(this); return NULL;
  748. }
  749. jpeg_create_decompress(&this->dinfo);
  750. /* Make an initial call so it will create the source manager */
  751. jpeg_mem_src_tj(&this->dinfo, buffer, 1);
  752. this->init|=DECOMPRESS;
  753. return (tjhandle)this;
  754. }
  755. DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
  756. {
  757. tjinstance *this;
  758. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  759. {
  760. snprintf(errStr, JMSG_LENGTH_MAX,
  761. "tjInitDecompress(): Memory allocation failure");
  762. return NULL;
  763. }
  764. MEMZERO(this, sizeof(tjinstance));
  765. return _tjInitDecompress(this);
  766. }
  767. DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
  768. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
  769. int *jpegSubsamp)
  770. {
  771. int retval=0;
  772. getinstance(handle);
  773. if((this->init&DECOMPRESS)==0)
  774. _throw("tjDecompressHeader2(): Instance has not been initialized for decompression");
  775. if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL
  776. || jpegSubsamp==NULL)
  777. _throw("tjDecompressHeader2(): Invalid argument");
  778. if(setjmp(this->jerr.setjmp_buffer))
  779. {
  780. /* If we get here, the JPEG code has signaled an error. */
  781. return -1;
  782. }
  783. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  784. jpeg_read_header(dinfo, TRUE);
  785. *width=dinfo->image_width;
  786. *height=dinfo->image_height;
  787. *jpegSubsamp=getSubsamp(dinfo);
  788. jpeg_abort_decompress(dinfo);
  789. if(*jpegSubsamp<0)
  790. _throw("tjDecompressHeader2(): Could not determine subsampling type for JPEG image");
  791. if(*width<1 || *height<1)
  792. _throw("tjDecompressHeader2(): Invalid data returned in header");
  793. bailout:
  794. return retval;
  795. }
  796. DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
  797. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height)
  798. {
  799. int jpegSubsamp;
  800. return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
  801. &jpegSubsamp);
  802. }
  803. DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors)
  804. {
  805. if(numscalingfactors==NULL)
  806. {
  807. snprintf(errStr, JMSG_LENGTH_MAX,
  808. "tjGetScalingFactors(): Invalid argument");
  809. return NULL;
  810. }
  811. *numscalingfactors=NUMSF;
  812. return (tjscalingfactor *)sf;
  813. }
  814. DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle, unsigned char *jpegBuf,
  815. unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
  816. int height, int pixelFormat, int flags)
  817. {
  818. int i, retval=0; JSAMPROW *row_pointer=NULL;
  819. int jpegwidth, jpegheight, scaledw, scaledh;
  820. #ifndef JCS_EXTENSIONS
  821. unsigned char *rgbBuf=NULL;
  822. unsigned char *_dstBuf=NULL; int _pitch=0;
  823. #endif
  824. getinstance(handle);
  825. if((this->init&DECOMPRESS)==0)
  826. _throw("tjDecompress2(): Instance has not been initialized for decompression");
  827. if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0
  828. || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF)
  829. _throw("tjDecompress2(): Invalid argument");
  830. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  831. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  832. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  833. if(setjmp(this->jerr.setjmp_buffer))
  834. {
  835. /* If we get here, the JPEG code has signaled an error. */
  836. retval=-1;
  837. goto bailout;
  838. }
  839. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  840. jpeg_read_header(dinfo, TRUE);
  841. if(setDecompDefaults(dinfo, pixelFormat, flags)==-1)
  842. {
  843. retval=-1; goto bailout;
  844. }
  845. if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
  846. jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
  847. if(width==0) width=jpegwidth;
  848. if(height==0) height=jpegheight;
  849. for(i=0; i<NUMSF; i++)
  850. {
  851. scaledw=TJSCALED(jpegwidth, sf[i]);
  852. scaledh=TJSCALED(jpegheight, sf[i]);
  853. if(scaledw<=width && scaledh<=height)
  854. break;
  855. }
  856. if(scaledw>width || scaledh>height)
  857. _throw("tjDecompress2(): Could not scale down to desired image dimensions");
  858. width=scaledw; height=scaledh;
  859. dinfo->scale_num=sf[i].num;
  860. dinfo->scale_denom=sf[i].denom;
  861. jpeg_start_decompress(dinfo);
  862. if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
  863. #ifndef JCS_EXTENSIONS
  864. if(pixelFormat!=TJPF_GRAY &&
  865. (RGB_RED!=tjRedOffset[pixelFormat] ||
  866. RGB_GREEN!=tjGreenOffset[pixelFormat] ||
  867. RGB_BLUE!=tjBlueOffset[pixelFormat] ||
  868. RGB_PIXELSIZE!=tjPixelSize[pixelFormat]))
  869. {
  870. rgbBuf=(unsigned char *)malloc(width*height*3);
  871. if(!rgbBuf) _throw("tjDecompress2(): Memory allocation failure");
  872. _pitch=pitch; pitch=width*3;
  873. _dstBuf=dstBuf; dstBuf=rgbBuf;
  874. }
  875. #endif
  876. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)
  877. *dinfo->output_height))==NULL)
  878. _throw("tjDecompress2(): Memory allocation failure");
  879. for(i=0; i<(int)dinfo->output_height; i++)
  880. {
  881. if(flags&TJFLAG_BOTTOMUP)
  882. row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch];
  883. else row_pointer[i]=&dstBuf[i*pitch];
  884. }
  885. while(dinfo->output_scanline<dinfo->output_height)
  886. {
  887. jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
  888. dinfo->output_height-dinfo->output_scanline);
  889. }
  890. jpeg_finish_decompress(dinfo);
  891. #ifndef JCS_EXTENSIONS
  892. fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat);
  893. #endif
  894. bailout:
  895. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  896. #ifndef JCS_EXTENSIONS
  897. if(rgbBuf) free(rgbBuf);
  898. #endif
  899. if(row_pointer) free(row_pointer);
  900. return retval;
  901. }
  902. DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf,
  903. unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
  904. int height, int pixelSize, int flags)
  905. {
  906. if(flags&TJ_YUV)
  907. return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
  908. else
  909. return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
  910. height, getPixelFormat(pixelSize, flags), flags);
  911. }
  912. DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
  913. unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  914. int flags)
  915. {
  916. int i, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS];
  917. int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS],
  918. tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
  919. JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf; JSAMPROW *tmpbuf[MAX_COMPONENTS];
  920. getinstance(handle);
  921. for(i=0; i<MAX_COMPONENTS; i++)
  922. {
  923. tmpbuf[i]=NULL; outbuf[i]=NULL;
  924. }
  925. if((this->init&DECOMPRESS)==0)
  926. _throw("tjDecompressToYUV(): Instance has not been initialized for decompression");
  927. if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL)
  928. _throw("tjDecompressToYUV(): Invalid argument");
  929. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  930. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  931. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  932. if(setjmp(this->jerr.setjmp_buffer))
  933. {
  934. /* If we get here, the JPEG code has signaled an error. */
  935. retval=-1;
  936. goto bailout;
  937. }
  938. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  939. jpeg_read_header(dinfo, TRUE);
  940. for(i=0; i<dinfo->num_components; i++)
  941. {
  942. jpeg_component_info *compptr=&dinfo->comp_info[i];
  943. int ih;
  944. iw[i]=compptr->width_in_blocks*DCTSIZE;
  945. ih=compptr->height_in_blocks*DCTSIZE;
  946. cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor)
  947. *compptr->h_samp_factor/dinfo->max_h_samp_factor;
  948. ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor)
  949. *compptr->v_samp_factor/dinfo->max_v_samp_factor;
  950. if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1;
  951. th[i]=compptr->v_samp_factor*DCTSIZE;
  952. tmpbufsize+=iw[i]*th[i];
  953. if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL)
  954. _throw("tjDecompressToYUV(): Memory allocation failure");
  955. for(row=0; row<ch[i]; row++)
  956. {
  957. outbuf[i][row]=ptr;
  958. ptr+=PAD(cw[i], 4);
  959. }
  960. }
  961. if(usetmpbuf)
  962. {
  963. if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
  964. _throw("tjDecompressToYUV(): Memory allocation failure");
  965. ptr=_tmpbuf;
  966. for(i=0; i<dinfo->num_components; i++)
  967. {
  968. if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
  969. _throw("tjDecompressToYUV(): Memory allocation failure");
  970. for(row=0; row<th[i]; row++)
  971. {
  972. tmpbuf[i][row]=ptr;
  973. ptr+=iw[i];
  974. }
  975. }
  976. }
  977. if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
  978. if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;
  979. dinfo->raw_data_out=TRUE;
  980. jpeg_start_decompress(dinfo);
  981. for(row=0; row<(int)dinfo->output_height;
  982. row+=dinfo->max_v_samp_factor*DCTSIZE)
  983. {
  984. JSAMPARRAY yuvptr[MAX_COMPONENTS];
  985. int crow[MAX_COMPONENTS];
  986. for(i=0; i<dinfo->num_components; i++)
  987. {
  988. jpeg_component_info *compptr=&dinfo->comp_info[i];
  989. crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor;
  990. if(usetmpbuf) yuvptr[i]=tmpbuf[i];
  991. else yuvptr[i]=&outbuf[i][crow[i]];
  992. }
  993. jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE);
  994. if(usetmpbuf)
  995. {
  996. int j;
  997. for(i=0; i<dinfo->num_components; i++)
  998. {
  999. for(j=0; j<min(th[i], ch[i]-crow[i]); j++)
  1000. {
  1001. memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. jpeg_finish_decompress(dinfo);
  1007. bailout:
  1008. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1009. for(i=0; i<MAX_COMPONENTS; i++)
  1010. {
  1011. if(tmpbuf[i]) free(tmpbuf[i]);
  1012. if(outbuf[i]) free(outbuf[i]);
  1013. }
  1014. if(_tmpbuf) free(_tmpbuf);
  1015. return retval;
  1016. }
  1017. /* Transformer */
  1018. DLLEXPORT tjhandle DLLCALL tjInitTransform(void)
  1019. {
  1020. tjinstance *this=NULL; tjhandle handle=NULL;
  1021. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  1022. {
  1023. snprintf(errStr, JMSG_LENGTH_MAX,
  1024. "tjInitTransform(): Memory allocation failure");
  1025. return NULL;
  1026. }
  1027. MEMZERO(this, sizeof(tjinstance));
  1028. handle=_tjInitCompress(this);
  1029. if(!handle) return NULL;
  1030. handle=_tjInitDecompress(this);
  1031. return handle;
  1032. }
  1033. DLLEXPORT int DLLCALL tjTransform(tjhandle handle, unsigned char *jpegBuf,
  1034. unsigned long jpegSize, int n, unsigned char **dstBufs,
  1035. unsigned long *dstSizes, tjtransform *t, int flags)
  1036. {
  1037. jpeg_transform_info *xinfo=NULL;
  1038. jvirt_barray_ptr *srccoefs, *dstcoefs;
  1039. int retval=0, i, jpegSubsamp;
  1040. getinstance(handle);
  1041. if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0)
  1042. _throw("tjTransform(): Instance has not been initialized for transformation");
  1043. if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL
  1044. || t==NULL || flags<0)
  1045. _throw("tjTransform(): Invalid argument");
  1046. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1047. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1048. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  1049. if(setjmp(this->jerr.setjmp_buffer))
  1050. {
  1051. /* If we get here, the JPEG code has signaled an error. */
  1052. retval=-1;
  1053. goto bailout;
  1054. }
  1055. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1056. if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n))
  1057. ==NULL)
  1058. _throw("tjTransform(): Memory allocation failure");
  1059. MEMZERO(xinfo, sizeof(jpeg_transform_info)*n);
  1060. for(i=0; i<n; i++)
  1061. {
  1062. xinfo[i].transform=xformtypes[t[i].op];
  1063. xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0;
  1064. xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0;
  1065. xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0;
  1066. xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0;
  1067. if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1;
  1068. else xinfo[i].slow_hflip=0;
  1069. if(xinfo[i].crop)
  1070. {
  1071. xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS;
  1072. xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS;
  1073. if(t[i].r.w!=0)
  1074. {
  1075. xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS;
  1076. }
  1077. else xinfo[i].crop_width=JCROP_UNSET;
  1078. if(t[i].r.h!=0)
  1079. {
  1080. xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS;
  1081. }
  1082. else xinfo[i].crop_height=JCROP_UNSET;
  1083. }
  1084. }
  1085. jcopy_markers_setup(dinfo, JCOPYOPT_ALL);
  1086. jpeg_read_header(dinfo, TRUE);
  1087. jpegSubsamp=getSubsamp(dinfo);
  1088. if(jpegSubsamp<0)
  1089. _throw("tjTransform(): Could not determine subsampling type for JPEG image");
  1090. for(i=0; i<n; i++)
  1091. {
  1092. if(!jtransform_request_workspace(dinfo, &xinfo[i]))
  1093. _throw("tjTransform(): Transform is not perfect");
  1094. if(xinfo[i].crop)
  1095. {
  1096. if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0
  1097. || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0)
  1098. {
  1099. snprintf(errStr, JMSG_LENGTH_MAX,
  1100. "To crop this JPEG image, x must be a multiple of %d\n"
  1101. "and y must be a multiple of %d.\n",
  1102. xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height);
  1103. retval=-1; goto bailout;
  1104. }
  1105. }
  1106. }
  1107. srccoefs=jpeg_read_coefficients(dinfo);
  1108. for(i=0; i<n; i++)
  1109. {
  1110. int w, h, alloc=1;
  1111. if(!xinfo[i].crop)
  1112. {
  1113. w=dinfo->image_width; h=dinfo->image_height;
  1114. }
  1115. else
  1116. {
  1117. w=xinfo[i].crop_width; h=xinfo[i].crop_height;
  1118. }
  1119. if(flags&TJFLAG_NOREALLOC)
  1120. {
  1121. alloc=0; dstSizes[i]=tjBufSize(w, h, jpegSubsamp);
  1122. }
  1123. if(!(t[i].options&TJXOPT_NOOUTPUT))
  1124. jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc);
  1125. jpeg_copy_critical_parameters(dinfo, cinfo);
  1126. dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs,
  1127. &xinfo[i]);
  1128. if(!(t[i].options&TJXOPT_NOOUTPUT))
  1129. {
  1130. jpeg_write_coefficients(cinfo, dstcoefs);
  1131. jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL);
  1132. }
  1133. else jinit_c_master_control(cinfo, TRUE);
  1134. jtransform_execute_transformation(dinfo, cinfo, srccoefs,
  1135. &xinfo[i]);
  1136. if(t[i].customFilter)
  1137. {
  1138. int ci, y; JDIMENSION by;
  1139. for(ci=0; ci<cinfo->num_components; ci++)
  1140. {
  1141. jpeg_component_info *compptr=&cinfo->comp_info[ci];
  1142. tjregion arrayRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1143. DCTSIZE};
  1144. tjregion planeRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1145. compptr->height_in_blocks*DCTSIZE};
  1146. for(by=0; by<compptr->height_in_blocks; by+=compptr->v_samp_factor)
  1147. {
  1148. JBLOCKARRAY barray=(dinfo->mem->access_virt_barray)
  1149. ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor,
  1150. TRUE);
  1151. for(y=0; y<compptr->v_samp_factor; y++)
  1152. {
  1153. if(t[i].customFilter(barray[y][0], arrayRegion, planeRegion,
  1154. ci, i, &t[i])==-1)
  1155. _throw("tjTransform(): Error in custom filter");
  1156. arrayRegion.y+=DCTSIZE;
  1157. }
  1158. }
  1159. }
  1160. }
  1161. if(!(t[i].options&TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo);
  1162. }
  1163. jpeg_finish_decompress(dinfo);
  1164. bailout:
  1165. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  1166. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1167. if(xinfo) free(xinfo);
  1168. return retval;
  1169. }