Path: blob/master/thirdparty/libjpeg-turbo/src/jcapimin.c
9904 views
/*1* jcapimin.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1998, Thomas G. Lane.5* Modified 2003-2010 by Guido Vollbeding.6* libjpeg-turbo Modifications:7* Copyright (C) 2022, 2024, D. R. Commander.8* For conditions of distribution and use, see the accompanying README.ijg9* file.10*11* This file contains application interface code for the compression half12* of the JPEG library. These are the "minimum" API routines that may be13* needed in either the normal full-compression case or the transcoding-only14* case.15*16* Most of the routines intended to be called directly by an application17* are in this file or in jcapistd.c. But also see jcparam.c for18* parameter-setup helper routines, jcomapi.c for routines shared by19* compression and decompression, and jctrans.c for the transcoding case.20*/2122#define JPEG_INTERNALS23#include "jinclude.h"24#include "jpeglib.h"25#include "jcmaster.h"262728/*29* Initialization of a JPEG compression object.30* The error manager must already be set up (in case memory manager fails).31*/3233GLOBAL(void)34jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)35{36int i;3738/* Guard against version mismatches between library and caller. */39cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */40if (version != JPEG_LIB_VERSION)41ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);42if (structsize != sizeof(struct jpeg_compress_struct))43ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,44(int)sizeof(struct jpeg_compress_struct), (int)structsize);4546/* For debugging purposes, we zero the whole master structure.47* But the application has already set the err pointer, and may have set48* client_data, so we have to save and restore those fields.49* Note: if application hasn't set client_data, tools like Purify may50* complain here.51*/52{53struct jpeg_error_mgr *err = cinfo->err;54void *client_data = cinfo->client_data; /* ignore Purify complaint here */55memset(cinfo, 0, sizeof(struct jpeg_compress_struct));56cinfo->err = err;57cinfo->client_data = client_data;58}59cinfo->is_decompressor = FALSE;6061/* Initialize a memory manager instance for this object */62jinit_memory_mgr((j_common_ptr)cinfo);6364/* Zero out pointers to permanent structures. */65cinfo->progress = NULL;66cinfo->dest = NULL;6768cinfo->comp_info = NULL;6970for (i = 0; i < NUM_QUANT_TBLS; i++) {71cinfo->quant_tbl_ptrs[i] = NULL;72#if JPEG_LIB_VERSION >= 7073cinfo->q_scale_factor[i] = 100;74#endif75}7677for (i = 0; i < NUM_HUFF_TBLS; i++) {78cinfo->dc_huff_tbl_ptrs[i] = NULL;79cinfo->ac_huff_tbl_ptrs[i] = NULL;80}8182#if JPEG_LIB_VERSION >= 8083/* Must do it here for emit_dqt in case jpeg_write_tables is used */84cinfo->block_size = DCTSIZE;85cinfo->natural_order = jpeg_natural_order;86cinfo->lim_Se = DCTSIZE2 - 1;87#endif8889cinfo->script_space = NULL;9091cinfo->input_gamma = 1.0; /* in case application forgets */9293cinfo->data_precision = BITS_IN_JSAMPLE;9495/* OK, I'm ready */96cinfo->global_state = CSTATE_START;9798/* The master struct is used to store extension parameters, so we allocate it99* here.100*/101cinfo->master = (struct jpeg_comp_master *)102(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,103sizeof(my_comp_master));104memset(cinfo->master, 0, sizeof(my_comp_master));105}106107108/*109* Destruction of a JPEG compression object110*/111112GLOBAL(void)113jpeg_destroy_compress(j_compress_ptr cinfo)114{115jpeg_destroy((j_common_ptr)cinfo); /* use common routine */116}117118119/*120* Abort processing of a JPEG compression operation,121* but don't destroy the object itself.122*/123124GLOBAL(void)125jpeg_abort_compress(j_compress_ptr cinfo)126{127jpeg_abort((j_common_ptr)cinfo); /* use common routine */128}129130131/*132* Forcibly suppress or un-suppress all quantization and Huffman tables.133* Marks all currently defined tables as already written (if suppress)134* or not written (if !suppress). This will control whether they get emitted135* by a subsequent jpeg_start_compress call.136*137* This routine is exported for use by applications that want to produce138* abbreviated JPEG datastreams. It logically belongs in jcparam.c, but139* since it is called by jpeg_start_compress, we put it here --- otherwise140* jcparam.o would be linked whether the application used it or not.141*/142143GLOBAL(void)144jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)145{146int i;147JQUANT_TBL *qtbl;148JHUFF_TBL *htbl;149150for (i = 0; i < NUM_QUANT_TBLS; i++) {151if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)152qtbl->sent_table = suppress;153}154155for (i = 0; i < NUM_HUFF_TBLS; i++) {156if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)157htbl->sent_table = suppress;158if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)159htbl->sent_table = suppress;160}161}162163164/*165* Finish JPEG compression.166*167* If a multipass operating mode was selected, this may do a great deal of168* work including most of the actual output.169*/170171GLOBAL(void)172jpeg_finish_compress(j_compress_ptr cinfo)173{174JDIMENSION iMCU_row;175176if (cinfo->global_state == CSTATE_SCANNING ||177cinfo->global_state == CSTATE_RAW_OK) {178/* Terminate first pass */179if (cinfo->next_scanline < cinfo->image_height)180ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);181(*cinfo->master->finish_pass) (cinfo);182} else if (cinfo->global_state != CSTATE_WRCOEFS)183ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);184/* Perform any remaining passes */185while (!cinfo->master->is_last_pass) {186(*cinfo->master->prepare_for_pass) (cinfo);187for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {188if (cinfo->progress != NULL) {189cinfo->progress->pass_counter = (long)iMCU_row;190cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows;191(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);192}193/* We bypass the main controller and invoke coef controller directly;194* all work is being done from the coefficient buffer.195*/196if (cinfo->data_precision <= 8) {197if (!(*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE)NULL))198ERREXIT(cinfo, JERR_CANT_SUSPEND);199} else if (cinfo->data_precision <= 12) {200if (!(*cinfo->coef->compress_data_12) (cinfo, (J12SAMPIMAGE)NULL))201ERREXIT(cinfo, JERR_CANT_SUSPEND);202} else {203#ifdef C_LOSSLESS_SUPPORTED204if (!(*cinfo->coef->compress_data_16) (cinfo, (J16SAMPIMAGE)NULL))205ERREXIT(cinfo, JERR_CANT_SUSPEND);206#else207ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);208#endif209}210}211(*cinfo->master->finish_pass) (cinfo);212}213/* Write EOI, do final cleanup */214(*cinfo->marker->write_file_trailer) (cinfo);215(*cinfo->dest->term_destination) (cinfo);216/* We can use jpeg_abort to release memory and reset global_state */217jpeg_abort((j_common_ptr)cinfo);218}219220221/*222* Write a special marker.223* This is only recommended for writing COM or APPn markers.224* Must be called after jpeg_start_compress() and before225* first call to jpeg_write_scanlines() or jpeg_write_raw_data().226*/227228GLOBAL(void)229jpeg_write_marker(j_compress_ptr cinfo, int marker, const JOCTET *dataptr,230unsigned int datalen)231{232void (*write_marker_byte) (j_compress_ptr info, int val);233234if (cinfo->next_scanline != 0 ||235(cinfo->global_state != CSTATE_SCANNING &&236cinfo->global_state != CSTATE_RAW_OK &&237cinfo->global_state != CSTATE_WRCOEFS))238ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);239240(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);241write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */242while (datalen--) {243(*write_marker_byte) (cinfo, *dataptr);244dataptr++;245}246}247248/* Same, but piecemeal. */249250GLOBAL(void)251jpeg_write_m_header(j_compress_ptr cinfo, int marker, unsigned int datalen)252{253if (cinfo->next_scanline != 0 ||254(cinfo->global_state != CSTATE_SCANNING &&255cinfo->global_state != CSTATE_RAW_OK &&256cinfo->global_state != CSTATE_WRCOEFS))257ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);258259(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);260}261262GLOBAL(void)263jpeg_write_m_byte(j_compress_ptr cinfo, int val)264{265(*cinfo->marker->write_marker_byte) (cinfo, val);266}267268269/*270* Alternate compression function: just write an abbreviated table file.271* Before calling this, all parameters and a data destination must be set up.272*273* To produce a pair of files containing abbreviated tables and abbreviated274* image data, one would proceed as follows:275*276* initialize JPEG object277* set JPEG parameters278* set destination to table file279* jpeg_write_tables(cinfo);280* set destination to image file281* jpeg_start_compress(cinfo, FALSE);282* write data...283* jpeg_finish_compress(cinfo);284*285* jpeg_write_tables has the side effect of marking all tables written286* (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress287* will not re-emit the tables unless it is passed write_all_tables=TRUE.288*/289290GLOBAL(void)291jpeg_write_tables(j_compress_ptr cinfo)292{293if (cinfo->global_state != CSTATE_START)294ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);295296/* (Re)initialize error mgr and destination modules */297(*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);298(*cinfo->dest->init_destination) (cinfo);299/* Initialize the marker writer ... bit of a crock to do it here. */300jinit_marker_writer(cinfo);301/* Write them tables! */302(*cinfo->marker->write_tables_only) (cinfo);303/* And clean up. */304(*cinfo->dest->term_destination) (cinfo);305/*306* In library releases up through v6a, we called jpeg_abort() here to free307* any working memory allocated by the destination manager and marker308* writer. Some applications had a problem with that: they allocated space309* of their own from the library memory manager, and didn't want it to go310* away during write_tables. So now we do nothing. This will cause a311* memory leak if an app calls write_tables repeatedly without doing a full312* compression cycle or otherwise resetting the JPEG object. However, that313* seems less bad than unexpectedly freeing memory in the normal case.314* An app that prefers the old behavior can call jpeg_abort for itself after315* each call to jpeg_write_tables().316*/317}318319320