Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jcapimin.c
Views: 11784
/*1* jcapimin.c2*3* Copyright (C) 1994-1998, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains application interface code for the compression half8* of the JPEG library. These are the "minimum" API routines that may be9* needed in either the normal full-compression case or the transcoding-only10* case.11*12* Most of the routines intended to be called directly by an application13* are in this file or in jcapistd.c. But also see jcparam.c for14* parameter-setup helper routines, jcomapi.c for routines shared by15* compression and decompression, and jctrans.c for the transcoding case.16*/1718#define JPEG_INTERNALS19#include "jinclude.h"20#include "jpeglib.h"212223/*24* Initialization of a JPEG compression object.25* The error manager must already be set up (in case memory manager fails).26*/2728GLOBAL(void)29jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)30{31int i;3233/* Guard against version mismatches between library and caller. */34cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */35if (version != JPEG_LIB_VERSION)36ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);37if (structsize != SIZEOF(struct jpeg_compress_struct))38ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,39(int) SIZEOF(struct jpeg_compress_struct), (int) structsize);4041/* For debugging purposes, we zero the whole master structure.42* But the application has already set the err pointer, and may have set43* client_data, so we have to save and restore those fields.44* Note: if application hasn't set client_data, tools like Purify may45* complain here.46*/47{48struct jpeg_error_mgr * err = cinfo->err;49void * client_data = cinfo->client_data; /* ignore Purify complaint here */50MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));51cinfo->err = err;52cinfo->client_data = client_data;53}54cinfo->is_decompressor = FALSE;5556/* Initialize a memory manager instance for this object */57jinit_memory_mgr((j_common_ptr) cinfo);5859/* Zero out pointers to permanent structures. */60cinfo->progress = NULL;61cinfo->dest = NULL;6263cinfo->comp_info = NULL;6465for (i = 0; i < NUM_QUANT_TBLS; i++)66cinfo->quant_tbl_ptrs[i] = NULL;6768for (i = 0; i < NUM_HUFF_TBLS; i++) {69cinfo->dc_huff_tbl_ptrs[i] = NULL;70cinfo->ac_huff_tbl_ptrs[i] = NULL;71}7273cinfo->script_space = NULL;7475cinfo->input_gamma = 1.0; /* in case application forgets */7677/* OK, I'm ready */78cinfo->global_state = CSTATE_START;79}808182/*83* Destruction of a JPEG compression object84*/8586GLOBAL(void)87jpeg_destroy_compress (j_compress_ptr cinfo)88{89jpeg_destroy((j_common_ptr) cinfo); /* use common routine */90}919293/*94* Abort processing of a JPEG compression operation,95* but don't destroy the object itself.96*/9798GLOBAL(void)99jpeg_abort_compress (j_compress_ptr cinfo)100{101jpeg_abort((j_common_ptr) cinfo); /* use common routine */102}103104105/*106* Forcibly suppress or un-suppress all quantization and Huffman tables.107* Marks all currently defined tables as already written (if suppress)108* or not written (if !suppress). This will control whether they get emitted109* by a subsequent jpeg_start_compress call.110*111* This routine is exported for use by applications that want to produce112* abbreviated JPEG datastreams. It logically belongs in jcparam.c, but113* since it is called by jpeg_start_compress, we put it here --- otherwise114* jcparam.o would be linked whether the application used it or not.115*/116117GLOBAL(void)118jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)119{120int i;121JQUANT_TBL * qtbl;122JHUFF_TBL * htbl;123124for (i = 0; i < NUM_QUANT_TBLS; i++) {125if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)126qtbl->sent_table = suppress;127}128129for (i = 0; i < NUM_HUFF_TBLS; i++) {130if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)131htbl->sent_table = suppress;132if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)133htbl->sent_table = suppress;134}135}136137138/*139* Finish JPEG compression.140*141* If a multipass operating mode was selected, this may do a great deal of142* work including most of the actual output.143*/144145GLOBAL(void)146jpeg_finish_compress (j_compress_ptr cinfo)147{148JDIMENSION iMCU_row;149150if (cinfo->global_state == CSTATE_SCANNING ||151cinfo->global_state == CSTATE_RAW_OK) {152/* Terminate first pass */153if (cinfo->next_scanline < cinfo->image_height)154ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);155(*cinfo->master->finish_pass) (cinfo);156} else if (cinfo->global_state != CSTATE_WRCOEFS)157ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);158/* Perform any remaining passes */159while (! cinfo->master->is_last_pass) {160(*cinfo->master->prepare_for_pass) (cinfo);161for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {162if (cinfo->progress != NULL) {163cinfo->progress->pass_counter = (long) iMCU_row;164cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;165(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);166}167/* We bypass the main controller and invoke coef controller directly;168* all work is being done from the coefficient buffer.169*/170if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))171ERREXIT(cinfo, JERR_CANT_SUSPEND);172}173(*cinfo->master->finish_pass) (cinfo);174}175/* Write EOI, do final cleanup */176(*cinfo->marker->write_file_trailer) (cinfo);177(*cinfo->dest->term_destination) (cinfo);178/* We can use jpeg_abort to release memory and reset global_state */179jpeg_abort((j_common_ptr) cinfo);180}181182183/*184* Write a special marker.185* This is only recommended for writing COM or APPn markers.186* Must be called after jpeg_start_compress() and before187* first call to jpeg_write_scanlines() or jpeg_write_raw_data().188*/189190GLOBAL(void)191jpeg_write_marker (j_compress_ptr cinfo, int marker,192const JOCTET *dataptr, unsigned int datalen)193{194JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));195196if (cinfo->next_scanline != 0 ||197(cinfo->global_state != CSTATE_SCANNING &&198cinfo->global_state != CSTATE_RAW_OK &&199cinfo->global_state != CSTATE_WRCOEFS))200ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);201202(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);203write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */204while (datalen--) {205(*write_marker_byte) (cinfo, *dataptr);206dataptr++;207}208}209210/* Same, but piecemeal. */211212GLOBAL(void)213jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)214{215if (cinfo->next_scanline != 0 ||216(cinfo->global_state != CSTATE_SCANNING &&217cinfo->global_state != CSTATE_RAW_OK &&218cinfo->global_state != CSTATE_WRCOEFS))219ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);220221(*cinfo->marker->write_marker_header) (cinfo, marker, datalen);222}223224GLOBAL(void)225jpeg_write_m_byte (j_compress_ptr cinfo, int val)226{227(*cinfo->marker->write_marker_byte) (cinfo, val);228}229230231/*232* Alternate compression function: just write an abbreviated table file.233* Before calling this, all parameters and a data destination must be set up.234*235* To produce a pair of files containing abbreviated tables and abbreviated236* image data, one would proceed as follows:237*238* initialize JPEG object239* set JPEG parameters240* set destination to table file241* jpeg_write_tables(cinfo);242* set destination to image file243* jpeg_start_compress(cinfo, FALSE);244* write data...245* jpeg_finish_compress(cinfo);246*247* jpeg_write_tables has the side effect of marking all tables written248* (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress249* will not re-emit the tables unless it is passed write_all_tables=TRUE.250*/251252GLOBAL(void)253jpeg_write_tables (j_compress_ptr cinfo)254{255if (cinfo->global_state != CSTATE_START)256ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);257258/* (Re)initialize error mgr and destination modules */259(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);260(*cinfo->dest->init_destination) (cinfo);261/* Initialize the marker writer ... bit of a crock to do it here. */262jinit_marker_writer(cinfo);263/* Write them tables! */264(*cinfo->marker->write_tables_only) (cinfo);265/* And clean up. */266(*cinfo->dest->term_destination) (cinfo);267/*268* In library releases up through v6a, we called jpeg_abort() here to free269* any working memory allocated by the destination manager and marker270* writer. Some applications had a problem with that: they allocated space271* of their own from the library memory manager, and didn't want it to go272* away during write_tables. So now we do nothing. This will cause a273* memory leak if an app calls write_tables repeatedly without doing a full274* compression cycle or otherwise resetting the JPEG object. However, that275* seems less bad than unexpectedly freeing memory in the normal case.276* An app that prefers the old behavior can call jpeg_abort for itself after277* each call to jpeg_write_tables().278*/279}280281282