vrshoot
diff libs/libjpeg/jcapimin.c @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jcapimin.c Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,280 @@ 1.4 +/* 1.5 + * jcapimin.c 1.6 + * 1.7 + * Copyright (C) 1994-1998, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains application interface code for the compression half 1.12 + * of the JPEG library. These are the "minimum" API routines that may be 1.13 + * needed in either the normal full-compression case or the transcoding-only 1.14 + * case. 1.15 + * 1.16 + * Most of the routines intended to be called directly by an application 1.17 + * are in this file or in jcapistd.c. But also see jcparam.c for 1.18 + * parameter-setup helper routines, jcomapi.c for routines shared by 1.19 + * compression and decompression, and jctrans.c for the transcoding case. 1.20 + */ 1.21 + 1.22 +#define JPEG_INTERNALS 1.23 +#include "jinclude.h" 1.24 +#include "jpeglib.h" 1.25 + 1.26 + 1.27 +/* 1.28 + * Initialization of a JPEG compression object. 1.29 + * The error manager must already be set up (in case memory manager fails). 1.30 + */ 1.31 + 1.32 +GLOBAL(void) 1.33 +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize) 1.34 +{ 1.35 + int i; 1.36 + 1.37 + /* Guard against version mismatches between library and caller. */ 1.38 + cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ 1.39 + if (version != JPEG_LIB_VERSION) 1.40 + ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); 1.41 + if (structsize != SIZEOF(struct jpeg_compress_struct)) 1.42 + ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 1.43 + (int) SIZEOF(struct jpeg_compress_struct), (int) structsize); 1.44 + 1.45 + /* For debugging purposes, we zero the whole master structure. 1.46 + * But the application has already set the err pointer, and may have set 1.47 + * client_data, so we have to save and restore those fields. 1.48 + * Note: if application hasn't set client_data, tools like Purify may 1.49 + * complain here. 1.50 + */ 1.51 + { 1.52 + struct jpeg_error_mgr * err = cinfo->err; 1.53 + void * client_data = cinfo->client_data; /* ignore Purify complaint here */ 1.54 + MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct)); 1.55 + cinfo->err = err; 1.56 + cinfo->client_data = client_data; 1.57 + } 1.58 + cinfo->is_decompressor = FALSE; 1.59 + 1.60 + /* Initialize a memory manager instance for this object */ 1.61 + jinit_memory_mgr((j_common_ptr) cinfo); 1.62 + 1.63 + /* Zero out pointers to permanent structures. */ 1.64 + cinfo->progress = NULL; 1.65 + cinfo->dest = NULL; 1.66 + 1.67 + cinfo->comp_info = NULL; 1.68 + 1.69 + for (i = 0; i < NUM_QUANT_TBLS; i++) 1.70 + cinfo->quant_tbl_ptrs[i] = NULL; 1.71 + 1.72 + for (i = 0; i < NUM_HUFF_TBLS; i++) { 1.73 + cinfo->dc_huff_tbl_ptrs[i] = NULL; 1.74 + cinfo->ac_huff_tbl_ptrs[i] = NULL; 1.75 + } 1.76 + 1.77 + cinfo->script_space = NULL; 1.78 + 1.79 + cinfo->input_gamma = 1.0; /* in case application forgets */ 1.80 + 1.81 + /* OK, I'm ready */ 1.82 + cinfo->global_state = CSTATE_START; 1.83 +} 1.84 + 1.85 + 1.86 +/* 1.87 + * Destruction of a JPEG compression object 1.88 + */ 1.89 + 1.90 +GLOBAL(void) 1.91 +jpeg_destroy_compress (j_compress_ptr cinfo) 1.92 +{ 1.93 + jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ 1.94 +} 1.95 + 1.96 + 1.97 +/* 1.98 + * Abort processing of a JPEG compression operation, 1.99 + * but don't destroy the object itself. 1.100 + */ 1.101 + 1.102 +GLOBAL(void) 1.103 +jpeg_abort_compress (j_compress_ptr cinfo) 1.104 +{ 1.105 + jpeg_abort((j_common_ptr) cinfo); /* use common routine */ 1.106 +} 1.107 + 1.108 + 1.109 +/* 1.110 + * Forcibly suppress or un-suppress all quantization and Huffman tables. 1.111 + * Marks all currently defined tables as already written (if suppress) 1.112 + * or not written (if !suppress). This will control whether they get emitted 1.113 + * by a subsequent jpeg_start_compress call. 1.114 + * 1.115 + * This routine is exported for use by applications that want to produce 1.116 + * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but 1.117 + * since it is called by jpeg_start_compress, we put it here --- otherwise 1.118 + * jcparam.o would be linked whether the application used it or not. 1.119 + */ 1.120 + 1.121 +GLOBAL(void) 1.122 +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress) 1.123 +{ 1.124 + int i; 1.125 + JQUANT_TBL * qtbl; 1.126 + JHUFF_TBL * htbl; 1.127 + 1.128 + for (i = 0; i < NUM_QUANT_TBLS; i++) { 1.129 + if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL) 1.130 + qtbl->sent_table = suppress; 1.131 + } 1.132 + 1.133 + for (i = 0; i < NUM_HUFF_TBLS; i++) { 1.134 + if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL) 1.135 + htbl->sent_table = suppress; 1.136 + if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL) 1.137 + htbl->sent_table = suppress; 1.138 + } 1.139 +} 1.140 + 1.141 + 1.142 +/* 1.143 + * Finish JPEG compression. 1.144 + * 1.145 + * If a multipass operating mode was selected, this may do a great deal of 1.146 + * work including most of the actual output. 1.147 + */ 1.148 + 1.149 +GLOBAL(void) 1.150 +jpeg_finish_compress (j_compress_ptr cinfo) 1.151 +{ 1.152 + JDIMENSION iMCU_row; 1.153 + 1.154 + if (cinfo->global_state == CSTATE_SCANNING || 1.155 + cinfo->global_state == CSTATE_RAW_OK) { 1.156 + /* Terminate first pass */ 1.157 + if (cinfo->next_scanline < cinfo->image_height) 1.158 + ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); 1.159 + (*cinfo->master->finish_pass) (cinfo); 1.160 + } else if (cinfo->global_state != CSTATE_WRCOEFS) 1.161 + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 1.162 + /* Perform any remaining passes */ 1.163 + while (! cinfo->master->is_last_pass) { 1.164 + (*cinfo->master->prepare_for_pass) (cinfo); 1.165 + for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { 1.166 + if (cinfo->progress != NULL) { 1.167 + cinfo->progress->pass_counter = (long) iMCU_row; 1.168 + cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows; 1.169 + (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); 1.170 + } 1.171 + /* We bypass the main controller and invoke coef controller directly; 1.172 + * all work is being done from the coefficient buffer. 1.173 + */ 1.174 + if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL)) 1.175 + ERREXIT(cinfo, JERR_CANT_SUSPEND); 1.176 + } 1.177 + (*cinfo->master->finish_pass) (cinfo); 1.178 + } 1.179 + /* Write EOI, do final cleanup */ 1.180 + (*cinfo->marker->write_file_trailer) (cinfo); 1.181 + (*cinfo->dest->term_destination) (cinfo); 1.182 + /* We can use jpeg_abort to release memory and reset global_state */ 1.183 + jpeg_abort((j_common_ptr) cinfo); 1.184 +} 1.185 + 1.186 + 1.187 +/* 1.188 + * Write a special marker. 1.189 + * This is only recommended for writing COM or APPn markers. 1.190 + * Must be called after jpeg_start_compress() and before 1.191 + * first call to jpeg_write_scanlines() or jpeg_write_raw_data(). 1.192 + */ 1.193 + 1.194 +GLOBAL(void) 1.195 +jpeg_write_marker (j_compress_ptr cinfo, int marker, 1.196 + const JOCTET *dataptr, unsigned int datalen) 1.197 +{ 1.198 + JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val)); 1.199 + 1.200 + if (cinfo->next_scanline != 0 || 1.201 + (cinfo->global_state != CSTATE_SCANNING && 1.202 + cinfo->global_state != CSTATE_RAW_OK && 1.203 + cinfo->global_state != CSTATE_WRCOEFS)) 1.204 + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 1.205 + 1.206 + (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); 1.207 + write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */ 1.208 + while (datalen--) { 1.209 + (*write_marker_byte) (cinfo, *dataptr); 1.210 + dataptr++; 1.211 + } 1.212 +} 1.213 + 1.214 +/* Same, but piecemeal. */ 1.215 + 1.216 +GLOBAL(void) 1.217 +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen) 1.218 +{ 1.219 + if (cinfo->next_scanline != 0 || 1.220 + (cinfo->global_state != CSTATE_SCANNING && 1.221 + cinfo->global_state != CSTATE_RAW_OK && 1.222 + cinfo->global_state != CSTATE_WRCOEFS)) 1.223 + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 1.224 + 1.225 + (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); 1.226 +} 1.227 + 1.228 +GLOBAL(void) 1.229 +jpeg_write_m_byte (j_compress_ptr cinfo, int val) 1.230 +{ 1.231 + (*cinfo->marker->write_marker_byte) (cinfo, val); 1.232 +} 1.233 + 1.234 + 1.235 +/* 1.236 + * Alternate compression function: just write an abbreviated table file. 1.237 + * Before calling this, all parameters and a data destination must be set up. 1.238 + * 1.239 + * To produce a pair of files containing abbreviated tables and abbreviated 1.240 + * image data, one would proceed as follows: 1.241 + * 1.242 + * initialize JPEG object 1.243 + * set JPEG parameters 1.244 + * set destination to table file 1.245 + * jpeg_write_tables(cinfo); 1.246 + * set destination to image file 1.247 + * jpeg_start_compress(cinfo, FALSE); 1.248 + * write data... 1.249 + * jpeg_finish_compress(cinfo); 1.250 + * 1.251 + * jpeg_write_tables has the side effect of marking all tables written 1.252 + * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress 1.253 + * will not re-emit the tables unless it is passed write_all_tables=TRUE. 1.254 + */ 1.255 + 1.256 +GLOBAL(void) 1.257 +jpeg_write_tables (j_compress_ptr cinfo) 1.258 +{ 1.259 + if (cinfo->global_state != CSTATE_START) 1.260 + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 1.261 + 1.262 + /* (Re)initialize error mgr and destination modules */ 1.263 + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 1.264 + (*cinfo->dest->init_destination) (cinfo); 1.265 + /* Initialize the marker writer ... bit of a crock to do it here. */ 1.266 + jinit_marker_writer(cinfo); 1.267 + /* Write them tables! */ 1.268 + (*cinfo->marker->write_tables_only) (cinfo); 1.269 + /* And clean up. */ 1.270 + (*cinfo->dest->term_destination) (cinfo); 1.271 + /* 1.272 + * In library releases up through v6a, we called jpeg_abort() here to free 1.273 + * any working memory allocated by the destination manager and marker 1.274 + * writer. Some applications had a problem with that: they allocated space 1.275 + * of their own from the library memory manager, and didn't want it to go 1.276 + * away during write_tables. So now we do nothing. This will cause a 1.277 + * memory leak if an app calls write_tables repeatedly without doing a full 1.278 + * compression cycle or otherwise resetting the JPEG object. However, that 1.279 + * seems less bad than unexpectedly freeing memory in the normal case. 1.280 + * An app that prefers the old behavior can call jpeg_abort for itself after 1.281 + * each call to jpeg_write_tables(). 1.282 + */ 1.283 +}