jpg_dec.c
Upload User: wgos123
Upload Date: 2013-09-16
Package Size: 16709k
Code Size: 13k
Category:

GDI-Bitmap

Development Platform:

Visual C++

  1. /*
  2.  * Copyright (c) 2001-2003 Michael David Adams.
  3.  * All rights reserved.
  4.  */
  5. /* __START_OF_JASPER_LICENSE__
  6.  * 
  7.  * JasPer Software License
  8.  * 
  9.  * IMAGE POWER JPEG-2000 PUBLIC LICENSE
  10.  * ************************************
  11.  * 
  12.  * GRANT:
  13.  * 
  14.  * Permission is hereby granted, free of charge, to any person (the "User")
  15.  * obtaining a copy of this software and associated documentation, to deal
  16.  * in the JasPer Software without restriction, including without limitation
  17.  * the right to use, copy, modify, merge, publish, distribute, sublicense,
  18.  * and/or sell copies of the JasPer Software (in source and binary forms),
  19.  * and to permit persons to whom the JasPer Software is furnished to do so,
  20.  * provided further that the License Conditions below are met.
  21.  * 
  22.  * License Conditions
  23.  * ******************
  24.  * 
  25.  * A.  Redistributions of source code must retain the above copyright notice,
  26.  * and this list of conditions, and the following disclaimer.
  27.  * 
  28.  * B.  Redistributions in binary form must reproduce the above copyright
  29.  * notice, and this list of conditions, and the following disclaimer in
  30.  * the documentation and/or other materials provided with the distribution.
  31.  * 
  32.  * C.  Neither the name of Image Power, Inc. nor any other contributor
  33.  * (including, but not limited to, the University of British Columbia and
  34.  * Michael David Adams) may be used to endorse or promote products derived
  35.  * from this software without specific prior written permission.
  36.  * 
  37.  * D.  User agrees that it shall not commence any action against Image Power,
  38.  * Inc., the University of British Columbia, Michael David Adams, or any
  39.  * other contributors (collectively "Licensors") for infringement of any
  40.  * intellectual property rights ("IPR") held by the User in respect of any
  41.  * technology that User owns or has a right to license or sublicense and
  42.  * which is an element required in order to claim compliance with ISO/IEC
  43.  * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
  44.  * rights worldwide arising under statutory or common law, and whether
  45.  * or not perfected, including, without limitation, all (i) patents and
  46.  * patent applications owned or licensable by User; (ii) rights associated
  47.  * with works of authorship including copyrights, copyright applications,
  48.  * copyright registrations, mask work rights, mask work applications,
  49.  * mask work registrations; (iii) rights relating to the protection of
  50.  * trade secrets and confidential information; (iv) any right analogous
  51.  * to those set forth in subsections (i), (ii), or (iii) and any other
  52.  * proprietary rights relating to intangible property (other than trademark,
  53.  * trade dress, or service mark rights); and (v) divisions, continuations,
  54.  * renewals, reissues and extensions of the foregoing (as and to the extent
  55.  * applicable) now existing, hereafter filed, issued or acquired.
  56.  * 
  57.  * E.  If User commences an infringement action against any Licensor(s) then
  58.  * such Licensor(s) shall have the right to terminate User's license and
  59.  * all sublicenses that have been granted hereunder by User to other parties.
  60.  * 
  61.  * F.  This software is for use only in hardware or software products that
  62.  * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
  63.  * or right to this Software is granted for products that do not comply
  64.  * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
  65.  * from the ISO.
  66.  * 
  67.  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
  68.  * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  69.  * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
  70.  * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
  71.  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
  72.  * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
  73.  * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
  74.  * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
  75.  * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
  76.  * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
  77.  * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
  78.  * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
  79.  * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
  80.  * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
  81.  * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
  82.  * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
  83.  * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
  84.  * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
  85.  * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
  86.  * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
  87.  * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
  88.  * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
  89.  * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
  90.  * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
  91.  * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
  92.  * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
  93.  * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
  94.  * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
  95.  * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
  96.  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
  97.  * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  98.  * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
  99.  * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
  100.  * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
  101.  * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
  102.  * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
  103.  * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
  104.  * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
  105.  * NOTICE SPECIFIED IN THIS SECTION.
  106.  * 
  107.  * __END_OF_JASPER_LICENSE__
  108.  */
  109. /******************************************************************************
  110. * Includes.
  111. ******************************************************************************/
  112. #include <stdio.h>
  113. #include <assert.h>
  114. #include <ctype.h>
  115. #include "jasper/jas_tvp.h"
  116. #include "jasper/jas_stream.h"
  117. #include "jasper/jas_image.h"
  118. #include "jasper/jas_string.h"
  119. #include "jpg_jpeglib.h"
  120. #include "jpg_cod.h"
  121. /******************************************************************************
  122. * Types.
  123. ******************************************************************************/
  124. /* JPEG decoder data sink type. */
  125. typedef struct jpg_dest_s {
  126. /* Initialize output. */
  127. void (*start_output)(j_decompress_ptr cinfo, struct jpg_dest_s *dinfo);
  128. /* Output rows of decompressed data. */
  129. void (*put_pixel_rows)(j_decompress_ptr cinfo, struct jpg_dest_s *dinfo,
  130.   JDIMENSION rows_supplied);
  131. /* Cleanup output. */
  132. void (*finish_output)(j_decompress_ptr cinfo, struct jpg_dest_s *dinfo);
  133. /* Output buffer. */
  134. JSAMPARRAY buffer;
  135. /* Height of output buffer. */
  136. JDIMENSION buffer_height;
  137. /* The current row. */
  138. JDIMENSION row;
  139. /* The image used to hold the decompressed sample data. */
  140. jas_image_t *image;
  141. /* The row buffer. */
  142. jas_matrix_t *data;
  143. /* The error indicator.  If this is nonzero, something has gone wrong
  144.   during decompression. */
  145. int error;
  146. } jpg_dest_t;
  147. /******************************************************************************
  148. * Local functions.
  149. ******************************************************************************/
  150. static void jpg_start_output(j_decompress_ptr cinfo, jpg_dest_t *dinfo);
  151. static void jpg_put_pixel_rows(j_decompress_ptr cinfo, jpg_dest_t *dinfo,
  152.   JDIMENSION rows_supplied);
  153. static void jpg_finish_output(j_decompress_ptr cinfo, jpg_dest_t *dinfo);
  154. static int jpg_copystreamtofile(FILE *out, jas_stream_t *in);
  155. static jas_image_t *jpg_mkimage(j_decompress_ptr cinfo);
  156. /******************************************************************************
  157. * Code for load operation.
  158. ******************************************************************************/
  159. /* Load an image from a stream in the JPG format. */
  160. jas_image_t *jpg_decode(jas_stream_t *in, char *optstr)
  161. {
  162. struct jpeg_decompress_struct cinfo;
  163. struct jpeg_error_mgr jerr;
  164. FILE *input_file;
  165. jpg_dest_t dest_mgr_buf;
  166. jpg_dest_t *dest_mgr = &dest_mgr_buf;
  167. int num_scanlines;
  168. jas_image_t *image;
  169. /* Avoid compiler warnings about unused parameters. */
  170. optstr = 0;
  171. image = 0;
  172. input_file = 0;
  173. if (!(input_file = tmpfile())) {
  174. goto error;
  175. }
  176. if (jpg_copystreamtofile(input_file, in)) {
  177. goto error;
  178. }
  179. rewind(input_file);
  180. /* Allocate and initialize a JPEG decompression object. */
  181. cinfo.err = jpeg_std_error(&jerr);
  182. jpeg_create_decompress(&cinfo);
  183. /* Specify the data source for decompression. */
  184. jpeg_stdio_src(&cinfo, input_file);
  185. /* Read the file header to obtain the image information. */
  186. jpeg_read_header(&cinfo, TRUE);
  187. /* Start the decompressor. */
  188. jpeg_start_decompress(&cinfo);
  189. /* Create an image object to hold the decoded data. */
  190. if (!(image = jpg_mkimage(&cinfo))) {
  191. goto error;
  192. }
  193. /* Initialize the data sink object. */
  194. dest_mgr->image = image;
  195. dest_mgr->data = jas_matrix_create(1, cinfo.output_width);
  196. dest_mgr->start_output = jpg_start_output;
  197. dest_mgr->put_pixel_rows = jpg_put_pixel_rows;
  198. dest_mgr->finish_output = jpg_finish_output;
  199.     dest_mgr->buffer = (*cinfo.mem->alloc_sarray)
  200.       ((j_common_ptr) &cinfo, JPOOL_IMAGE,
  201.        cinfo.output_width * cinfo.output_components, (JDIMENSION) 1);
  202. dest_mgr->buffer_height = 1;
  203. dest_mgr->error = 0;
  204. /* Process the compressed data. */
  205. (*dest_mgr->start_output)(&cinfo, dest_mgr);
  206. while (cinfo.output_scanline < cinfo.output_height) {
  207. num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
  208.   dest_mgr->buffer_height);
  209. (*dest_mgr->put_pixel_rows)(&cinfo, dest_mgr, num_scanlines);
  210. }
  211. (*dest_mgr->finish_output)(&cinfo, dest_mgr);
  212. /* Complete the decompression process. */
  213. jpeg_finish_decompress(&cinfo);
  214. /* Destroy the JPEG decompression object. */
  215. jpeg_destroy_decompress(&cinfo);
  216. jas_matrix_destroy(dest_mgr->data);
  217. fclose(input_file);
  218. if (dest_mgr->error) {
  219. goto error;
  220. }
  221. return image;
  222. error:
  223. if (image) {
  224. jas_image_destroy(image);
  225. }
  226. if (input_file) {
  227. fclose(input_file);
  228. }
  229. return 0;
  230. }
  231. /******************************************************************************
  232. *
  233. ******************************************************************************/
  234. static jas_image_t *jpg_mkimage(j_decompress_ptr cinfo)
  235. {
  236. jas_image_t *image;
  237. int cmptno;
  238. jas_image_cmptparm_t cmptparm;
  239. int numcmpts;
  240. image = 0;
  241. numcmpts = cinfo->output_components;
  242. if (!(image = jas_image_create0())) {
  243. goto error;
  244. }
  245. for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
  246. cmptparm.tlx = 0;
  247. cmptparm.tly = 0;
  248. cmptparm.hstep = 1;
  249. cmptparm.vstep = 1;
  250. cmptparm.width = cinfo->image_width;
  251. cmptparm.height = cinfo->image_height;
  252. cmptparm.prec = 8;
  253. cmptparm.sgnd = false;
  254. if (jas_image_addcmpt(image, cmptno, &cmptparm)) {
  255. goto error;
  256. }
  257. }
  258. if (numcmpts == 3) {
  259. jas_image_setclrspc(image, JAS_CLRSPC_SRGB);
  260. jas_image_setcmpttype(image, 0,
  261.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
  262. jas_image_setcmpttype(image, 1,
  263.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
  264. jas_image_setcmpttype(image, 2,
  265.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
  266. } else {
  267. jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
  268. jas_image_setcmpttype(image, 0,
  269.   JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
  270. }
  271. return image;
  272. error:
  273. if (image) {
  274. jas_image_destroy(image);
  275. }
  276. return 0;
  277. }
  278. /******************************************************************************
  279. * Data source code.
  280. ******************************************************************************/
  281. static int jpg_copystreamtofile(FILE *out, jas_stream_t *in)
  282. {
  283. int c;
  284. while ((c = jas_stream_getc(in)) != EOF) {
  285. if (fputc(c, out) == EOF) {
  286. return -1;
  287. }
  288. }
  289. if (jas_stream_error(in)) {
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. /******************************************************************************
  295. * Data sink code.
  296. ******************************************************************************/
  297. static void jpg_start_output(j_decompress_ptr cinfo, jpg_dest_t *dinfo)
  298. {
  299. /* Avoid compiler warnings about unused parameters. */
  300. cinfo = 0;
  301. dinfo->row = 0;
  302. }
  303. static void jpg_put_pixel_rows(j_decompress_ptr cinfo, jpg_dest_t *dinfo,
  304.   JDIMENSION rows_supplied)
  305. {
  306. JSAMPLE *bufptr;
  307. int cmptno;
  308. JDIMENSION x;
  309. uint_fast32_t width;
  310. if (dinfo->error) {
  311. return;
  312. }
  313. assert(cinfo->output_components == jas_image_numcmpts(dinfo->image));
  314. for (cmptno = 0; cmptno < cinfo->output_components; ++cmptno) {
  315. width = jas_image_cmptwidth(dinfo->image, cmptno);
  316. bufptr = (dinfo->buffer[0]) + cmptno;
  317. for (x = 0; x < width; ++x) {
  318. jas_matrix_set(dinfo->data, 0, x, GETJSAMPLE(*bufptr));
  319. bufptr += cinfo->output_components;
  320. }
  321. if (jas_image_writecmpt(dinfo->image, cmptno, 0, dinfo->row, width, 1,
  322.   dinfo->data)) {
  323. dinfo->error = 1;
  324. }
  325. }
  326. dinfo->row += rows_supplied;
  327. }
  328. static void jpg_finish_output(j_decompress_ptr cinfo, jpg_dest_t *dinfo)
  329. {
  330. /* Avoid compiler warnings about unused parameters. */
  331. cinfo = 0;
  332. dinfo = 0;
  333. }