Path: blob/master/thirdparty/libjpeg-turbo/src/jcsample.c
9904 views
/*1* jcsample.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1996, Thomas G. Lane.5* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB9* Copyright (C) 2014, MIPS Technologies, Inc., California.10* Copyright (C) 2015, 2019, 2022, 2024, D. R. Commander.11* For conditions of distribution and use, see the accompanying README.ijg12* file.13*14* This file contains downsampling routines.15*16* Downsampling input data is counted in "row groups". A row group17* is defined to be max_v_samp_factor pixel rows of each component,18* from which the downsampler produces v_samp_factor sample rows.19* A single row group is processed in each call to the downsampler module.20*21* The downsampler is responsible for edge-expansion of its output data22* to fill an integral number of DCT blocks horizontally. The source buffer23* may be modified if it is helpful for this purpose (the source buffer is24* allocated wide enough to correspond to the desired output width).25* The caller (the prep controller) is responsible for vertical padding.26*27* The downsampler may request "context rows" by setting need_context_rows28* during startup. In this case, the input arrays will contain at least29* one row group's worth of pixels above and below the passed-in data;30* the caller will create dummy rows at image top and bottom by replicating31* the first or last real pixel row.32*33* An excellent reference for image resampling is34* Digital Image Warping, George Wolberg, 1990.35* Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.36*37* The downsampling algorithm used here is a simple average of the source38* pixels covered by the output pixel. The hi-falutin sampling literature39* refers to this as a "box filter". In general the characteristics of a box40* filter are not very good, but for the specific cases we normally use (1:141* and 2:1 ratios) the box is equivalent to a "triangle filter" which is not42* nearly so bad. If you intend to use other sampling ratios, you'd be well43* advised to improve this code.44*45* A simple input-smoothing capability is provided. This is mainly intended46* for cleaning up color-dithered GIF input files (if you find it inadequate,47* we suggest using an external filtering program such as pnmconvol). When48* enabled, each input pixel P is replaced by a weighted sum of itself and its49* eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,50* where SF = (smoothing_factor / 1024).51* Currently, smoothing is only supported for 2h2v sampling factors.52*/5354#define JPEG_INTERNALS55#include "jinclude.h"56#include "jpeglib.h"57#include "jsimd.h"58#include "jsamplecomp.h"596061#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)6263/* Pointer to routine to downsample a single component */64typedef void (*downsample1_ptr) (j_compress_ptr cinfo,65jpeg_component_info *compptr,66_JSAMPARRAY input_data,67_JSAMPARRAY output_data);6869/* Private subobject */7071typedef struct {72struct jpeg_downsampler pub; /* public fields */7374/* Downsampling method pointers, one per component */75downsample1_ptr methods[MAX_COMPONENTS];76} my_downsampler;7778typedef my_downsampler *my_downsample_ptr;798081/*82* Initialize for a downsampling pass.83*/8485METHODDEF(void)86start_pass_downsample(j_compress_ptr cinfo)87{88/* no work for now */89}909192/*93* Expand a component horizontally from width input_cols to width output_cols,94* by duplicating the rightmost samples.95*/9697LOCAL(void)98expand_right_edge(_JSAMPARRAY image_data, int num_rows, JDIMENSION input_cols,99JDIMENSION output_cols)100{101register _JSAMPROW ptr;102register _JSAMPLE pixval;103register int count;104int row;105int numcols = (int)(output_cols - input_cols);106107if (numcols > 0) {108for (row = 0; row < num_rows; row++) {109ptr = image_data[row] + input_cols;110pixval = ptr[-1];111for (count = numcols; count > 0; count--)112*ptr++ = pixval;113}114}115}116117118/*119* Do downsampling for a whole row group (all components).120*121* In this version we simply downsample each component independently.122*/123124METHODDEF(void)125sep_downsample(j_compress_ptr cinfo, _JSAMPIMAGE input_buf,126JDIMENSION in_row_index, _JSAMPIMAGE output_buf,127JDIMENSION out_row_group_index)128{129my_downsample_ptr downsample = (my_downsample_ptr)cinfo->downsample;130int ci;131jpeg_component_info *compptr;132_JSAMPARRAY in_ptr, out_ptr;133134for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;135ci++, compptr++) {136in_ptr = input_buf[ci] + in_row_index;137out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);138(*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);139}140}141142143/*144* Downsample pixel values of a single component.145* One row group is processed per call.146* This version handles arbitrary integral sampling ratios, without smoothing.147* Note that this version is not actually used for customary sampling ratios.148*/149150METHODDEF(void)151int_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,152_JSAMPARRAY input_data, _JSAMPARRAY output_data)153{154int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;155JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */156int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;157JDIMENSION output_cols = compptr->width_in_blocks * data_unit;158_JSAMPROW inptr, outptr;159JLONG outvalue;160161h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;162v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;163numpix = h_expand * v_expand;164numpix2 = numpix / 2;165166/* Expand input data enough to let all the output samples be generated167* by the standard loop. Special-casing padded output would be more168* efficient.169*/170expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,171output_cols * h_expand);172173inrow = 0;174for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {175outptr = output_data[outrow];176for (outcol = 0, outcol_h = 0; outcol < output_cols;177outcol++, outcol_h += h_expand) {178outvalue = 0;179for (v = 0; v < v_expand; v++) {180inptr = input_data[inrow + v] + outcol_h;181for (h = 0; h < h_expand; h++) {182outvalue += (JLONG)(*inptr++);183}184}185*outptr++ = (_JSAMPLE)((outvalue + numpix2) / numpix);186}187inrow += v_expand;188}189}190191192/*193* Downsample pixel values of a single component.194* This version handles the special case of a full-size component,195* without smoothing.196*/197198METHODDEF(void)199fullsize_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,200_JSAMPARRAY input_data, _JSAMPARRAY output_data)201{202int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;203204/* Copy the data */205_jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor,206cinfo->image_width);207/* Edge-expand */208expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width,209compptr->width_in_blocks * data_unit);210}211212213/*214* Downsample pixel values of a single component.215* This version handles the common case of 2:1 horizontal and 1:1 vertical,216* without smoothing.217*218* A note about the "bias" calculations: when rounding fractional values to219* integer, we do not want to always round 0.5 up to the next integer.220* If we did that, we'd introduce a noticeable bias towards larger values.221* Instead, this code is arranged so that 0.5 will be rounded up or down at222* alternate pixel locations (a simple ordered dither pattern).223*/224225METHODDEF(void)226h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,227_JSAMPARRAY input_data, _JSAMPARRAY output_data)228{229int outrow;230JDIMENSION outcol;231int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;232JDIMENSION output_cols = compptr->width_in_blocks * data_unit;233register _JSAMPROW inptr, outptr;234register int bias;235236/* Expand input data enough to let all the output samples be generated237* by the standard loop. Special-casing padded output would be more238* efficient.239*/240expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,241output_cols * 2);242243for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {244outptr = output_data[outrow];245inptr = input_data[outrow];246bias = 0; /* bias = 0,1,0,1,... for successive samples */247for (outcol = 0; outcol < output_cols; outcol++) {248*outptr++ = (_JSAMPLE)((inptr[0] + inptr[1] + bias) >> 1);249bias ^= 1; /* 0=>1, 1=>0 */250inptr += 2;251}252}253}254255256/*257* Downsample pixel values of a single component.258* This version handles the standard case of 2:1 horizontal and 2:1 vertical,259* without smoothing.260*/261262METHODDEF(void)263h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,264_JSAMPARRAY input_data, _JSAMPARRAY output_data)265{266int inrow, outrow;267JDIMENSION outcol;268int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;269JDIMENSION output_cols = compptr->width_in_blocks * data_unit;270register _JSAMPROW inptr0, inptr1, outptr;271register int bias;272273/* Expand input data enough to let all the output samples be generated274* by the standard loop. Special-casing padded output would be more275* efficient.276*/277expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,278output_cols * 2);279280inrow = 0;281for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {282outptr = output_data[outrow];283inptr0 = input_data[inrow];284inptr1 = input_data[inrow + 1];285bias = 1; /* bias = 1,2,1,2,... for successive samples */286for (outcol = 0; outcol < output_cols; outcol++) {287*outptr++ = (_JSAMPLE)288((inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1] + bias) >> 2);289bias ^= 3; /* 1=>2, 2=>1 */290inptr0 += 2; inptr1 += 2;291}292inrow += 2;293}294}295296297#ifdef INPUT_SMOOTHING_SUPPORTED298299/*300* Downsample pixel values of a single component.301* This version handles the standard case of 2:1 horizontal and 2:1 vertical,302* with smoothing. One row of context is required.303*/304305METHODDEF(void)306h2v2_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,307_JSAMPARRAY input_data, _JSAMPARRAY output_data)308{309int inrow, outrow;310JDIMENSION colctr;311int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;312JDIMENSION output_cols = compptr->width_in_blocks * data_unit;313register _JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;314JLONG membersum, neighsum, memberscale, neighscale;315316/* Expand input data enough to let all the output samples be generated317* by the standard loop. Special-casing padded output would be more318* efficient.319*/320expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,321cinfo->image_width, output_cols * 2);322323/* We don't bother to form the individual "smoothed" input pixel values;324* we can directly compute the output which is the average of the four325* smoothed values. Each of the four member pixels contributes a fraction326* (1-8*SF) to its own smoothed image and a fraction SF to each of the three327* other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final328* output. The four corner-adjacent neighbor pixels contribute a fraction329* SF to just one smoothed pixel, or SF/4 to the final output; while the330* eight edge-adjacent neighbors contribute SF to each of two smoothed331* pixels, or SF/2 overall. In order to use integer arithmetic, these332* factors are scaled by 2^16 = 65536.333* Also recall that SF = smoothing_factor / 1024.334*/335336memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */337neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */338339inrow = 0;340for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {341outptr = output_data[outrow];342inptr0 = input_data[inrow];343inptr1 = input_data[inrow + 1];344above_ptr = input_data[inrow - 1];345below_ptr = input_data[inrow + 2];346347/* Special case for first column: pretend column -1 is same as column 0 */348membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];349neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +350inptr0[0] + inptr0[2] + inptr1[0] + inptr1[2];351neighsum += neighsum;352neighsum += above_ptr[0] + above_ptr[2] + below_ptr[0] + below_ptr[2];353membersum = membersum * memberscale + neighsum * neighscale;354*outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);355inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;356357for (colctr = output_cols - 2; colctr > 0; colctr--) {358/* sum of pixels directly mapped to this output element */359membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];360/* sum of edge-neighbor pixels */361neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +362inptr0[-1] + inptr0[2] + inptr1[-1] + inptr1[2];363/* The edge-neighbors count twice as much as corner-neighbors */364neighsum += neighsum;365/* Add in the corner-neighbors */366neighsum += above_ptr[-1] + above_ptr[2] + below_ptr[-1] + below_ptr[2];367/* form final output scaled up by 2^16 */368membersum = membersum * memberscale + neighsum * neighscale;369/* round, descale and output it */370*outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);371inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;372}373374/* Special case for last column */375membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];376neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +377inptr0[-1] + inptr0[1] + inptr1[-1] + inptr1[1];378neighsum += neighsum;379neighsum += above_ptr[-1] + above_ptr[1] + below_ptr[-1] + below_ptr[1];380membersum = membersum * memberscale + neighsum * neighscale;381*outptr = (_JSAMPLE)((membersum + 32768) >> 16);382383inrow += 2;384}385}386387388/*389* Downsample pixel values of a single component.390* This version handles the special case of a full-size component,391* with smoothing. One row of context is required.392*/393394METHODDEF(void)395fullsize_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,396_JSAMPARRAY input_data, _JSAMPARRAY output_data)397{398int outrow;399JDIMENSION colctr;400int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;401JDIMENSION output_cols = compptr->width_in_blocks * data_unit;402register _JSAMPROW inptr, above_ptr, below_ptr, outptr;403JLONG membersum, neighsum, memberscale, neighscale;404int colsum, lastcolsum, nextcolsum;405406/* Expand input data enough to let all the output samples be generated407* by the standard loop. Special-casing padded output would be more408* efficient.409*/410expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,411cinfo->image_width, output_cols);412413/* Each of the eight neighbor pixels contributes a fraction SF to the414* smoothed pixel, while the main pixel contributes (1-8*SF). In order415* to use integer arithmetic, these factors are multiplied by 2^16 = 65536.416* Also recall that SF = smoothing_factor / 1024.417*/418419memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */420neighscale = cinfo->smoothing_factor * 64; /* scaled SF */421422for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {423outptr = output_data[outrow];424inptr = input_data[outrow];425above_ptr = input_data[outrow - 1];426below_ptr = input_data[outrow + 1];427428/* Special case for first column */429colsum = (*above_ptr++) + (*below_ptr++) + inptr[0];430membersum = *inptr++;431nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];432neighsum = colsum + (colsum - membersum) + nextcolsum;433membersum = membersum * memberscale + neighsum * neighscale;434*outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);435lastcolsum = colsum; colsum = nextcolsum;436437for (colctr = output_cols - 2; colctr > 0; colctr--) {438membersum = *inptr++;439above_ptr++; below_ptr++;440nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];441neighsum = lastcolsum + (colsum - membersum) + nextcolsum;442membersum = membersum * memberscale + neighsum * neighscale;443*outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);444lastcolsum = colsum; colsum = nextcolsum;445}446447/* Special case for last column */448membersum = *inptr;449neighsum = lastcolsum + (colsum - membersum) + colsum;450membersum = membersum * memberscale + neighsum * neighscale;451*outptr = (_JSAMPLE)((membersum + 32768) >> 16);452453}454}455456#endif /* INPUT_SMOOTHING_SUPPORTED */457458459/*460* Module initialization routine for downsampling.461* Note that we must select a routine for each component.462*/463464GLOBAL(void)465_jinit_downsampler(j_compress_ptr cinfo)466{467my_downsample_ptr downsample;468int ci;469jpeg_component_info *compptr;470boolean smoothok = TRUE;471472#ifdef C_LOSSLESS_SUPPORTED473if (cinfo->master->lossless) {474#if BITS_IN_JSAMPLE == 8475if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)476#else477if (cinfo->data_precision > BITS_IN_JSAMPLE ||478cinfo->data_precision < BITS_IN_JSAMPLE - 3)479#endif480ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);481} else482#endif483{484if (cinfo->data_precision != BITS_IN_JSAMPLE)485ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);486}487488downsample = (my_downsample_ptr)489(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,490sizeof(my_downsampler));491cinfo->downsample = (struct jpeg_downsampler *)downsample;492downsample->pub.start_pass = start_pass_downsample;493downsample->pub._downsample = sep_downsample;494downsample->pub.need_context_rows = FALSE;495496if (cinfo->CCIR601_sampling)497ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);498499/* Verify we can handle the sampling factors, and set up method pointers */500for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;501ci++, compptr++) {502if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&503compptr->v_samp_factor == cinfo->max_v_samp_factor) {504#ifdef INPUT_SMOOTHING_SUPPORTED505if (cinfo->smoothing_factor) {506downsample->methods[ci] = fullsize_smooth_downsample;507downsample->pub.need_context_rows = TRUE;508} else509#endif510downsample->methods[ci] = fullsize_downsample;511} else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&512compptr->v_samp_factor == cinfo->max_v_samp_factor) {513smoothok = FALSE;514#ifdef WITH_SIMD515if (jsimd_can_h2v1_downsample())516downsample->methods[ci] = jsimd_h2v1_downsample;517else518#endif519downsample->methods[ci] = h2v1_downsample;520} else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&521compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {522#ifdef INPUT_SMOOTHING_SUPPORTED523if (cinfo->smoothing_factor) {524#if defined(WITH_SIMD) && defined(__mips__)525if (jsimd_can_h2v2_smooth_downsample())526downsample->methods[ci] = jsimd_h2v2_smooth_downsample;527else528#endif529downsample->methods[ci] = h2v2_smooth_downsample;530downsample->pub.need_context_rows = TRUE;531} else532#endif533{534#ifdef WITH_SIMD535if (jsimd_can_h2v2_downsample())536downsample->methods[ci] = jsimd_h2v2_downsample;537else538#endif539downsample->methods[ci] = h2v2_downsample;540}541} else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&542(cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {543smoothok = FALSE;544downsample->methods[ci] = int_downsample;545} else546ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);547}548549#ifdef INPUT_SMOOTHING_SUPPORTED550if (cinfo->smoothing_factor && !smoothok)551TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);552#endif553}554555#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */556557558