turbojpeg.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. /*
  2. * Copyright (C)2009-2016 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 <ctype.h>
  33. #include <jinclude.h>
  34. #define JPEG_INTERNALS
  35. #include <jpeglib.h>
  36. #include <jerror.h>
  37. #include <setjmp.h>
  38. #include "./turbojpeg.h"
  39. #include "./tjutil.h"
  40. #include "transupp.h"
  41. #include "./jpegcomp.h"
  42. extern void jpeg_mem_dest_tj(j_compress_ptr, unsigned char **,
  43. unsigned long *, boolean);
  44. extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *,
  45. unsigned long);
  46. #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
  47. #define isPow2(x) (((x)&(x-1))==0)
  48. /* Error handling (based on example in example.c) */
  49. static char errStr[JMSG_LENGTH_MAX]="No error";
  50. struct my_error_mgr
  51. {
  52. struct jpeg_error_mgr pub;
  53. jmp_buf setjmp_buffer;
  54. void (*emit_message)(j_common_ptr, int);
  55. boolean warning;
  56. };
  57. typedef struct my_error_mgr *my_error_ptr;
  58. static void my_error_exit(j_common_ptr cinfo)
  59. {
  60. my_error_ptr myerr=(my_error_ptr)cinfo->err;
  61. (*cinfo->err->output_message)(cinfo);
  62. longjmp(myerr->setjmp_buffer, 1);
  63. }
  64. /* Based on output_message() in jerror.c */
  65. static void my_output_message(j_common_ptr cinfo)
  66. {
  67. (*cinfo->err->format_message)(cinfo, errStr);
  68. }
  69. static void my_emit_message(j_common_ptr cinfo, int msg_level)
  70. {
  71. my_error_ptr myerr=(my_error_ptr)cinfo->err;
  72. myerr->emit_message(cinfo, msg_level);
  73. if(msg_level<0) myerr->warning=TRUE;
  74. }
  75. /* Global structures, macros, etc. */
  76. enum {COMPRESS=1, DECOMPRESS=2};
  77. typedef struct _tjinstance
  78. {
  79. struct jpeg_compress_struct cinfo;
  80. struct jpeg_decompress_struct dinfo;
  81. struct my_error_mgr jerr;
  82. int init, headerRead;
  83. } tjinstance;
  84. static const int pixelsize[TJ_NUMSAMP]={3, 3, 3, 1, 3, 3};
  85. static const JXFORM_CODE xformtypes[TJ_NUMXOP]=
  86. {
  87. JXFORM_NONE, JXFORM_FLIP_H, JXFORM_FLIP_V, JXFORM_TRANSPOSE,
  88. JXFORM_TRANSVERSE, JXFORM_ROT_90, JXFORM_ROT_180, JXFORM_ROT_270
  89. };
  90. #define NUMSF 16
  91. static const tjscalingfactor sf[NUMSF]={
  92. {2, 1},
  93. {15, 8},
  94. {7, 4},
  95. {13, 8},
  96. {3, 2},
  97. {11, 8},
  98. {5, 4},
  99. {9, 8},
  100. {1, 1},
  101. {7, 8},
  102. {3, 4},
  103. {5, 8},
  104. {1, 2},
  105. {3, 8},
  106. {1, 4},
  107. {1, 8}
  108. };
  109. #define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
  110. retval=-1; goto bailout;}
  111. #define getinstance(handle) tjinstance *this=(tjinstance *)handle; \
  112. j_compress_ptr cinfo=NULL; j_decompress_ptr dinfo=NULL; \
  113. if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
  114. return -1;} \
  115. cinfo=&this->cinfo; dinfo=&this->dinfo; \
  116. this->jerr.warning=FALSE;
  117. #define getcinstance(handle) tjinstance *this=(tjinstance *)handle; \
  118. j_compress_ptr cinfo=NULL; \
  119. if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
  120. return -1;} \
  121. cinfo=&this->cinfo; \
  122. this->jerr.warning=FALSE;
  123. #define getdinstance(handle) tjinstance *this=(tjinstance *)handle; \
  124. j_decompress_ptr dinfo=NULL; \
  125. if(!this) {snprintf(errStr, JMSG_LENGTH_MAX, "Invalid handle"); \
  126. return -1;} \
  127. dinfo=&this->dinfo; \
  128. this->jerr.warning=FALSE;
  129. static int getPixelFormat(int pixelSize, int flags)
  130. {
  131. if(pixelSize==1) return TJPF_GRAY;
  132. if(pixelSize==3)
  133. {
  134. if(flags&TJ_BGR) return TJPF_BGR;
  135. else return TJPF_RGB;
  136. }
  137. if(pixelSize==4)
  138. {
  139. if(flags&TJ_ALPHAFIRST)
  140. {
  141. if(flags&TJ_BGR) return TJPF_XBGR;
  142. else return TJPF_XRGB;
  143. }
  144. else
  145. {
  146. if(flags&TJ_BGR) return TJPF_BGRX;
  147. else return TJPF_RGBX;
  148. }
  149. }
  150. return -1;
  151. }
  152. static int setCompDefaults(struct jpeg_compress_struct *cinfo,
  153. int pixelFormat, int subsamp, int jpegQual, int flags)
  154. {
  155. int retval=0;
  156. char *env=NULL;
  157. switch(pixelFormat)
  158. {
  159. case TJPF_GRAY:
  160. cinfo->in_color_space=JCS_GRAYSCALE; break;
  161. #if JCS_EXTENSIONS==1
  162. case TJPF_RGB:
  163. cinfo->in_color_space=JCS_EXT_RGB; break;
  164. case TJPF_BGR:
  165. cinfo->in_color_space=JCS_EXT_BGR; break;
  166. case TJPF_RGBX:
  167. case TJPF_RGBA:
  168. cinfo->in_color_space=JCS_EXT_RGBX; break;
  169. case TJPF_BGRX:
  170. case TJPF_BGRA:
  171. cinfo->in_color_space=JCS_EXT_BGRX; break;
  172. case TJPF_XRGB:
  173. case TJPF_ARGB:
  174. cinfo->in_color_space=JCS_EXT_XRGB; break;
  175. case TJPF_XBGR:
  176. case TJPF_ABGR:
  177. cinfo->in_color_space=JCS_EXT_XBGR; break;
  178. #else
  179. case TJPF_RGB:
  180. case TJPF_BGR:
  181. case TJPF_RGBX:
  182. case TJPF_BGRX:
  183. case TJPF_XRGB:
  184. case TJPF_XBGR:
  185. case TJPF_RGBA:
  186. case TJPF_BGRA:
  187. case TJPF_ARGB:
  188. case TJPF_ABGR:
  189. cinfo->in_color_space=JCS_RGB; pixelFormat=TJPF_RGB;
  190. break;
  191. #endif
  192. case TJPF_CMYK:
  193. cinfo->in_color_space=JCS_CMYK; break;
  194. }
  195. cinfo->input_components=tjPixelSize[pixelFormat];
  196. jpeg_set_defaults(cinfo);
  197. #ifndef NO_GETENV
  198. if((env=getenv("TJ_OPTIMIZE"))!=NULL && strlen(env)>0 && !strcmp(env, "1"))
  199. cinfo->optimize_coding=TRUE;
  200. if((env=getenv("TJ_ARITHMETIC"))!=NULL && strlen(env)>0 && !strcmp(env, "1"))
  201. cinfo->arith_code=TRUE;
  202. if((env=getenv("TJ_RESTART"))!=NULL && strlen(env)>0)
  203. {
  204. int temp=-1; char tempc=0;
  205. if(sscanf(env, "%d%c", &temp, &tempc)>=1 && temp>=0 && temp<=65535)
  206. {
  207. if(toupper(tempc)=='B')
  208. {
  209. cinfo->restart_interval=temp;
  210. cinfo->restart_in_rows=0;
  211. }
  212. else
  213. cinfo->restart_in_rows=temp;
  214. }
  215. }
  216. #endif
  217. if(jpegQual>=0)
  218. {
  219. jpeg_set_quality(cinfo, jpegQual, TRUE);
  220. if(jpegQual>=96 || flags&TJFLAG_ACCURATEDCT) cinfo->dct_method=JDCT_ISLOW;
  221. else cinfo->dct_method=JDCT_FASTEST;
  222. }
  223. if(subsamp==TJSAMP_GRAY)
  224. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  225. else if(pixelFormat==TJPF_CMYK)
  226. jpeg_set_colorspace(cinfo, JCS_YCCK);
  227. else jpeg_set_colorspace(cinfo, JCS_YCbCr);
  228. #ifndef NO_GETENV
  229. if((env=getenv("TJ_PROGRESSIVE"))!=NULL && strlen(env)>0
  230. && !strcmp(env, "1"))
  231. jpeg_simple_progression(cinfo);
  232. #endif
  233. cinfo->comp_info[0].h_samp_factor=tjMCUWidth[subsamp]/8;
  234. cinfo->comp_info[1].h_samp_factor=1;
  235. cinfo->comp_info[2].h_samp_factor=1;
  236. if(cinfo->num_components>3)
  237. cinfo->comp_info[3].h_samp_factor=tjMCUWidth[subsamp]/8;
  238. cinfo->comp_info[0].v_samp_factor=tjMCUHeight[subsamp]/8;
  239. cinfo->comp_info[1].v_samp_factor=1;
  240. cinfo->comp_info[2].v_samp_factor=1;
  241. if(cinfo->num_components>3)
  242. cinfo->comp_info[3].v_samp_factor=tjMCUHeight[subsamp]/8;
  243. return retval;
  244. }
  245. static int setDecompDefaults(struct jpeg_decompress_struct *dinfo,
  246. int pixelFormat, int flags)
  247. {
  248. int retval=0;
  249. switch(pixelFormat)
  250. {
  251. case TJPF_GRAY:
  252. dinfo->out_color_space=JCS_GRAYSCALE; break;
  253. #if JCS_EXTENSIONS==1
  254. case TJPF_RGB:
  255. dinfo->out_color_space=JCS_EXT_RGB; break;
  256. case TJPF_BGR:
  257. dinfo->out_color_space=JCS_EXT_BGR; break;
  258. case TJPF_RGBX:
  259. dinfo->out_color_space=JCS_EXT_RGBX; break;
  260. case TJPF_BGRX:
  261. dinfo->out_color_space=JCS_EXT_BGRX; break;
  262. case TJPF_XRGB:
  263. dinfo->out_color_space=JCS_EXT_XRGB; break;
  264. case TJPF_XBGR:
  265. dinfo->out_color_space=JCS_EXT_XBGR; break;
  266. #if JCS_ALPHA_EXTENSIONS==1
  267. case TJPF_RGBA:
  268. dinfo->out_color_space=JCS_EXT_RGBA; break;
  269. case TJPF_BGRA:
  270. dinfo->out_color_space=JCS_EXT_BGRA; break;
  271. case TJPF_ARGB:
  272. dinfo->out_color_space=JCS_EXT_ARGB; break;
  273. case TJPF_ABGR:
  274. dinfo->out_color_space=JCS_EXT_ABGR; break;
  275. #endif
  276. #else
  277. case TJPF_RGB:
  278. case TJPF_BGR:
  279. case TJPF_RGBX:
  280. case TJPF_BGRX:
  281. case TJPF_XRGB:
  282. case TJPF_XBGR:
  283. case TJPF_RGBA:
  284. case TJPF_BGRA:
  285. case TJPF_ARGB:
  286. case TJPF_ABGR:
  287. dinfo->out_color_space=JCS_RGB; break;
  288. #endif
  289. case TJPF_CMYK:
  290. dinfo->out_color_space=JCS_CMYK; break;
  291. default:
  292. _throw("Unsupported pixel format");
  293. }
  294. if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;
  295. bailout:
  296. return retval;
  297. }
  298. static int getSubsamp(j_decompress_ptr dinfo)
  299. {
  300. int retval=-1, i, k;
  301. /* The sampling factors actually have no meaning with grayscale JPEG files,
  302. and in fact it's possible to generate grayscale JPEGs with sampling
  303. factors > 1 (even though those sampling factors are ignored by the
  304. decompressor.) Thus, we need to treat grayscale as a special case. */
  305. if(dinfo->num_components==1 && dinfo->jpeg_color_space==JCS_GRAYSCALE)
  306. return TJSAMP_GRAY;
  307. for(i=0; i<NUMSUBOPT; i++)
  308. {
  309. if(dinfo->num_components==pixelsize[i]
  310. || ((dinfo->jpeg_color_space==JCS_YCCK
  311. || dinfo->jpeg_color_space==JCS_CMYK)
  312. && pixelsize[i]==3 && dinfo->num_components==4))
  313. {
  314. if(dinfo->comp_info[0].h_samp_factor==tjMCUWidth[i]/8
  315. && dinfo->comp_info[0].v_samp_factor==tjMCUHeight[i]/8)
  316. {
  317. int match=0;
  318. for(k=1; k<dinfo->num_components; k++)
  319. {
  320. int href=1, vref=1;
  321. if(dinfo->jpeg_color_space==JCS_YCCK && k==3)
  322. {
  323. href=tjMCUWidth[i]/8; vref=tjMCUHeight[i]/8;
  324. }
  325. if(dinfo->comp_info[k].h_samp_factor==href
  326. && dinfo->comp_info[k].v_samp_factor==vref)
  327. match++;
  328. }
  329. if(match==dinfo->num_components-1)
  330. {
  331. retval=i; break;
  332. }
  333. }
  334. /* Handle 4:2:2 and 4:4:0 images whose sampling factors are specified
  335. in non-standard ways. */
  336. if(dinfo->comp_info[0].h_samp_factor==2 &&
  337. dinfo->comp_info[0].v_samp_factor==2 &&
  338. (i==TJSAMP_422 || i==TJSAMP_440))
  339. {
  340. int match=0;
  341. for(k=1; k<dinfo->num_components; k++)
  342. {
  343. int href=tjMCUHeight[i]/8, vref=tjMCUWidth[i]/8;
  344. if(dinfo->jpeg_color_space==JCS_YCCK && k==3)
  345. {
  346. href=vref=2;
  347. }
  348. if(dinfo->comp_info[k].h_samp_factor==href
  349. && dinfo->comp_info[k].v_samp_factor==vref)
  350. match++;
  351. }
  352. if(match==dinfo->num_components-1)
  353. {
  354. retval=i; break;
  355. }
  356. }
  357. }
  358. }
  359. return retval;
  360. }
  361. #ifndef JCS_EXTENSIONS
  362. /* Conversion functions to emulate the colorspace extensions. This allows the
  363. TurboJPEG wrapper to be used with libjpeg */
  364. #define TORGB(PS, ROFFSET, GOFFSET, BOFFSET) { \
  365. int rowPad=pitch-width*PS; \
  366. while(height--) \
  367. { \
  368. unsigned char *endOfRow=src+width*PS; \
  369. while(src<endOfRow) \
  370. { \
  371. dst[RGB_RED]=src[ROFFSET]; \
  372. dst[RGB_GREEN]=src[GOFFSET]; \
  373. dst[RGB_BLUE]=src[BOFFSET]; \
  374. dst+=RGB_PIXELSIZE; src+=PS; \
  375. } \
  376. src+=rowPad; \
  377. } \
  378. }
  379. static unsigned char *toRGB(unsigned char *src, int width, int pitch,
  380. int height, int pixelFormat, unsigned char *dst)
  381. {
  382. unsigned char *retval=src;
  383. switch(pixelFormat)
  384. {
  385. case TJPF_RGB:
  386. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  387. retval=dst; TORGB(3, 0, 1, 2);
  388. #endif
  389. break;
  390. case TJPF_BGR:
  391. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  392. retval=dst; TORGB(3, 2, 1, 0);
  393. #endif
  394. break;
  395. case TJPF_RGBX:
  396. case TJPF_RGBA:
  397. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  398. retval=dst; TORGB(4, 0, 1, 2);
  399. #endif
  400. break;
  401. case TJPF_BGRX:
  402. case TJPF_BGRA:
  403. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  404. retval=dst; TORGB(4, 2, 1, 0);
  405. #endif
  406. break;
  407. case TJPF_XRGB:
  408. case TJPF_ARGB:
  409. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  410. retval=dst; TORGB(4, 1, 2, 3);
  411. #endif
  412. break;
  413. case TJPF_XBGR:
  414. case TJPF_ABGR:
  415. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  416. retval=dst; TORGB(4, 3, 2, 1);
  417. #endif
  418. break;
  419. }
  420. return retval;
  421. }
  422. #define FROMRGB(PS, ROFFSET, GOFFSET, BOFFSET, SETALPHA) { \
  423. int rowPad=pitch-width*PS; \
  424. while(height--) \
  425. { \
  426. unsigned char *endOfRow=dst+width*PS; \
  427. while(dst<endOfRow) \
  428. { \
  429. dst[ROFFSET]=src[RGB_RED]; \
  430. dst[GOFFSET]=src[RGB_GREEN]; \
  431. dst[BOFFSET]=src[RGB_BLUE]; \
  432. SETALPHA \
  433. dst+=PS; src+=RGB_PIXELSIZE; \
  434. } \
  435. dst+=rowPad; \
  436. } \
  437. }
  438. static void fromRGB(unsigned char *src, unsigned char *dst, int width,
  439. int pitch, int height, int pixelFormat)
  440. {
  441. switch(pixelFormat)
  442. {
  443. case TJPF_RGB:
  444. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=3
  445. FROMRGB(3, 0, 1, 2,);
  446. #endif
  447. break;
  448. case TJPF_BGR:
  449. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=3
  450. FROMRGB(3, 2, 1, 0,);
  451. #endif
  452. break;
  453. case TJPF_RGBX:
  454. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  455. FROMRGB(4, 0, 1, 2,);
  456. #endif
  457. break;
  458. case TJPF_RGBA:
  459. #if RGB_RED!=0 || RGB_GREEN!=1 || RGB_BLUE!=2 || RGB_PIXELSIZE!=4
  460. FROMRGB(4, 0, 1, 2, dst[3]=0xFF;);
  461. #endif
  462. break;
  463. case TJPF_BGRX:
  464. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  465. FROMRGB(4, 2, 1, 0,);
  466. #endif
  467. break;
  468. case TJPF_BGRA:
  469. #if RGB_RED!=2 || RGB_GREEN!=1 || RGB_BLUE!=0 || RGB_PIXELSIZE!=4
  470. FROMRGB(4, 2, 1, 0, dst[3]=0xFF;); return;
  471. #endif
  472. break;
  473. case TJPF_XRGB:
  474. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  475. FROMRGB(4, 1, 2, 3,); return;
  476. #endif
  477. break;
  478. case TJPF_ARGB:
  479. #if RGB_RED!=1 || RGB_GREEN!=2 || RGB_BLUE!=3 || RGB_PIXELSIZE!=4
  480. FROMRGB(4, 1, 2, 3, dst[0]=0xFF;); return;
  481. #endif
  482. break;
  483. case TJPF_XBGR:
  484. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  485. FROMRGB(4, 3, 2, 1,); return;
  486. #endif
  487. break;
  488. case TJPF_ABGR:
  489. #if RGB_RED!=3 || RGB_GREEN!=2 || RGB_BLUE!=1 || RGB_PIXELSIZE!=4
  490. FROMRGB(4, 3, 2, 1, dst[0]=0xFF;); return;
  491. #endif
  492. break;
  493. }
  494. }
  495. #endif
  496. /* General API functions */
  497. DLLEXPORT char* DLLCALL tjGetErrorStr(void)
  498. {
  499. return errStr;
  500. }
  501. DLLEXPORT int DLLCALL tjDestroy(tjhandle handle)
  502. {
  503. getinstance(handle);
  504. if(setjmp(this->jerr.setjmp_buffer)) return -1;
  505. if(this->init&COMPRESS) jpeg_destroy_compress(cinfo);
  506. if(this->init&DECOMPRESS) jpeg_destroy_decompress(dinfo);
  507. free(this);
  508. return 0;
  509. }
  510. /* These are exposed mainly because Windows can't malloc() and free() across
  511. DLL boundaries except when the CRT DLL is used, and we don't use the CRT DLL
  512. with turbojpeg.dll for compatibility reasons. However, these functions
  513. can potentially be used for other purposes by different implementations. */
  514. DLLEXPORT void DLLCALL tjFree(unsigned char *buf)
  515. {
  516. if(buf) free(buf);
  517. }
  518. DLLEXPORT unsigned char *DLLCALL tjAlloc(int bytes)
  519. {
  520. return (unsigned char *)malloc(bytes);
  521. }
  522. /* Compressor */
  523. static tjhandle _tjInitCompress(tjinstance *this)
  524. {
  525. static unsigned char buffer[1];
  526. unsigned char *buf=buffer; unsigned long size=1;
  527. /* This is also straight out of example.c */
  528. this->cinfo.err=jpeg_std_error(&this->jerr.pub);
  529. this->jerr.pub.error_exit=my_error_exit;
  530. this->jerr.pub.output_message=my_output_message;
  531. this->jerr.emit_message=this->jerr.pub.emit_message;
  532. this->jerr.pub.emit_message=my_emit_message;
  533. if(setjmp(this->jerr.setjmp_buffer))
  534. {
  535. /* If we get here, the JPEG code has signaled an error. */
  536. if(this) free(this);
  537. return NULL;
  538. }
  539. jpeg_create_compress(&this->cinfo);
  540. /* Make an initial call so it will create the destination manager */
  541. jpeg_mem_dest_tj(&this->cinfo, &buf, &size, 0);
  542. this->init|=COMPRESS;
  543. return (tjhandle)this;
  544. }
  545. DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
  546. {
  547. tjinstance *this=NULL;
  548. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  549. {
  550. snprintf(errStr, JMSG_LENGTH_MAX,
  551. "tjInitCompress(): Memory allocation failure");
  552. return NULL;
  553. }
  554. MEMZERO(this, sizeof(tjinstance));
  555. return _tjInitCompress(this);
  556. }
  557. DLLEXPORT unsigned long DLLCALL tjBufSize(int width, int height,
  558. int jpegSubsamp)
  559. {
  560. unsigned long retval=0; int mcuw, mcuh, chromasf;
  561. if(width<1 || height<1 || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT)
  562. _throw("tjBufSize(): Invalid argument");
  563. /* This allows for rare corner cases in which a JPEG image can actually be
  564. larger than the uncompressed input (we wouldn't mention it if it hadn't
  565. happened before.) */
  566. mcuw=tjMCUWidth[jpegSubsamp];
  567. mcuh=tjMCUHeight[jpegSubsamp];
  568. chromasf=jpegSubsamp==TJSAMP_GRAY? 0: 4*64/(mcuw*mcuh);
  569. retval=PAD(width, mcuw) * PAD(height, mcuh) * (2 + chromasf) + 2048;
  570. bailout:
  571. return retval;
  572. }
  573. DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
  574. {
  575. unsigned long retval=0;
  576. if(width<1 || height<1)
  577. _throw("TJBUFSIZE(): Invalid argument");
  578. /* This allows for rare corner cases in which a JPEG image can actually be
  579. larger than the uncompressed input (we wouldn't mention it if it hadn't
  580. happened before.) */
  581. retval=PAD(width, 16) * PAD(height, 16) * 6 + 2048;
  582. bailout:
  583. return retval;
  584. }
  585. DLLEXPORT unsigned long DLLCALL tjBufSizeYUV2(int width, int pad, int height,
  586. int subsamp)
  587. {
  588. int retval=0, nc, i;
  589. if(subsamp<0 || subsamp>=NUMSUBOPT)
  590. _throw("tjBufSizeYUV2(): Invalid argument");
  591. nc=(subsamp==TJSAMP_GRAY? 1:3);
  592. for(i=0; i<nc; i++)
  593. {
  594. int pw=tjPlaneWidth(i, width, subsamp);
  595. int stride=PAD(pw, pad);
  596. int ph=tjPlaneHeight(i, height, subsamp);
  597. if(pw<0 || ph<0) return -1;
  598. else retval+=stride*ph;
  599. }
  600. bailout:
  601. return retval;
  602. }
  603. DLLEXPORT unsigned long DLLCALL tjBufSizeYUV(int width, int height,
  604. int subsamp)
  605. {
  606. return tjBufSizeYUV2(width, 4, height, subsamp);
  607. }
  608. DLLEXPORT unsigned long DLLCALL TJBUFSIZEYUV(int width, int height,
  609. int subsamp)
  610. {
  611. return tjBufSizeYUV(width, height, subsamp);
  612. }
  613. DLLEXPORT int tjPlaneWidth(int componentID, int width, int subsamp)
  614. {
  615. int pw, nc, retval=0;
  616. if(width<1 || subsamp<0 || subsamp>=TJ_NUMSAMP)
  617. _throw("tjPlaneWidth(): Invalid argument");
  618. nc=(subsamp==TJSAMP_GRAY? 1:3);
  619. if(componentID<0 || componentID>=nc)
  620. _throw("tjPlaneWidth(): Invalid argument");
  621. pw=PAD(width, tjMCUWidth[subsamp]/8);
  622. if(componentID==0)
  623. retval=pw;
  624. else
  625. retval=pw*8/tjMCUWidth[subsamp];
  626. bailout:
  627. return retval;
  628. }
  629. DLLEXPORT int tjPlaneHeight(int componentID, int height, int subsamp)
  630. {
  631. int ph, nc, retval=0;
  632. if(height<1 || subsamp<0 || subsamp>=TJ_NUMSAMP)
  633. _throw("tjPlaneHeight(): Invalid argument");
  634. nc=(subsamp==TJSAMP_GRAY? 1:3);
  635. if(componentID<0 || componentID>=nc)
  636. _throw("tjPlaneHeight(): Invalid argument");
  637. ph=PAD(height, tjMCUHeight[subsamp]/8);
  638. if(componentID==0)
  639. retval=ph;
  640. else
  641. retval=ph*8/tjMCUHeight[subsamp];
  642. bailout:
  643. return retval;
  644. }
  645. DLLEXPORT unsigned long DLLCALL tjPlaneSizeYUV(int componentID, int width,
  646. int stride, int height, int subsamp)
  647. {
  648. unsigned long retval=0;
  649. int pw, ph;
  650. if(width<1 || height<1 || subsamp<0 || subsamp>=NUMSUBOPT)
  651. _throw("tjPlaneSizeYUV(): Invalid argument");
  652. pw=tjPlaneWidth(componentID, width, subsamp);
  653. ph=tjPlaneHeight(componentID, height, subsamp);
  654. if(pw<0 || ph<0) return -1;
  655. if(stride==0) stride=pw;
  656. else stride=abs(stride);
  657. retval=stride*(ph-1)+pw;
  658. bailout:
  659. return retval;
  660. }
  661. DLLEXPORT int DLLCALL tjCompress2(tjhandle handle, const unsigned char *srcBuf,
  662. int width, int pitch, int height, int pixelFormat, unsigned char **jpegBuf,
  663. unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  664. {
  665. int i, retval=0, alloc=1; JSAMPROW *row_pointer=NULL;
  666. #ifndef JCS_EXTENSIONS
  667. unsigned char *rgbBuf=NULL;
  668. #endif
  669. getcinstance(handle)
  670. if((this->init&COMPRESS)==0)
  671. _throw("tjCompress2(): Instance has not been initialized for compression");
  672. if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  673. || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL
  674. || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100)
  675. _throw("tjCompress2(): Invalid argument");
  676. if(setjmp(this->jerr.setjmp_buffer))
  677. {
  678. /* If we get here, the JPEG code has signaled an error. */
  679. retval=-1;
  680. goto bailout;
  681. }
  682. if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
  683. #ifndef JCS_EXTENSIONS
  684. if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK)
  685. {
  686. rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  687. if(!rgbBuf) _throw("tjCompress2(): Memory allocation failure");
  688. srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  689. pitch=width*RGB_PIXELSIZE;
  690. }
  691. #endif
  692. cinfo->image_width=width;
  693. cinfo->image_height=height;
  694. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  695. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  696. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  697. if(flags&TJFLAG_NOREALLOC)
  698. {
  699. alloc=0; *jpegSize=tjBufSize(width, height, jpegSubsamp);
  700. }
  701. jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
  702. if(setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual, flags)==-1)
  703. return -1;
  704. jpeg_start_compress(cinfo, TRUE);
  705. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
  706. _throw("tjCompress2(): Memory allocation failure");
  707. for(i=0; i<height; i++)
  708. {
  709. if(flags&TJFLAG_BOTTOMUP)
  710. row_pointer[i]=(JSAMPROW)&srcBuf[(height-i-1)*pitch];
  711. else row_pointer[i]=(JSAMPROW)&srcBuf[i*pitch];
  712. }
  713. while(cinfo->next_scanline<cinfo->image_height)
  714. {
  715. jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
  716. cinfo->image_height-cinfo->next_scanline);
  717. }
  718. jpeg_finish_compress(cinfo);
  719. bailout:
  720. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  721. #ifndef JCS_EXTENSIONS
  722. if(rgbBuf) free(rgbBuf);
  723. #endif
  724. if(row_pointer) free(row_pointer);
  725. if(this->jerr.warning) retval=-1;
  726. return retval;
  727. }
  728. DLLEXPORT int DLLCALL tjCompress(tjhandle handle, unsigned char *srcBuf,
  729. int width, int pitch, int height, int pixelSize, unsigned char *jpegBuf,
  730. unsigned long *jpegSize, int jpegSubsamp, int jpegQual, int flags)
  731. {
  732. int retval=0; unsigned long size;
  733. if(flags&TJ_YUV)
  734. {
  735. size=tjBufSizeYUV(width, height, jpegSubsamp);
  736. retval=tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  737. getPixelFormat(pixelSize, flags), jpegBuf, jpegSubsamp, flags);
  738. }
  739. else
  740. {
  741. retval=tjCompress2(handle, srcBuf, width, pitch, height,
  742. getPixelFormat(pixelSize, flags), &jpegBuf, &size, jpegSubsamp, jpegQual,
  743. flags|TJFLAG_NOREALLOC);
  744. }
  745. *jpegSize=size;
  746. return retval;
  747. }
  748. DLLEXPORT int DLLCALL tjEncodeYUVPlanes(tjhandle handle,
  749. const unsigned char *srcBuf, int width, int pitch, int height,
  750. int pixelFormat, unsigned char **dstPlanes, int *strides, int subsamp,
  751. int flags)
  752. {
  753. int i, retval=0; JSAMPROW *row_pointer=NULL;
  754. JSAMPLE *_tmpbuf[MAX_COMPONENTS], *_tmpbuf2[MAX_COMPONENTS];
  755. JSAMPROW *tmpbuf[MAX_COMPONENTS], *tmpbuf2[MAX_COMPONENTS];
  756. JSAMPROW *outbuf[MAX_COMPONENTS];
  757. int row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
  758. JSAMPLE *ptr;
  759. jpeg_component_info *compptr;
  760. #ifndef JCS_EXTENSIONS
  761. unsigned char *rgbBuf=NULL;
  762. #endif
  763. getcinstance(handle);
  764. for(i=0; i<MAX_COMPONENTS; i++)
  765. {
  766. tmpbuf[i]=NULL; _tmpbuf[i]=NULL;
  767. tmpbuf2[i]=NULL; _tmpbuf2[i]=NULL; outbuf[i]=NULL;
  768. }
  769. if((this->init&COMPRESS)==0)
  770. _throw("tjEncodeYUVPlanes(): Instance has not been initialized for compression");
  771. if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  772. || pixelFormat>=TJ_NUMPF || !dstPlanes || !dstPlanes[0] || subsamp<0
  773. || subsamp>=NUMSUBOPT)
  774. _throw("tjEncodeYUVPlanes(): Invalid argument");
  775. if(subsamp!=TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
  776. _throw("tjEncodeYUVPlanes(): Invalid argument");
  777. if(setjmp(this->jerr.setjmp_buffer))
  778. {
  779. /* If we get here, the JPEG code has signaled an error. */
  780. retval=-1;
  781. goto bailout;
  782. }
  783. if(pixelFormat==TJPF_CMYK)
  784. _throw("tjEncodeYUVPlanes(): Cannot generate YUV images from CMYK pixels");
  785. if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
  786. #ifndef JCS_EXTENSIONS
  787. if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK)
  788. {
  789. rgbBuf=(unsigned char *)malloc(width*height*RGB_PIXELSIZE);
  790. if(!rgbBuf) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  791. srcBuf=toRGB(srcBuf, width, pitch, height, pixelFormat, rgbBuf);
  792. pitch=width*RGB_PIXELSIZE;
  793. }
  794. #endif
  795. cinfo->image_width=width;
  796. cinfo->image_height=height;
  797. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  798. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  799. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  800. if(setCompDefaults(cinfo, pixelFormat, subsamp, -1, flags)==-1) return -1;
  801. /* Execute only the parts of jpeg_start_compress() that we need. If we
  802. were to call the whole jpeg_start_compress() function, then it would try
  803. to write the file headers, which could overflow the output buffer if the
  804. YUV image were very small. */
  805. if(cinfo->global_state!=CSTATE_START)
  806. _throw("tjEncodeYUVPlanes(): libjpeg API is in the wrong state");
  807. (*cinfo->err->reset_error_mgr)((j_common_ptr)cinfo);
  808. jinit_c_master_control(cinfo, FALSE);
  809. jinit_color_converter(cinfo);
  810. jinit_downsampler(cinfo);
  811. (*cinfo->cconvert->start_pass)(cinfo);
  812. pw0=PAD(width, cinfo->max_h_samp_factor);
  813. ph0=PAD(height, cinfo->max_v_samp_factor);
  814. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph0))==NULL)
  815. _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  816. for(i=0; i<height; i++)
  817. {
  818. if(flags&TJFLAG_BOTTOMUP)
  819. row_pointer[i]=(JSAMPROW)&srcBuf[(height-i-1)*pitch];
  820. else row_pointer[i]=(JSAMPROW)&srcBuf[i*pitch];
  821. }
  822. if(height<ph0)
  823. for(i=height; i<ph0; i++) row_pointer[i]=row_pointer[height-1];
  824. for(i=0; i<cinfo->num_components; i++)
  825. {
  826. compptr=&cinfo->comp_info[i];
  827. _tmpbuf[i]=(JSAMPLE *)malloc(
  828. PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  829. /compptr->h_samp_factor, 16) * cinfo->max_v_samp_factor + 16);
  830. if(!_tmpbuf[i]) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  831. tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*cinfo->max_v_samp_factor);
  832. if(!tmpbuf[i]) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  833. for(row=0; row<cinfo->max_v_samp_factor; row++)
  834. {
  835. unsigned char *_tmpbuf_aligned=
  836. (unsigned char *)PAD((size_t)_tmpbuf[i], 16);
  837. tmpbuf[i][row]=&_tmpbuf_aligned[
  838. PAD((compptr->width_in_blocks*cinfo->max_h_samp_factor*DCTSIZE)
  839. /compptr->h_samp_factor, 16) * row];
  840. }
  841. _tmpbuf2[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16)
  842. * compptr->v_samp_factor + 16);
  843. if(!_tmpbuf2[i]) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  844. tmpbuf2[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor);
  845. if(!tmpbuf2[i]) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  846. for(row=0; row<compptr->v_samp_factor; row++)
  847. {
  848. unsigned char *_tmpbuf2_aligned=
  849. (unsigned char *)PAD((size_t)_tmpbuf2[i], 16);
  850. tmpbuf2[i][row]=&_tmpbuf2_aligned[
  851. PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
  852. }
  853. pw[i]=pw0*compptr->h_samp_factor/cinfo->max_h_samp_factor;
  854. ph[i]=ph0*compptr->v_samp_factor/cinfo->max_v_samp_factor;
  855. outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph[i]);
  856. if(!outbuf[i]) _throw("tjEncodeYUVPlanes(): Memory allocation failure");
  857. ptr=dstPlanes[i];
  858. for(row=0; row<ph[i]; row++)
  859. {
  860. outbuf[i][row]=ptr;
  861. ptr+=(strides && strides[i]!=0)? strides[i]:pw[i];
  862. }
  863. }
  864. for(row=0; row<ph0; row+=cinfo->max_v_samp_factor)
  865. {
  866. (*cinfo->cconvert->color_convert)(cinfo, &row_pointer[row], tmpbuf, 0,
  867. cinfo->max_v_samp_factor);
  868. (cinfo->downsample->downsample)(cinfo, tmpbuf, 0, tmpbuf2, 0);
  869. for(i=0, compptr=cinfo->comp_info; i<cinfo->num_components; i++, compptr++)
  870. jcopy_sample_rows(tmpbuf2[i], 0, outbuf[i],
  871. row*compptr->v_samp_factor/cinfo->max_v_samp_factor,
  872. compptr->v_samp_factor, pw[i]);
  873. }
  874. cinfo->next_scanline+=height;
  875. jpeg_abort_compress(cinfo);
  876. bailout:
  877. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  878. #ifndef JCS_EXTENSIONS
  879. if(rgbBuf) free(rgbBuf);
  880. #endif
  881. if(row_pointer) free(row_pointer);
  882. for(i=0; i<MAX_COMPONENTS; i++)
  883. {
  884. if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
  885. if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
  886. if(tmpbuf2[i]!=NULL) free(tmpbuf2[i]);
  887. if(_tmpbuf2[i]!=NULL) free(_tmpbuf2[i]);
  888. if(outbuf[i]!=NULL) free(outbuf[i]);
  889. }
  890. if(this->jerr.warning) retval=-1;
  891. return retval;
  892. }
  893. DLLEXPORT int DLLCALL tjEncodeYUV3(tjhandle handle,
  894. const unsigned char *srcBuf, int width, int pitch, int height,
  895. int pixelFormat, unsigned char *dstBuf, int pad, int subsamp, int flags)
  896. {
  897. unsigned char *dstPlanes[3];
  898. int pw0, ph0, strides[3], retval=-1;
  899. if(width<=0 || height<=0 || dstBuf==NULL || pad<0 || !isPow2(pad)
  900. || subsamp<0 || subsamp>=NUMSUBOPT)
  901. _throw("tjEncodeYUV3(): Invalid argument");
  902. pw0=tjPlaneWidth(0, width, subsamp);
  903. ph0=tjPlaneHeight(0, height, subsamp);
  904. dstPlanes[0]=dstBuf;
  905. strides[0]=PAD(pw0, pad);
  906. if(subsamp==TJSAMP_GRAY)
  907. {
  908. strides[1]=strides[2]=0;
  909. dstPlanes[1]=dstPlanes[2]=NULL;
  910. }
  911. else
  912. {
  913. int pw1=tjPlaneWidth(1, width, subsamp);
  914. int ph1=tjPlaneHeight(1, height, subsamp);
  915. strides[1]=strides[2]=PAD(pw1, pad);
  916. dstPlanes[1]=dstPlanes[0]+strides[0]*ph0;
  917. dstPlanes[2]=dstPlanes[1]+strides[1]*ph1;
  918. }
  919. return tjEncodeYUVPlanes(handle, srcBuf, width, pitch, height, pixelFormat,
  920. dstPlanes, strides, subsamp, flags);
  921. bailout:
  922. return retval;
  923. }
  924. DLLEXPORT int DLLCALL tjEncodeYUV2(tjhandle handle, unsigned char *srcBuf,
  925. int width, int pitch, int height, int pixelFormat, unsigned char *dstBuf,
  926. int subsamp, int flags)
  927. {
  928. return tjEncodeYUV3(handle, srcBuf, width, pitch, height, pixelFormat,
  929. dstBuf, 4, subsamp, flags);
  930. }
  931. DLLEXPORT int DLLCALL tjEncodeYUV(tjhandle handle, unsigned char *srcBuf,
  932. int width, int pitch, int height, int pixelSize, unsigned char *dstBuf,
  933. int subsamp, int flags)
  934. {
  935. return tjEncodeYUV2(handle, srcBuf, width, pitch, height,
  936. getPixelFormat(pixelSize, flags), dstBuf, subsamp, flags);
  937. }
  938. DLLEXPORT int DLLCALL tjCompressFromYUVPlanes(tjhandle handle,
  939. const unsigned char **srcPlanes, int width, const int *strides, int height,
  940. int subsamp, unsigned char **jpegBuf, unsigned long *jpegSize, int jpegQual,
  941. int flags)
  942. {
  943. int i, row, retval=0, alloc=1; JSAMPROW *inbuf[MAX_COMPONENTS];
  944. int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
  945. tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
  946. JSAMPLE *_tmpbuf=NULL, *ptr; JSAMPROW *tmpbuf[MAX_COMPONENTS];
  947. getcinstance(handle)
  948. for(i=0; i<MAX_COMPONENTS; i++)
  949. {
  950. tmpbuf[i]=NULL; inbuf[i]=NULL;
  951. }
  952. if((this->init&COMPRESS)==0)
  953. _throw("tjCompressFromYUVPlanes(): Instance has not been initialized for compression");
  954. if(!srcPlanes || !srcPlanes[0] || width<=0 || height<=0 || subsamp<0
  955. || subsamp>=NUMSUBOPT || jpegBuf==NULL || jpegSize==NULL || jpegQual<0
  956. || jpegQual>100)
  957. _throw("tjCompressFromYUVPlanes(): Invalid argument");
  958. if(subsamp!=TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
  959. _throw("tjCompressFromYUVPlanes(): Invalid argument");
  960. if(setjmp(this->jerr.setjmp_buffer))
  961. {
  962. /* If we get here, the JPEG code has signaled an error. */
  963. retval=-1;
  964. goto bailout;
  965. }
  966. cinfo->image_width=width;
  967. cinfo->image_height=height;
  968. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  969. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  970. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  971. if(flags&TJFLAG_NOREALLOC)
  972. {
  973. alloc=0; *jpegSize=tjBufSize(width, height, subsamp);
  974. }
  975. jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
  976. if(setCompDefaults(cinfo, TJPF_RGB, subsamp, jpegQual, flags)==-1)
  977. return -1;
  978. cinfo->raw_data_in=TRUE;
  979. jpeg_start_compress(cinfo, TRUE);
  980. for(i=0; i<cinfo->num_components; i++)
  981. {
  982. jpeg_component_info *compptr=&cinfo->comp_info[i];
  983. int ih;
  984. iw[i]=compptr->width_in_blocks*DCTSIZE;
  985. ih=compptr->height_in_blocks*DCTSIZE;
  986. pw[i]=PAD(cinfo->image_width, cinfo->max_h_samp_factor)
  987. *compptr->h_samp_factor/cinfo->max_h_samp_factor;
  988. ph[i]=PAD(cinfo->image_height, cinfo->max_v_samp_factor)
  989. *compptr->v_samp_factor/cinfo->max_v_samp_factor;
  990. if(iw[i]!=pw[i] || ih!=ph[i]) usetmpbuf=1;
  991. th[i]=compptr->v_samp_factor*DCTSIZE;
  992. tmpbufsize+=iw[i]*th[i];
  993. if((inbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph[i]))==NULL)
  994. _throw("tjCompressFromYUVPlanes(): Memory allocation failure");
  995. ptr=(JSAMPLE *)srcPlanes[i];
  996. for(row=0; row<ph[i]; row++)
  997. {
  998. inbuf[i][row]=ptr;
  999. ptr+=(strides && strides[i]!=0)? strides[i]:pw[i];
  1000. }
  1001. }
  1002. if(usetmpbuf)
  1003. {
  1004. if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
  1005. _throw("tjCompressFromYUVPlanes(): Memory allocation failure");
  1006. ptr=_tmpbuf;
  1007. for(i=0; i<cinfo->num_components; i++)
  1008. {
  1009. if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
  1010. _throw("tjCompressFromYUVPlanes(): Memory allocation failure");
  1011. for(row=0; row<th[i]; row++)
  1012. {
  1013. tmpbuf[i][row]=ptr;
  1014. ptr+=iw[i];
  1015. }
  1016. }
  1017. }
  1018. for(row=0; row<(int)cinfo->image_height;
  1019. row+=cinfo->max_v_samp_factor*DCTSIZE)
  1020. {
  1021. JSAMPARRAY yuvptr[MAX_COMPONENTS];
  1022. int crow[MAX_COMPONENTS];
  1023. for(i=0; i<cinfo->num_components; i++)
  1024. {
  1025. jpeg_component_info *compptr=&cinfo->comp_info[i];
  1026. crow[i]=row*compptr->v_samp_factor/cinfo->max_v_samp_factor;
  1027. if(usetmpbuf)
  1028. {
  1029. int j, k;
  1030. for(j=0; j<min(th[i], ph[i]-crow[i]); j++)
  1031. {
  1032. memcpy(tmpbuf[i][j], inbuf[i][crow[i]+j], pw[i]);
  1033. /* Duplicate last sample in row to fill out MCU */
  1034. for(k=pw[i]; k<iw[i]; k++) tmpbuf[i][j][k]=tmpbuf[i][j][pw[i]-1];
  1035. }
  1036. /* Duplicate last row to fill out MCU */
  1037. for(j=ph[i]-crow[i]; j<th[i]; j++)
  1038. memcpy(tmpbuf[i][j], tmpbuf[i][ph[i]-crow[i]-1], iw[i]);
  1039. yuvptr[i]=tmpbuf[i];
  1040. }
  1041. else
  1042. yuvptr[i]=&inbuf[i][crow[i]];
  1043. }
  1044. jpeg_write_raw_data(cinfo, yuvptr, cinfo->max_v_samp_factor*DCTSIZE);
  1045. }
  1046. jpeg_finish_compress(cinfo);
  1047. bailout:
  1048. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  1049. for(i=0; i<MAX_COMPONENTS; i++)
  1050. {
  1051. if(tmpbuf[i]) free(tmpbuf[i]);
  1052. if(inbuf[i]) free(inbuf[i]);
  1053. }
  1054. if(_tmpbuf) free(_tmpbuf);
  1055. if(this->jerr.warning) retval=-1;
  1056. return retval;
  1057. }
  1058. DLLEXPORT int DLLCALL tjCompressFromYUV(tjhandle handle,
  1059. const unsigned char *srcBuf, int width, int pad, int height, int subsamp,
  1060. unsigned char **jpegBuf, unsigned long *jpegSize, int jpegQual, int flags)
  1061. {
  1062. const unsigned char *srcPlanes[3];
  1063. int pw0, ph0, strides[3], retval=-1;
  1064. if(srcBuf==NULL || width<=0 || pad<1 || height<=0 || subsamp<0
  1065. || subsamp>=NUMSUBOPT)
  1066. _throw("tjCompressFromYUV(): Invalid argument");
  1067. pw0=tjPlaneWidth(0, width, subsamp);
  1068. ph0=tjPlaneHeight(0, height, subsamp);
  1069. srcPlanes[0]=srcBuf;
  1070. strides[0]=PAD(pw0, pad);
  1071. if(subsamp==TJSAMP_GRAY)
  1072. {
  1073. strides[1]=strides[2]=0;
  1074. srcPlanes[1]=srcPlanes[2]=NULL;
  1075. }
  1076. else
  1077. {
  1078. int pw1=tjPlaneWidth(1, width, subsamp);
  1079. int ph1=tjPlaneHeight(1, height, subsamp);
  1080. strides[1]=strides[2]=PAD(pw1, pad);
  1081. srcPlanes[1]=srcPlanes[0]+strides[0]*ph0;
  1082. srcPlanes[2]=srcPlanes[1]+strides[1]*ph1;
  1083. }
  1084. return tjCompressFromYUVPlanes(handle, srcPlanes, width, strides, height,
  1085. subsamp, jpegBuf, jpegSize, jpegQual, flags);
  1086. bailout:
  1087. return retval;
  1088. }
  1089. /* Decompressor */
  1090. static tjhandle _tjInitDecompress(tjinstance *this)
  1091. {
  1092. static unsigned char buffer[1];
  1093. /* This is also straight out of example.c */
  1094. this->dinfo.err=jpeg_std_error(&this->jerr.pub);
  1095. this->jerr.pub.error_exit=my_error_exit;
  1096. this->jerr.pub.output_message=my_output_message;
  1097. this->jerr.emit_message=this->jerr.pub.emit_message;
  1098. this->jerr.pub.emit_message=my_emit_message;
  1099. if(setjmp(this->jerr.setjmp_buffer))
  1100. {
  1101. /* If we get here, the JPEG code has signaled an error. */
  1102. if(this) free(this);
  1103. return NULL;
  1104. }
  1105. jpeg_create_decompress(&this->dinfo);
  1106. /* Make an initial call so it will create the source manager */
  1107. jpeg_mem_src_tj(&this->dinfo, buffer, 1);
  1108. this->init|=DECOMPRESS;
  1109. return (tjhandle)this;
  1110. }
  1111. DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
  1112. {
  1113. tjinstance *this;
  1114. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  1115. {
  1116. snprintf(errStr, JMSG_LENGTH_MAX,
  1117. "tjInitDecompress(): Memory allocation failure");
  1118. return NULL;
  1119. }
  1120. MEMZERO(this, sizeof(tjinstance));
  1121. return _tjInitDecompress(this);
  1122. }
  1123. DLLEXPORT int DLLCALL tjDecompressHeader3(tjhandle handle,
  1124. const unsigned char *jpegBuf, unsigned long jpegSize, int *width,
  1125. int *height, int *jpegSubsamp, int *jpegColorspace)
  1126. {
  1127. int retval=0;
  1128. getdinstance(handle);
  1129. if((this->init&DECOMPRESS)==0)
  1130. _throw("tjDecompressHeader3(): Instance has not been initialized for decompression");
  1131. if(jpegBuf==NULL || jpegSize<=0 || width==NULL || height==NULL
  1132. || jpegSubsamp==NULL || jpegColorspace==NULL)
  1133. _throw("tjDecompressHeader3(): Invalid argument");
  1134. if(setjmp(this->jerr.setjmp_buffer))
  1135. {
  1136. /* If we get here, the JPEG code has signaled an error. */
  1137. return -1;
  1138. }
  1139. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1140. jpeg_read_header(dinfo, TRUE);
  1141. *width=dinfo->image_width;
  1142. *height=dinfo->image_height;
  1143. *jpegSubsamp=getSubsamp(dinfo);
  1144. switch(dinfo->jpeg_color_space)
  1145. {
  1146. case JCS_GRAYSCALE: *jpegColorspace=TJCS_GRAY; break;
  1147. case JCS_RGB: *jpegColorspace=TJCS_RGB; break;
  1148. case JCS_YCbCr: *jpegColorspace=TJCS_YCbCr; break;
  1149. case JCS_CMYK: *jpegColorspace=TJCS_CMYK; break;
  1150. case JCS_YCCK: *jpegColorspace=TJCS_YCCK; break;
  1151. default: *jpegColorspace=-1; break;
  1152. }
  1153. jpeg_abort_decompress(dinfo);
  1154. if(*jpegSubsamp<0)
  1155. _throw("tjDecompressHeader3(): Could not determine subsampling type for JPEG image");
  1156. if(*jpegColorspace<0)
  1157. _throw("tjDecompressHeader3(): Could not determine colorspace of JPEG image");
  1158. if(*width<1 || *height<1)
  1159. _throw("tjDecompressHeader3(): Invalid data returned in header");
  1160. bailout:
  1161. if(this->jerr.warning) retval=-1;
  1162. return retval;
  1163. }
  1164. DLLEXPORT int DLLCALL tjDecompressHeader2(tjhandle handle,
  1165. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height,
  1166. int *jpegSubsamp)
  1167. {
  1168. int jpegColorspace;
  1169. return tjDecompressHeader3(handle, jpegBuf, jpegSize, width, height,
  1170. jpegSubsamp, &jpegColorspace);
  1171. }
  1172. DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle handle,
  1173. unsigned char *jpegBuf, unsigned long jpegSize, int *width, int *height)
  1174. {
  1175. int jpegSubsamp;
  1176. return tjDecompressHeader2(handle, jpegBuf, jpegSize, width, height,
  1177. &jpegSubsamp);
  1178. }
  1179. DLLEXPORT tjscalingfactor* DLLCALL tjGetScalingFactors(int *numscalingfactors)
  1180. {
  1181. if(numscalingfactors==NULL)
  1182. {
  1183. snprintf(errStr, JMSG_LENGTH_MAX,
  1184. "tjGetScalingFactors(): Invalid argument");
  1185. return NULL;
  1186. }
  1187. *numscalingfactors=NUMSF;
  1188. return (tjscalingfactor *)sf;
  1189. }
  1190. DLLEXPORT int DLLCALL tjDecompress2(tjhandle handle,
  1191. const unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  1192. int width, int pitch, int height, int pixelFormat, int flags)
  1193. {
  1194. int i, retval=0; JSAMPROW *row_pointer=NULL;
  1195. int jpegwidth, jpegheight, scaledw, scaledh;
  1196. #ifndef JCS_EXTENSIONS
  1197. unsigned char *rgbBuf=NULL;
  1198. unsigned char *_dstBuf=NULL; int _pitch=0;
  1199. #endif
  1200. getdinstance(handle);
  1201. if((this->init&DECOMPRESS)==0)
  1202. _throw("tjDecompress2(): Instance has not been initialized for decompression");
  1203. if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0
  1204. || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF)
  1205. _throw("tjDecompress2(): Invalid argument");
  1206. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1207. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1208. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  1209. if(setjmp(this->jerr.setjmp_buffer))
  1210. {
  1211. /* If we get here, the JPEG code has signaled an error. */
  1212. retval=-1;
  1213. goto bailout;
  1214. }
  1215. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1216. jpeg_read_header(dinfo, TRUE);
  1217. if(setDecompDefaults(dinfo, pixelFormat, flags)==-1)
  1218. {
  1219. retval=-1; goto bailout;
  1220. }
  1221. if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
  1222. jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
  1223. if(width==0) width=jpegwidth;
  1224. if(height==0) height=jpegheight;
  1225. for(i=0; i<NUMSF; i++)
  1226. {
  1227. scaledw=TJSCALED(jpegwidth, sf[i]);
  1228. scaledh=TJSCALED(jpegheight, sf[i]);
  1229. if(scaledw<=width && scaledh<=height)
  1230. break;
  1231. }
  1232. if(i>=NUMSF)
  1233. _throw("tjDecompress2(): Could not scale down to desired image dimensions");
  1234. width=scaledw; height=scaledh;
  1235. dinfo->scale_num=sf[i].num;
  1236. dinfo->scale_denom=sf[i].denom;
  1237. jpeg_start_decompress(dinfo);
  1238. if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
  1239. #ifndef JCS_EXTENSIONS
  1240. if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK &&
  1241. (RGB_RED!=tjRedOffset[pixelFormat] ||
  1242. RGB_GREEN!=tjGreenOffset[pixelFormat] ||
  1243. RGB_BLUE!=tjBlueOffset[pixelFormat] ||
  1244. RGB_PIXELSIZE!=tjPixelSize[pixelFormat]))
  1245. {
  1246. rgbBuf=(unsigned char *)malloc(width*height*3);
  1247. if(!rgbBuf) _throw("tjDecompress2(): Memory allocation failure");
  1248. _pitch=pitch; pitch=width*3;
  1249. _dstBuf=dstBuf; dstBuf=rgbBuf;
  1250. }
  1251. #endif
  1252. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)
  1253. *dinfo->output_height))==NULL)
  1254. _throw("tjDecompress2(): Memory allocation failure");
  1255. for(i=0; i<(int)dinfo->output_height; i++)
  1256. {
  1257. if(flags&TJFLAG_BOTTOMUP)
  1258. row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch];
  1259. else row_pointer[i]=&dstBuf[i*pitch];
  1260. }
  1261. while(dinfo->output_scanline<dinfo->output_height)
  1262. {
  1263. jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
  1264. dinfo->output_height-dinfo->output_scanline);
  1265. }
  1266. jpeg_finish_decompress(dinfo);
  1267. #ifndef JCS_EXTENSIONS
  1268. fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat);
  1269. #endif
  1270. bailout:
  1271. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1272. #ifndef JCS_EXTENSIONS
  1273. if(rgbBuf) free(rgbBuf);
  1274. #endif
  1275. if(row_pointer) free(row_pointer);
  1276. if(this->jerr.warning) retval=-1;
  1277. return retval;
  1278. }
  1279. DLLEXPORT int DLLCALL tjDecompress(tjhandle handle, unsigned char *jpegBuf,
  1280. unsigned long jpegSize, unsigned char *dstBuf, int width, int pitch,
  1281. int height, int pixelSize, int flags)
  1282. {
  1283. if(flags&TJ_YUV)
  1284. return tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags);
  1285. else
  1286. return tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, width, pitch,
  1287. height, getPixelFormat(pixelSize, flags), flags);
  1288. }
  1289. static int setDecodeDefaults(struct jpeg_decompress_struct *dinfo,
  1290. int pixelFormat, int subsamp, int flags)
  1291. {
  1292. int i;
  1293. dinfo->scale_num=dinfo->scale_denom=1;
  1294. if(subsamp==TJSAMP_GRAY)
  1295. {
  1296. dinfo->num_components=dinfo->comps_in_scan=1;
  1297. dinfo->jpeg_color_space=JCS_GRAYSCALE;
  1298. }
  1299. else
  1300. {
  1301. dinfo->num_components=dinfo->comps_in_scan=3;
  1302. dinfo->jpeg_color_space=JCS_YCbCr;
  1303. }
  1304. dinfo->comp_info=(jpeg_component_info *)
  1305. (*dinfo->mem->alloc_small)((j_common_ptr)dinfo, JPOOL_IMAGE,
  1306. dinfo->num_components*sizeof(jpeg_component_info));
  1307. for(i=0; i<dinfo->num_components; i++)
  1308. {
  1309. jpeg_component_info *compptr=&dinfo->comp_info[i];
  1310. compptr->h_samp_factor=(i==0)? tjMCUWidth[subsamp]/8:1;
  1311. compptr->v_samp_factor=(i==0)? tjMCUHeight[subsamp]/8:1;
  1312. compptr->component_index=i;
  1313. compptr->component_id=i+1;
  1314. compptr->quant_tbl_no=compptr->dc_tbl_no=compptr->ac_tbl_no=
  1315. (i==0)? 0:1;
  1316. dinfo->cur_comp_info[i]=compptr;
  1317. }
  1318. dinfo->data_precision=8;
  1319. for(i=0; i<2; i++)
  1320. {
  1321. if(dinfo->quant_tbl_ptrs[i]==NULL)
  1322. dinfo->quant_tbl_ptrs[i]=jpeg_alloc_quant_table((j_common_ptr)dinfo);
  1323. }
  1324. return 0;
  1325. }
  1326. int my_read_markers(j_decompress_ptr dinfo)
  1327. {
  1328. return JPEG_REACHED_SOS;
  1329. }
  1330. void my_reset_marker_reader(j_decompress_ptr dinfo)
  1331. {
  1332. }
  1333. DLLEXPORT int DLLCALL tjDecodeYUVPlanes(tjhandle handle,
  1334. const unsigned char **srcPlanes, const int *strides, int subsamp,
  1335. unsigned char *dstBuf, int width, int pitch, int height, int pixelFormat,
  1336. int flags)
  1337. {
  1338. int i, retval=0; JSAMPROW *row_pointer=NULL;
  1339. JSAMPLE *_tmpbuf[MAX_COMPONENTS];
  1340. JSAMPROW *tmpbuf[MAX_COMPONENTS], *inbuf[MAX_COMPONENTS];
  1341. int row, pw0, ph0, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS];
  1342. JSAMPLE *ptr;
  1343. jpeg_component_info *compptr;
  1344. #ifndef JCS_EXTENSIONS
  1345. unsigned char *rgbBuf=NULL;
  1346. unsigned char *_dstBuf=NULL; int _pitch=0;
  1347. #endif
  1348. int (*old_read_markers)(j_decompress_ptr);
  1349. void (*old_reset_marker_reader)(j_decompress_ptr);
  1350. getdinstance(handle);
  1351. for(i=0; i<MAX_COMPONENTS; i++)
  1352. {
  1353. tmpbuf[i]=NULL; _tmpbuf[i]=NULL; inbuf[i]=NULL;
  1354. }
  1355. if((this->init&DECOMPRESS)==0)
  1356. _throw("tjDecodeYUVPlanes(): Instance has not been initialized for decompression");
  1357. if(!srcPlanes || !srcPlanes[0] || subsamp<0 || subsamp>=NUMSUBOPT
  1358. || dstBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
  1359. || pixelFormat>=TJ_NUMPF)
  1360. _throw("tjDecodeYUVPlanes(): Invalid argument");
  1361. if(subsamp!=TJSAMP_GRAY && (!srcPlanes[1] || !srcPlanes[2]))
  1362. _throw("tjDecodeYUVPlanes(): Invalid argument");
  1363. if(setjmp(this->jerr.setjmp_buffer))
  1364. {
  1365. /* If we get here, the JPEG code has signaled an error. */
  1366. retval=-1;
  1367. goto bailout;
  1368. }
  1369. if(pixelFormat==TJPF_CMYK)
  1370. _throw("tjDecodeYUVPlanes(): Cannot decode YUV images into CMYK pixels.");
  1371. if(pitch==0) pitch=width*tjPixelSize[pixelFormat];
  1372. dinfo->image_width=width;
  1373. dinfo->image_height=height;
  1374. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1375. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1376. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  1377. if(setDecodeDefaults(dinfo, pixelFormat, subsamp, flags)==-1)
  1378. {
  1379. retval=-1; goto bailout;
  1380. }
  1381. old_read_markers=dinfo->marker->read_markers;
  1382. dinfo->marker->read_markers=my_read_markers;
  1383. old_reset_marker_reader=dinfo->marker->reset_marker_reader;
  1384. dinfo->marker->reset_marker_reader=my_reset_marker_reader;
  1385. jpeg_read_header(dinfo, TRUE);
  1386. dinfo->marker->read_markers=old_read_markers;
  1387. dinfo->marker->reset_marker_reader=old_reset_marker_reader;
  1388. if(setDecompDefaults(dinfo, pixelFormat, flags)==-1)
  1389. {
  1390. retval=-1; goto bailout;
  1391. }
  1392. dinfo->do_fancy_upsampling=FALSE;
  1393. dinfo->Se=DCTSIZE2-1;
  1394. jinit_master_decompress(dinfo);
  1395. (*dinfo->upsample->start_pass)(dinfo);
  1396. pw0=PAD(width, dinfo->max_h_samp_factor);
  1397. ph0=PAD(height, dinfo->max_v_samp_factor);
  1398. if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
  1399. #ifndef JCS_EXTENSIONS
  1400. if(pixelFormat!=TJPF_GRAY && pixelFormat!=TJPF_CMYK &&
  1401. (RGB_RED!=tjRedOffset[pixelFormat] ||
  1402. RGB_GREEN!=tjGreenOffset[pixelFormat] ||
  1403. RGB_BLUE!=tjBlueOffset[pixelFormat] ||
  1404. RGB_PIXELSIZE!=tjPixelSize[pixelFormat]))
  1405. {
  1406. rgbBuf=(unsigned char *)malloc(width*height*3);
  1407. if(!rgbBuf) _throw("tjDecodeYUVPlanes(): Memory allocation failure");
  1408. _pitch=pitch; pitch=width*3;
  1409. _dstBuf=dstBuf; dstBuf=rgbBuf;
  1410. }
  1411. #endif
  1412. if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph0))==NULL)
  1413. _throw("tjDecodeYUVPlanes(): Memory allocation failure");
  1414. for(i=0; i<height; i++)
  1415. {
  1416. if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&dstBuf[(height-i-1)*pitch];
  1417. else row_pointer[i]=&dstBuf[i*pitch];
  1418. }
  1419. if(height<ph0)
  1420. for(i=height; i<ph0; i++) row_pointer[i]=row_pointer[height-1];
  1421. for(i=0; i<dinfo->num_components; i++)
  1422. {
  1423. compptr=&dinfo->comp_info[i];
  1424. _tmpbuf[i]=(JSAMPLE *)malloc(PAD(compptr->width_in_blocks*DCTSIZE, 16)
  1425. * compptr->v_samp_factor + 16);
  1426. if(!_tmpbuf[i]) _throw("tjDecodeYUVPlanes(): Memory allocation failure");
  1427. tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*compptr->v_samp_factor);
  1428. if(!tmpbuf[i]) _throw("tjDecodeYUVPlanes(): Memory allocation failure");
  1429. for(row=0; row<compptr->v_samp_factor; row++)
  1430. {
  1431. unsigned char *_tmpbuf_aligned=
  1432. (unsigned char *)PAD((size_t)_tmpbuf[i], 16);
  1433. tmpbuf[i][row]=&_tmpbuf_aligned[
  1434. PAD(compptr->width_in_blocks*DCTSIZE, 16) * row];
  1435. }
  1436. pw[i]=pw0*compptr->h_samp_factor/dinfo->max_h_samp_factor;
  1437. ph[i]=ph0*compptr->v_samp_factor/dinfo->max_v_samp_factor;
  1438. inbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph[i]);
  1439. if(!inbuf[i]) _throw("tjDecodeYUVPlanes(): Memory allocation failure");
  1440. ptr=(JSAMPLE *)srcPlanes[i];
  1441. for(row=0; row<ph[i]; row++)
  1442. {
  1443. inbuf[i][row]=ptr;
  1444. ptr+=(strides && strides[i]!=0)? strides[i]:pw[i];
  1445. }
  1446. }
  1447. for(row=0; row<ph0; row+=dinfo->max_v_samp_factor)
  1448. {
  1449. JDIMENSION inrow=0, outrow=0;
  1450. for(i=0, compptr=dinfo->comp_info; i<dinfo->num_components; i++, compptr++)
  1451. jcopy_sample_rows(inbuf[i],
  1452. row*compptr->v_samp_factor/dinfo->max_v_samp_factor, tmpbuf[i], 0,
  1453. compptr->v_samp_factor, pw[i]);
  1454. (dinfo->upsample->upsample)(dinfo, tmpbuf, &inrow,
  1455. dinfo->max_v_samp_factor, &row_pointer[row], &outrow,
  1456. dinfo->max_v_samp_factor);
  1457. }
  1458. jpeg_abort_decompress(dinfo);
  1459. #ifndef JCS_EXTENSIONS
  1460. fromRGB(rgbBuf, _dstBuf, width, _pitch, height, pixelFormat);
  1461. #endif
  1462. bailout:
  1463. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1464. #ifndef JCS_EXTENSIONS
  1465. if(rgbBuf) free(rgbBuf);
  1466. #endif
  1467. if(row_pointer) free(row_pointer);
  1468. for(i=0; i<MAX_COMPONENTS; i++)
  1469. {
  1470. if(tmpbuf[i]!=NULL) free(tmpbuf[i]);
  1471. if(_tmpbuf[i]!=NULL) free(_tmpbuf[i]);
  1472. if(inbuf[i]!=NULL) free(inbuf[i]);
  1473. }
  1474. if(this->jerr.warning) retval=-1;
  1475. return retval;
  1476. }
  1477. DLLEXPORT int DLLCALL tjDecodeYUV(tjhandle handle, const unsigned char *srcBuf,
  1478. int pad, int subsamp, unsigned char *dstBuf, int width, int pitch,
  1479. int height, int pixelFormat, int flags)
  1480. {
  1481. const unsigned char *srcPlanes[3];
  1482. int pw0, ph0, strides[3], retval=-1;
  1483. if(srcBuf==NULL || pad<0 || !isPow2(pad) || subsamp<0 || subsamp>=NUMSUBOPT
  1484. || width<=0 || height<=0)
  1485. _throw("tjDecodeYUV(): Invalid argument");
  1486. pw0=tjPlaneWidth(0, width, subsamp);
  1487. ph0=tjPlaneHeight(0, height, subsamp);
  1488. srcPlanes[0]=srcBuf;
  1489. strides[0]=PAD(pw0, pad);
  1490. if(subsamp==TJSAMP_GRAY)
  1491. {
  1492. strides[1]=strides[2]=0;
  1493. srcPlanes[1]=srcPlanes[2]=NULL;
  1494. }
  1495. else
  1496. {
  1497. int pw1=tjPlaneWidth(1, width, subsamp);
  1498. int ph1=tjPlaneHeight(1, height, subsamp);
  1499. strides[1]=strides[2]=PAD(pw1, pad);
  1500. srcPlanes[1]=srcPlanes[0]+strides[0]*ph0;
  1501. srcPlanes[2]=srcPlanes[1]+strides[1]*ph1;
  1502. }
  1503. return tjDecodeYUVPlanes(handle, srcPlanes, strides, subsamp, dstBuf, width,
  1504. pitch, height, pixelFormat, flags);
  1505. bailout:
  1506. return retval;
  1507. }
  1508. DLLEXPORT int DLLCALL tjDecompressToYUVPlanes(tjhandle handle,
  1509. const unsigned char *jpegBuf, unsigned long jpegSize,
  1510. unsigned char **dstPlanes, int width, int *strides, int height, int flags)
  1511. {
  1512. int i, sfi, row, retval=0; JSAMPROW *outbuf[MAX_COMPONENTS];
  1513. int jpegwidth, jpegheight, jpegSubsamp, scaledw, scaledh;
  1514. int pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], iw[MAX_COMPONENTS],
  1515. tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
  1516. JSAMPLE *_tmpbuf=NULL, *ptr; JSAMPROW *tmpbuf[MAX_COMPONENTS];
  1517. int dctsize;
  1518. getdinstance(handle);
  1519. for(i=0; i<MAX_COMPONENTS; i++)
  1520. {
  1521. tmpbuf[i]=NULL; outbuf[i]=NULL;
  1522. }
  1523. if((this->init&DECOMPRESS)==0)
  1524. _throw("tjDecompressToYUVPlanes(): Instance has not been initialized for decompression");
  1525. if(jpegBuf==NULL || jpegSize<=0 || !dstPlanes || !dstPlanes[0] || width<0
  1526. || height<0)
  1527. _throw("tjDecompressToYUVPlanes(): Invalid argument");
  1528. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1529. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1530. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  1531. if(setjmp(this->jerr.setjmp_buffer))
  1532. {
  1533. /* If we get here, the JPEG code has signaled an error. */
  1534. retval=-1;
  1535. goto bailout;
  1536. }
  1537. if(!this->headerRead)
  1538. {
  1539. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1540. jpeg_read_header(dinfo, TRUE);
  1541. }
  1542. this->headerRead=0;
  1543. jpegSubsamp=getSubsamp(dinfo);
  1544. if(jpegSubsamp<0)
  1545. _throw("tjDecompressToYUVPlanes(): Could not determine subsampling type for JPEG image");
  1546. if(jpegSubsamp!=TJSAMP_GRAY && (!dstPlanes[1] || !dstPlanes[2]))
  1547. _throw("tjDecompressToYUVPlanes(): Invalid argument");
  1548. jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
  1549. if(width==0) width=jpegwidth;
  1550. if(height==0) height=jpegheight;
  1551. for(i=0; i<NUMSF; i++)
  1552. {
  1553. scaledw=TJSCALED(jpegwidth, sf[i]);
  1554. scaledh=TJSCALED(jpegheight, sf[i]);
  1555. if(scaledw<=width && scaledh<=height)
  1556. break;
  1557. }
  1558. if(i>=NUMSF)
  1559. _throw("tjDecompressToYUVPlanes(): Could not scale down to desired image dimensions");
  1560. if(dinfo->num_components>3)
  1561. _throw("tjDecompressToYUVPlanes(): JPEG image must have 3 or fewer components");
  1562. width=scaledw; height=scaledh;
  1563. dinfo->scale_num=sf[i].num;
  1564. dinfo->scale_denom=sf[i].denom;
  1565. sfi=i;
  1566. jpeg_calc_output_dimensions(dinfo);
  1567. dctsize=DCTSIZE*sf[sfi].num/sf[sfi].denom;
  1568. for(i=0; i<dinfo->num_components; i++)
  1569. {
  1570. jpeg_component_info *compptr=&dinfo->comp_info[i];
  1571. int ih;
  1572. iw[i]=compptr->width_in_blocks*dctsize;
  1573. ih=compptr->height_in_blocks*dctsize;
  1574. pw[i]=PAD(dinfo->output_width, dinfo->max_h_samp_factor)
  1575. *compptr->h_samp_factor/dinfo->max_h_samp_factor;
  1576. ph[i]=PAD(dinfo->output_height, dinfo->max_v_samp_factor)
  1577. *compptr->v_samp_factor/dinfo->max_v_samp_factor;
  1578. if(iw[i]!=pw[i] || ih!=ph[i]) usetmpbuf=1;
  1579. th[i]=compptr->v_samp_factor*dctsize;
  1580. tmpbufsize+=iw[i]*th[i];
  1581. if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ph[i]))==NULL)
  1582. _throw("tjDecompressToYUVPlanes(): Memory allocation failure");
  1583. ptr=dstPlanes[i];
  1584. for(row=0; row<ph[i]; row++)
  1585. {
  1586. outbuf[i][row]=ptr;
  1587. ptr+=(strides && strides[i]!=0)? strides[i]:pw[i];
  1588. }
  1589. }
  1590. if(usetmpbuf)
  1591. {
  1592. if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
  1593. _throw("tjDecompressToYUVPlanes(): Memory allocation failure");
  1594. ptr=_tmpbuf;
  1595. for(i=0; i<dinfo->num_components; i++)
  1596. {
  1597. if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
  1598. _throw("tjDecompressToYUVPlanes(): Memory allocation failure");
  1599. for(row=0; row<th[i]; row++)
  1600. {
  1601. tmpbuf[i][row]=ptr;
  1602. ptr+=iw[i];
  1603. }
  1604. }
  1605. }
  1606. if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;
  1607. if(flags&TJFLAG_FASTDCT) dinfo->dct_method=JDCT_FASTEST;
  1608. dinfo->raw_data_out=TRUE;
  1609. jpeg_start_decompress(dinfo);
  1610. for(row=0; row<(int)dinfo->output_height;
  1611. row+=dinfo->max_v_samp_factor*dinfo->_min_DCT_scaled_size)
  1612. {
  1613. JSAMPARRAY yuvptr[MAX_COMPONENTS];
  1614. int crow[MAX_COMPONENTS];
  1615. for(i=0; i<dinfo->num_components; i++)
  1616. {
  1617. jpeg_component_info *compptr=&dinfo->comp_info[i];
  1618. if(jpegSubsamp==TJ_420)
  1619. {
  1620. /* When 4:2:0 subsampling is used with IDCT scaling, libjpeg will try
  1621. to be clever and use the IDCT to perform upsampling on the U and V
  1622. planes. For instance, if the output image is to be scaled by 1/2
  1623. relative to the JPEG image, then the scaling factor and upsampling
  1624. effectively cancel each other, so a normal 8x8 IDCT can be used.
  1625. However, this is not desirable when using the decompress-to-YUV
  1626. functionality in TurboJPEG, since we want to output the U and V
  1627. planes in their subsampled form. Thus, we have to override some
  1628. internal libjpeg parameters to force it to use the "scaled" IDCT
  1629. functions on the U and V planes. */
  1630. compptr->_DCT_scaled_size=dctsize;
  1631. compptr->MCU_sample_width=tjMCUWidth[jpegSubsamp]*
  1632. sf[sfi].num/sf[sfi].denom*
  1633. compptr->v_samp_factor/dinfo->max_v_samp_factor;
  1634. dinfo->idct->inverse_DCT[i] = dinfo->idct->inverse_DCT[0];
  1635. }
  1636. crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor;
  1637. if(usetmpbuf) yuvptr[i]=tmpbuf[i];
  1638. else yuvptr[i]=&outbuf[i][crow[i]];
  1639. }
  1640. jpeg_read_raw_data(dinfo, yuvptr,
  1641. dinfo->max_v_samp_factor*dinfo->_min_DCT_scaled_size);
  1642. if(usetmpbuf)
  1643. {
  1644. int j;
  1645. for(i=0; i<dinfo->num_components; i++)
  1646. {
  1647. for(j=0; j<min(th[i], ph[i]-crow[i]); j++)
  1648. {
  1649. memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], pw[i]);
  1650. }
  1651. }
  1652. }
  1653. }
  1654. jpeg_finish_decompress(dinfo);
  1655. bailout:
  1656. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1657. for(i=0; i<MAX_COMPONENTS; i++)
  1658. {
  1659. if(tmpbuf[i]) free(tmpbuf[i]);
  1660. if(outbuf[i]) free(outbuf[i]);
  1661. }
  1662. if(_tmpbuf) free(_tmpbuf);
  1663. if(this->jerr.warning) retval=-1;
  1664. return retval;
  1665. }
  1666. DLLEXPORT int DLLCALL tjDecompressToYUV2(tjhandle handle,
  1667. const unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  1668. int width, int pad, int height, int flags)
  1669. {
  1670. unsigned char *dstPlanes[3];
  1671. int pw0, ph0, strides[3], retval=-1, jpegSubsamp=-1;
  1672. int i, jpegwidth, jpegheight, scaledw, scaledh;
  1673. getdinstance(handle);
  1674. if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pad<1
  1675. || !isPow2(pad) || height<0)
  1676. _throw("tjDecompressToYUV2(): Invalid argument");
  1677. if(setjmp(this->jerr.setjmp_buffer))
  1678. {
  1679. /* If we get here, the JPEG code has signaled an error. */
  1680. return -1;
  1681. }
  1682. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1683. jpeg_read_header(dinfo, TRUE);
  1684. jpegSubsamp=getSubsamp(dinfo);
  1685. if(jpegSubsamp<0)
  1686. _throw("tjDecompressToYUV2(): Could not determine subsampling type for JPEG image");
  1687. jpegwidth=dinfo->image_width; jpegheight=dinfo->image_height;
  1688. if(width==0) width=jpegwidth;
  1689. if(height==0) height=jpegheight;
  1690. for(i=0; i<NUMSF; i++)
  1691. {
  1692. scaledw=TJSCALED(jpegwidth, sf[i]);
  1693. scaledh=TJSCALED(jpegheight, sf[i]);
  1694. if(scaledw<=width && scaledh<=height)
  1695. break;
  1696. }
  1697. if(i>=NUMSF)
  1698. _throw("tjDecompressToYUV2(): Could not scale down to desired image dimensions");
  1699. pw0=tjPlaneWidth(0, width, jpegSubsamp);
  1700. ph0=tjPlaneHeight(0, height, jpegSubsamp);
  1701. dstPlanes[0]=dstBuf;
  1702. strides[0]=PAD(pw0, pad);
  1703. if(jpegSubsamp==TJSAMP_GRAY)
  1704. {
  1705. strides[1]=strides[2]=0;
  1706. dstPlanes[1]=dstPlanes[2]=NULL;
  1707. }
  1708. else
  1709. {
  1710. int pw1=tjPlaneWidth(1, width, jpegSubsamp);
  1711. int ph1=tjPlaneHeight(1, height, jpegSubsamp);
  1712. strides[1]=strides[2]=PAD(pw1, pad);
  1713. dstPlanes[1]=dstPlanes[0]+strides[0]*ph0;
  1714. dstPlanes[2]=dstPlanes[1]+strides[1]*ph1;
  1715. }
  1716. this->headerRead=1;
  1717. return tjDecompressToYUVPlanes(handle, jpegBuf, jpegSize, dstPlanes, width,
  1718. strides, height, flags);
  1719. bailout:
  1720. return retval;
  1721. }
  1722. DLLEXPORT int DLLCALL tjDecompressToYUV(tjhandle handle,
  1723. unsigned char *jpegBuf, unsigned long jpegSize, unsigned char *dstBuf,
  1724. int flags)
  1725. {
  1726. return tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, 0, 4, 0, flags);
  1727. }
  1728. /* Transformer */
  1729. DLLEXPORT tjhandle DLLCALL tjInitTransform(void)
  1730. {
  1731. tjinstance *this=NULL; tjhandle handle=NULL;
  1732. if((this=(tjinstance *)malloc(sizeof(tjinstance)))==NULL)
  1733. {
  1734. snprintf(errStr, JMSG_LENGTH_MAX,
  1735. "tjInitTransform(): Memory allocation failure");
  1736. return NULL;
  1737. }
  1738. MEMZERO(this, sizeof(tjinstance));
  1739. handle=_tjInitCompress(this);
  1740. if(!handle) return NULL;
  1741. handle=_tjInitDecompress(this);
  1742. return handle;
  1743. }
  1744. DLLEXPORT int DLLCALL tjTransform(tjhandle handle,
  1745. const unsigned char *jpegBuf, unsigned long jpegSize, int n,
  1746. unsigned char **dstBufs, unsigned long *dstSizes, tjtransform *t, int flags)
  1747. {
  1748. jpeg_transform_info *xinfo=NULL;
  1749. jvirt_barray_ptr *srccoefs, *dstcoefs;
  1750. int retval=0, i, jpegSubsamp;
  1751. getinstance(handle);
  1752. if((this->init&COMPRESS)==0 || (this->init&DECOMPRESS)==0)
  1753. _throw("tjTransform(): Instance has not been initialized for transformation");
  1754. if(jpegBuf==NULL || jpegSize<=0 || n<1 || dstBufs==NULL || dstSizes==NULL
  1755. || t==NULL || flags<0)
  1756. _throw("tjTransform(): Invalid argument");
  1757. if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
  1758. else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
  1759. else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");
  1760. if(setjmp(this->jerr.setjmp_buffer))
  1761. {
  1762. /* If we get here, the JPEG code has signaled an error. */
  1763. retval=-1;
  1764. goto bailout;
  1765. }
  1766. jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
  1767. if((xinfo=(jpeg_transform_info *)malloc(sizeof(jpeg_transform_info)*n))
  1768. ==NULL)
  1769. _throw("tjTransform(): Memory allocation failure");
  1770. MEMZERO(xinfo, sizeof(jpeg_transform_info)*n);
  1771. for(i=0; i<n; i++)
  1772. {
  1773. xinfo[i].transform=xformtypes[t[i].op];
  1774. xinfo[i].perfect=(t[i].options&TJXOPT_PERFECT)? 1:0;
  1775. xinfo[i].trim=(t[i].options&TJXOPT_TRIM)? 1:0;
  1776. xinfo[i].force_grayscale=(t[i].options&TJXOPT_GRAY)? 1:0;
  1777. xinfo[i].crop=(t[i].options&TJXOPT_CROP)? 1:0;
  1778. if(n!=1 && t[i].op==TJXOP_HFLIP) xinfo[i].slow_hflip=1;
  1779. else xinfo[i].slow_hflip=0;
  1780. if(xinfo[i].crop)
  1781. {
  1782. xinfo[i].crop_xoffset=t[i].r.x; xinfo[i].crop_xoffset_set=JCROP_POS;
  1783. xinfo[i].crop_yoffset=t[i].r.y; xinfo[i].crop_yoffset_set=JCROP_POS;
  1784. if(t[i].r.w!=0)
  1785. {
  1786. xinfo[i].crop_width=t[i].r.w; xinfo[i].crop_width_set=JCROP_POS;
  1787. }
  1788. else xinfo[i].crop_width=JCROP_UNSET;
  1789. if(t[i].r.h!=0)
  1790. {
  1791. xinfo[i].crop_height=t[i].r.h; xinfo[i].crop_height_set=JCROP_POS;
  1792. }
  1793. else xinfo[i].crop_height=JCROP_UNSET;
  1794. }
  1795. }
  1796. jcopy_markers_setup(dinfo, JCOPYOPT_ALL);
  1797. jpeg_read_header(dinfo, TRUE);
  1798. jpegSubsamp=getSubsamp(dinfo);
  1799. if(jpegSubsamp<0)
  1800. _throw("tjTransform(): Could not determine subsampling type for JPEG image");
  1801. for(i=0; i<n; i++)
  1802. {
  1803. if(!jtransform_request_workspace(dinfo, &xinfo[i]))
  1804. _throw("tjTransform(): Transform is not perfect");
  1805. if(xinfo[i].crop)
  1806. {
  1807. if((t[i].r.x%xinfo[i].iMCU_sample_width)!=0
  1808. || (t[i].r.y%xinfo[i].iMCU_sample_height)!=0)
  1809. {
  1810. snprintf(errStr, JMSG_LENGTH_MAX,
  1811. "To crop this JPEG image, x must be a multiple of %d\n"
  1812. "and y must be a multiple of %d.\n",
  1813. xinfo[i].iMCU_sample_width, xinfo[i].iMCU_sample_height);
  1814. retval=-1; goto bailout;
  1815. }
  1816. }
  1817. }
  1818. srccoefs=jpeg_read_coefficients(dinfo);
  1819. for(i=0; i<n; i++)
  1820. {
  1821. int w, h, alloc=1;
  1822. if(!xinfo[i].crop)
  1823. {
  1824. w=dinfo->image_width; h=dinfo->image_height;
  1825. }
  1826. else
  1827. {
  1828. w=xinfo[i].crop_width; h=xinfo[i].crop_height;
  1829. }
  1830. if(flags&TJFLAG_NOREALLOC)
  1831. {
  1832. alloc=0; dstSizes[i]=tjBufSize(w, h, jpegSubsamp);
  1833. }
  1834. if(!(t[i].options&TJXOPT_NOOUTPUT))
  1835. jpeg_mem_dest_tj(cinfo, &dstBufs[i], &dstSizes[i], alloc);
  1836. jpeg_copy_critical_parameters(dinfo, cinfo);
  1837. dstcoefs=jtransform_adjust_parameters(dinfo, cinfo, srccoefs,
  1838. &xinfo[i]);
  1839. if(!(t[i].options&TJXOPT_NOOUTPUT))
  1840. {
  1841. jpeg_write_coefficients(cinfo, dstcoefs);
  1842. jcopy_markers_execute(dinfo, cinfo, JCOPYOPT_ALL);
  1843. }
  1844. else jinit_c_master_control(cinfo, TRUE);
  1845. jtransform_execute_transformation(dinfo, cinfo, srccoefs,
  1846. &xinfo[i]);
  1847. if(t[i].customFilter)
  1848. {
  1849. int ci, y; JDIMENSION by;
  1850. for(ci=0; ci<cinfo->num_components; ci++)
  1851. {
  1852. jpeg_component_info *compptr=&cinfo->comp_info[ci];
  1853. tjregion arrayRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1854. DCTSIZE};
  1855. tjregion planeRegion={0, 0, compptr->width_in_blocks*DCTSIZE,
  1856. compptr->height_in_blocks*DCTSIZE};
  1857. for(by=0; by<compptr->height_in_blocks; by+=compptr->v_samp_factor)
  1858. {
  1859. JBLOCKARRAY barray=(dinfo->mem->access_virt_barray)
  1860. ((j_common_ptr)dinfo, dstcoefs[ci], by, compptr->v_samp_factor,
  1861. TRUE);
  1862. for(y=0; y<compptr->v_samp_factor; y++)
  1863. {
  1864. if(t[i].customFilter(barray[y][0], arrayRegion, planeRegion,
  1865. ci, i, &t[i])==-1)
  1866. _throw("tjTransform(): Error in custom filter");
  1867. arrayRegion.y+=DCTSIZE;
  1868. }
  1869. }
  1870. }
  1871. }
  1872. if(!(t[i].options&TJXOPT_NOOUTPUT)) jpeg_finish_compress(cinfo);
  1873. }
  1874. jpeg_finish_decompress(dinfo);
  1875. bailout:
  1876. if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
  1877. if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
  1878. if(xinfo) free(xinfo);
  1879. if(this->jerr.warning) retval=-1;
  1880. return retval;
  1881. }