3dphotoshoot

annotate libs/libjpeg/jcapimin.c @ 23:a94af102872f

minor mistake
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 13 Jun 2015 05:35:43 +0300
parents
children
rev   line source
nuclear@14 1 /*
nuclear@14 2 * jcapimin.c
nuclear@14 3 *
nuclear@14 4 * Copyright (C) 1994-1998, Thomas G. Lane.
nuclear@14 5 * This file is part of the Independent JPEG Group's software.
nuclear@14 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@14 7 *
nuclear@14 8 * This file contains application interface code for the compression half
nuclear@14 9 * of the JPEG library. These are the "minimum" API routines that may be
nuclear@14 10 * needed in either the normal full-compression case or the transcoding-only
nuclear@14 11 * case.
nuclear@14 12 *
nuclear@14 13 * Most of the routines intended to be called directly by an application
nuclear@14 14 * are in this file or in jcapistd.c. But also see jcparam.c for
nuclear@14 15 * parameter-setup helper routines, jcomapi.c for routines shared by
nuclear@14 16 * compression and decompression, and jctrans.c for the transcoding case.
nuclear@14 17 */
nuclear@14 18
nuclear@14 19 #define JPEG_INTERNALS
nuclear@14 20 #include "jinclude.h"
nuclear@14 21 #include "jpeglib.h"
nuclear@14 22
nuclear@14 23
nuclear@14 24 /*
nuclear@14 25 * Initialization of a JPEG compression object.
nuclear@14 26 * The error manager must already be set up (in case memory manager fails).
nuclear@14 27 */
nuclear@14 28
nuclear@14 29 GLOBAL(void)
nuclear@14 30 jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
nuclear@14 31 {
nuclear@14 32 int i;
nuclear@14 33
nuclear@14 34 /* Guard against version mismatches between library and caller. */
nuclear@14 35 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
nuclear@14 36 if (version != JPEG_LIB_VERSION)
nuclear@14 37 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
nuclear@14 38 if (structsize != SIZEOF(struct jpeg_compress_struct))
nuclear@14 39 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
nuclear@14 40 (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
nuclear@14 41
nuclear@14 42 /* For debugging purposes, we zero the whole master structure.
nuclear@14 43 * But the application has already set the err pointer, and may have set
nuclear@14 44 * client_data, so we have to save and restore those fields.
nuclear@14 45 * Note: if application hasn't set client_data, tools like Purify may
nuclear@14 46 * complain here.
nuclear@14 47 */
nuclear@14 48 {
nuclear@14 49 struct jpeg_error_mgr * err = cinfo->err;
nuclear@14 50 void * client_data = cinfo->client_data; /* ignore Purify complaint here */
nuclear@14 51 MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
nuclear@14 52 cinfo->err = err;
nuclear@14 53 cinfo->client_data = client_data;
nuclear@14 54 }
nuclear@14 55 cinfo->is_decompressor = FALSE;
nuclear@14 56
nuclear@14 57 /* Initialize a memory manager instance for this object */
nuclear@14 58 jinit_memory_mgr((j_common_ptr) cinfo);
nuclear@14 59
nuclear@14 60 /* Zero out pointers to permanent structures. */
nuclear@14 61 cinfo->progress = NULL;
nuclear@14 62 cinfo->dest = NULL;
nuclear@14 63
nuclear@14 64 cinfo->comp_info = NULL;
nuclear@14 65
nuclear@14 66 for (i = 0; i < NUM_QUANT_TBLS; i++)
nuclear@14 67 cinfo->quant_tbl_ptrs[i] = NULL;
nuclear@14 68
nuclear@14 69 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@14 70 cinfo->dc_huff_tbl_ptrs[i] = NULL;
nuclear@14 71 cinfo->ac_huff_tbl_ptrs[i] = NULL;
nuclear@14 72 }
nuclear@14 73
nuclear@14 74 cinfo->script_space = NULL;
nuclear@14 75
nuclear@14 76 cinfo->input_gamma = 1.0; /* in case application forgets */
nuclear@14 77
nuclear@14 78 /* OK, I'm ready */
nuclear@14 79 cinfo->global_state = CSTATE_START;
nuclear@14 80 }
nuclear@14 81
nuclear@14 82
nuclear@14 83 /*
nuclear@14 84 * Destruction of a JPEG compression object
nuclear@14 85 */
nuclear@14 86
nuclear@14 87 GLOBAL(void)
nuclear@14 88 jpeg_destroy_compress (j_compress_ptr cinfo)
nuclear@14 89 {
nuclear@14 90 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
nuclear@14 91 }
nuclear@14 92
nuclear@14 93
nuclear@14 94 /*
nuclear@14 95 * Abort processing of a JPEG compression operation,
nuclear@14 96 * but don't destroy the object itself.
nuclear@14 97 */
nuclear@14 98
nuclear@14 99 GLOBAL(void)
nuclear@14 100 jpeg_abort_compress (j_compress_ptr cinfo)
nuclear@14 101 {
nuclear@14 102 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
nuclear@14 103 }
nuclear@14 104
nuclear@14 105
nuclear@14 106 /*
nuclear@14 107 * Forcibly suppress or un-suppress all quantization and Huffman tables.
nuclear@14 108 * Marks all currently defined tables as already written (if suppress)
nuclear@14 109 * or not written (if !suppress). This will control whether they get emitted
nuclear@14 110 * by a subsequent jpeg_start_compress call.
nuclear@14 111 *
nuclear@14 112 * This routine is exported for use by applications that want to produce
nuclear@14 113 * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
nuclear@14 114 * since it is called by jpeg_start_compress, we put it here --- otherwise
nuclear@14 115 * jcparam.o would be linked whether the application used it or not.
nuclear@14 116 */
nuclear@14 117
nuclear@14 118 GLOBAL(void)
nuclear@14 119 jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
nuclear@14 120 {
nuclear@14 121 int i;
nuclear@14 122 JQUANT_TBL * qtbl;
nuclear@14 123 JHUFF_TBL * htbl;
nuclear@14 124
nuclear@14 125 for (i = 0; i < NUM_QUANT_TBLS; i++) {
nuclear@14 126 if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
nuclear@14 127 qtbl->sent_table = suppress;
nuclear@14 128 }
nuclear@14 129
nuclear@14 130 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@14 131 if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
nuclear@14 132 htbl->sent_table = suppress;
nuclear@14 133 if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
nuclear@14 134 htbl->sent_table = suppress;
nuclear@14 135 }
nuclear@14 136 }
nuclear@14 137
nuclear@14 138
nuclear@14 139 /*
nuclear@14 140 * Finish JPEG compression.
nuclear@14 141 *
nuclear@14 142 * If a multipass operating mode was selected, this may do a great deal of
nuclear@14 143 * work including most of the actual output.
nuclear@14 144 */
nuclear@14 145
nuclear@14 146 GLOBAL(void)
nuclear@14 147 jpeg_finish_compress (j_compress_ptr cinfo)
nuclear@14 148 {
nuclear@14 149 JDIMENSION iMCU_row;
nuclear@14 150
nuclear@14 151 if (cinfo->global_state == CSTATE_SCANNING ||
nuclear@14 152 cinfo->global_state == CSTATE_RAW_OK) {
nuclear@14 153 /* Terminate first pass */
nuclear@14 154 if (cinfo->next_scanline < cinfo->image_height)
nuclear@14 155 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
nuclear@14 156 (*cinfo->master->finish_pass) (cinfo);
nuclear@14 157 } else if (cinfo->global_state != CSTATE_WRCOEFS)
nuclear@14 158 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@14 159 /* Perform any remaining passes */
nuclear@14 160 while (! cinfo->master->is_last_pass) {
nuclear@14 161 (*cinfo->master->prepare_for_pass) (cinfo);
nuclear@14 162 for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
nuclear@14 163 if (cinfo->progress != NULL) {
nuclear@14 164 cinfo->progress->pass_counter = (long) iMCU_row;
nuclear@14 165 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
nuclear@14 166 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
nuclear@14 167 }
nuclear@14 168 /* We bypass the main controller and invoke coef controller directly;
nuclear@14 169 * all work is being done from the coefficient buffer.
nuclear@14 170 */
nuclear@14 171 if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
nuclear@14 172 ERREXIT(cinfo, JERR_CANT_SUSPEND);
nuclear@14 173 }
nuclear@14 174 (*cinfo->master->finish_pass) (cinfo);
nuclear@14 175 }
nuclear@14 176 /* Write EOI, do final cleanup */
nuclear@14 177 (*cinfo->marker->write_file_trailer) (cinfo);
nuclear@14 178 (*cinfo->dest->term_destination) (cinfo);
nuclear@14 179 /* We can use jpeg_abort to release memory and reset global_state */
nuclear@14 180 jpeg_abort((j_common_ptr) cinfo);
nuclear@14 181 }
nuclear@14 182
nuclear@14 183
nuclear@14 184 /*
nuclear@14 185 * Write a special marker.
nuclear@14 186 * This is only recommended for writing COM or APPn markers.
nuclear@14 187 * Must be called after jpeg_start_compress() and before
nuclear@14 188 * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
nuclear@14 189 */
nuclear@14 190
nuclear@14 191 GLOBAL(void)
nuclear@14 192 jpeg_write_marker (j_compress_ptr cinfo, int marker,
nuclear@14 193 const JOCTET *dataptr, unsigned int datalen)
nuclear@14 194 {
nuclear@14 195 JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
nuclear@14 196
nuclear@14 197 if (cinfo->next_scanline != 0 ||
nuclear@14 198 (cinfo->global_state != CSTATE_SCANNING &&
nuclear@14 199 cinfo->global_state != CSTATE_RAW_OK &&
nuclear@14 200 cinfo->global_state != CSTATE_WRCOEFS))
nuclear@14 201 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@14 202
nuclear@14 203 (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
nuclear@14 204 write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
nuclear@14 205 while (datalen--) {
nuclear@14 206 (*write_marker_byte) (cinfo, *dataptr);
nuclear@14 207 dataptr++;
nuclear@14 208 }
nuclear@14 209 }
nuclear@14 210
nuclear@14 211 /* Same, but piecemeal. */
nuclear@14 212
nuclear@14 213 GLOBAL(void)
nuclear@14 214 jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
nuclear@14 215 {
nuclear@14 216 if (cinfo->next_scanline != 0 ||
nuclear@14 217 (cinfo->global_state != CSTATE_SCANNING &&
nuclear@14 218 cinfo->global_state != CSTATE_RAW_OK &&
nuclear@14 219 cinfo->global_state != CSTATE_WRCOEFS))
nuclear@14 220 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@14 221
nuclear@14 222 (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
nuclear@14 223 }
nuclear@14 224
nuclear@14 225 GLOBAL(void)
nuclear@14 226 jpeg_write_m_byte (j_compress_ptr cinfo, int val)
nuclear@14 227 {
nuclear@14 228 (*cinfo->marker->write_marker_byte) (cinfo, val);
nuclear@14 229 }
nuclear@14 230
nuclear@14 231
nuclear@14 232 /*
nuclear@14 233 * Alternate compression function: just write an abbreviated table file.
nuclear@14 234 * Before calling this, all parameters and a data destination must be set up.
nuclear@14 235 *
nuclear@14 236 * To produce a pair of files containing abbreviated tables and abbreviated
nuclear@14 237 * image data, one would proceed as follows:
nuclear@14 238 *
nuclear@14 239 * initialize JPEG object
nuclear@14 240 * set JPEG parameters
nuclear@14 241 * set destination to table file
nuclear@14 242 * jpeg_write_tables(cinfo);
nuclear@14 243 * set destination to image file
nuclear@14 244 * jpeg_start_compress(cinfo, FALSE);
nuclear@14 245 * write data...
nuclear@14 246 * jpeg_finish_compress(cinfo);
nuclear@14 247 *
nuclear@14 248 * jpeg_write_tables has the side effect of marking all tables written
nuclear@14 249 * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
nuclear@14 250 * will not re-emit the tables unless it is passed write_all_tables=TRUE.
nuclear@14 251 */
nuclear@14 252
nuclear@14 253 GLOBAL(void)
nuclear@14 254 jpeg_write_tables (j_compress_ptr cinfo)
nuclear@14 255 {
nuclear@14 256 if (cinfo->global_state != CSTATE_START)
nuclear@14 257 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@14 258
nuclear@14 259 /* (Re)initialize error mgr and destination modules */
nuclear@14 260 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
nuclear@14 261 (*cinfo->dest->init_destination) (cinfo);
nuclear@14 262 /* Initialize the marker writer ... bit of a crock to do it here. */
nuclear@14 263 jinit_marker_writer(cinfo);
nuclear@14 264 /* Write them tables! */
nuclear@14 265 (*cinfo->marker->write_tables_only) (cinfo);
nuclear@14 266 /* And clean up. */
nuclear@14 267 (*cinfo->dest->term_destination) (cinfo);
nuclear@14 268 /*
nuclear@14 269 * In library releases up through v6a, we called jpeg_abort() here to free
nuclear@14 270 * any working memory allocated by the destination manager and marker
nuclear@14 271 * writer. Some applications had a problem with that: they allocated space
nuclear@14 272 * of their own from the library memory manager, and didn't want it to go
nuclear@14 273 * away during write_tables. So now we do nothing. This will cause a
nuclear@14 274 * memory leak if an app calls write_tables repeatedly without doing a full
nuclear@14 275 * compression cycle or otherwise resetting the JPEG object. However, that
nuclear@14 276 * seems less bad than unexpectedly freeing memory in the normal case.
nuclear@14 277 * An app that prefers the old behavior can call jpeg_abort for itself after
nuclear@14 278 * each call to jpeg_write_tables().
nuclear@14 279 */
nuclear@14 280 }