Path: blob/master/3rdparty/libjpeg-turbo/src/jdmerge.c
16337 views
/*1* jdmerge.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1994-1996, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB7* Copyright (C) 2009, 2011, 2014-2015, D. R. Commander.8* Copyright (C) 2013, Linaro Limited.9* For conditions of distribution and use, see the accompanying README.ijg10* file.11*12* This file contains code for merged upsampling/color conversion.13*14* This file combines functions from jdsample.c and jdcolor.c;15* read those files first to understand what's going on.16*17* When the chroma components are to be upsampled by simple replication18* (ie, box filtering), we can save some work in color conversion by19* calculating all the output pixels corresponding to a pair of chroma20* samples at one time. In the conversion equations21* R = Y + K1 * Cr22* G = Y + K2 * Cb + K3 * Cr23* B = Y + K4 * Cb24* only the Y term varies among the group of pixels corresponding to a pair25* of chroma samples, so the rest of the terms can be calculated just once.26* At typical sampling ratios, this eliminates half or three-quarters of the27* multiplications needed for color conversion.28*29* This file currently provides implementations for the following cases:30* YCbCr => RGB color conversion only.31* Sampling ratios of 2h1v or 2h2v.32* No scaling needed at upsample time.33* Corner-aligned (non-CCIR601) sampling alignment.34* Other special cases could be added, but in most applications these are35* the only common cases. (For uncommon cases we fall back on the more36* general code in jdsample.c and jdcolor.c.)37*/3839#define JPEG_INTERNALS40#include "jinclude.h"41#include "jpeglib.h"42#include "jsimd.h"43#include "jconfigint.h"4445#ifdef UPSAMPLE_MERGING_SUPPORTED464748/* Private subobject */4950typedef struct {51struct jpeg_upsampler pub; /* public fields */5253/* Pointer to routine to do actual upsampling/conversion of one row group */54void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,55JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf);5657/* Private state for YCC->RGB conversion */58int *Cr_r_tab; /* => table for Cr to R conversion */59int *Cb_b_tab; /* => table for Cb to B conversion */60JLONG *Cr_g_tab; /* => table for Cr to G conversion */61JLONG *Cb_g_tab; /* => table for Cb to G conversion */6263/* For 2:1 vertical sampling, we produce two output rows at a time.64* We need a "spare" row buffer to hold the second output row if the65* application provides just a one-row buffer; we also use the spare66* to discard the dummy last row if the image height is odd.67*/68JSAMPROW spare_row;69boolean spare_full; /* T if spare buffer is occupied */7071JDIMENSION out_row_width; /* samples per output row */72JDIMENSION rows_to_go; /* counts rows remaining in image */73} my_upsampler;7475typedef my_upsampler *my_upsample_ptr;7677#define SCALEBITS 16 /* speediest right-shift on some machines */78#define ONE_HALF ((JLONG) 1 << (SCALEBITS-1))79#define FIX(x) ((JLONG) ((x) * (1L<<SCALEBITS) + 0.5))808182/* Include inline routines for colorspace extensions */8384#include "jdmrgext.c"85#undef RGB_RED86#undef RGB_GREEN87#undef RGB_BLUE88#undef RGB_PIXELSIZE8990#define RGB_RED EXT_RGB_RED91#define RGB_GREEN EXT_RGB_GREEN92#define RGB_BLUE EXT_RGB_BLUE93#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE94#define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal95#define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal96#include "jdmrgext.c"97#undef RGB_RED98#undef RGB_GREEN99#undef RGB_BLUE100#undef RGB_PIXELSIZE101#undef h2v1_merged_upsample_internal102#undef h2v2_merged_upsample_internal103104#define RGB_RED EXT_RGBX_RED105#define RGB_GREEN EXT_RGBX_GREEN106#define RGB_BLUE EXT_RGBX_BLUE107#define RGB_ALPHA 3108#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE109#define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal110#define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal111#include "jdmrgext.c"112#undef RGB_RED113#undef RGB_GREEN114#undef RGB_BLUE115#undef RGB_ALPHA116#undef RGB_PIXELSIZE117#undef h2v1_merged_upsample_internal118#undef h2v2_merged_upsample_internal119120#define RGB_RED EXT_BGR_RED121#define RGB_GREEN EXT_BGR_GREEN122#define RGB_BLUE EXT_BGR_BLUE123#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE124#define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal125#define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal126#include "jdmrgext.c"127#undef RGB_RED128#undef RGB_GREEN129#undef RGB_BLUE130#undef RGB_PIXELSIZE131#undef h2v1_merged_upsample_internal132#undef h2v2_merged_upsample_internal133134#define RGB_RED EXT_BGRX_RED135#define RGB_GREEN EXT_BGRX_GREEN136#define RGB_BLUE EXT_BGRX_BLUE137#define RGB_ALPHA 3138#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE139#define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal140#define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal141#include "jdmrgext.c"142#undef RGB_RED143#undef RGB_GREEN144#undef RGB_BLUE145#undef RGB_ALPHA146#undef RGB_PIXELSIZE147#undef h2v1_merged_upsample_internal148#undef h2v2_merged_upsample_internal149150#define RGB_RED EXT_XBGR_RED151#define RGB_GREEN EXT_XBGR_GREEN152#define RGB_BLUE EXT_XBGR_BLUE153#define RGB_ALPHA 0154#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE155#define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal156#define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal157#include "jdmrgext.c"158#undef RGB_RED159#undef RGB_GREEN160#undef RGB_BLUE161#undef RGB_ALPHA162#undef RGB_PIXELSIZE163#undef h2v1_merged_upsample_internal164#undef h2v2_merged_upsample_internal165166#define RGB_RED EXT_XRGB_RED167#define RGB_GREEN EXT_XRGB_GREEN168#define RGB_BLUE EXT_XRGB_BLUE169#define RGB_ALPHA 0170#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE171#define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal172#define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal173#include "jdmrgext.c"174#undef RGB_RED175#undef RGB_GREEN176#undef RGB_BLUE177#undef RGB_ALPHA178#undef RGB_PIXELSIZE179#undef h2v1_merged_upsample_internal180#undef h2v2_merged_upsample_internal181182183/*184* Initialize tables for YCC->RGB colorspace conversion.185* This is taken directly from jdcolor.c; see that file for more info.186*/187188LOCAL(void)189build_ycc_rgb_table (j_decompress_ptr cinfo)190{191my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;192int i;193JLONG x;194SHIFT_TEMPS195196upsample->Cr_r_tab = (int *)197(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,198(MAXJSAMPLE+1) * sizeof(int));199upsample->Cb_b_tab = (int *)200(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,201(MAXJSAMPLE+1) * sizeof(int));202upsample->Cr_g_tab = (JLONG *)203(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,204(MAXJSAMPLE+1) * sizeof(JLONG));205upsample->Cb_g_tab = (JLONG *)206(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,207(MAXJSAMPLE+1) * sizeof(JLONG));208209for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {210/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */211/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */212/* Cr=>R value is nearest int to 1.40200 * x */213upsample->Cr_r_tab[i] = (int)214RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);215/* Cb=>B value is nearest int to 1.77200 * x */216upsample->Cb_b_tab[i] = (int)217RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);218/* Cr=>G value is scaled-up -0.71414 * x */219upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;220/* Cb=>G value is scaled-up -0.34414 * x */221/* We also add in ONE_HALF so that need not do it in inner loop */222upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;223}224}225226227/*228* Initialize for an upsampling pass.229*/230231METHODDEF(void)232start_pass_merged_upsample (j_decompress_ptr cinfo)233{234my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;235236/* Mark the spare buffer empty */237upsample->spare_full = FALSE;238/* Initialize total-height counter for detecting bottom of image */239upsample->rows_to_go = cinfo->output_height;240}241242243/*244* Control routine to do upsampling (and color conversion).245*246* The control routine just handles the row buffering considerations.247*/248249METHODDEF(void)250merged_2v_upsample (j_decompress_ptr cinfo,251JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,252JDIMENSION in_row_groups_avail,253JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,254JDIMENSION out_rows_avail)255/* 2:1 vertical sampling case: may need a spare row. */256{257my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;258JSAMPROW work_ptrs[2];259JDIMENSION num_rows; /* number of rows returned to caller */260261if (upsample->spare_full) {262/* If we have a spare row saved from a previous cycle, just return it. */263JDIMENSION size = upsample->out_row_width;264if (cinfo->out_color_space == JCS_RGB565)265size = cinfo->output_width * 2;266jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,2671, size);268num_rows = 1;269upsample->spare_full = FALSE;270} else {271/* Figure number of rows to return to caller. */272num_rows = 2;273/* Not more than the distance to the end of the image. */274if (num_rows > upsample->rows_to_go)275num_rows = upsample->rows_to_go;276/* And not more than what the client can accept: */277out_rows_avail -= *out_row_ctr;278if (num_rows > out_rows_avail)279num_rows = out_rows_avail;280/* Create output pointer array for upsampler. */281work_ptrs[0] = output_buf[*out_row_ctr];282if (num_rows > 1) {283work_ptrs[1] = output_buf[*out_row_ctr + 1];284} else {285work_ptrs[1] = upsample->spare_row;286upsample->spare_full = TRUE;287}288/* Now do the upsampling. */289(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);290}291292/* Adjust counts */293*out_row_ctr += num_rows;294upsample->rows_to_go -= num_rows;295/* When the buffer is emptied, declare this input row group consumed */296if (! upsample->spare_full)297(*in_row_group_ctr)++;298}299300301METHODDEF(void)302merged_1v_upsample (j_decompress_ptr cinfo,303JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,304JDIMENSION in_row_groups_avail,305JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,306JDIMENSION out_rows_avail)307/* 1:1 vertical sampling case: much easier, never need a spare row. */308{309my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;310311/* Just do the upsampling. */312(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,313output_buf + *out_row_ctr);314/* Adjust counts */315(*out_row_ctr)++;316(*in_row_group_ctr)++;317}318319320/*321* These are the routines invoked by the control routines to do322* the actual upsampling/conversion. One row group is processed per call.323*324* Note: since we may be writing directly into application-supplied buffers,325* we have to be honest about the output width; we can't assume the buffer326* has been rounded up to an even width.327*/328329330/*331* Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.332*/333334METHODDEF(void)335h2v1_merged_upsample (j_decompress_ptr cinfo,336JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,337JSAMPARRAY output_buf)338{339switch (cinfo->out_color_space) {340case JCS_EXT_RGB:341extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,342output_buf);343break;344case JCS_EXT_RGBX:345case JCS_EXT_RGBA:346extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,347output_buf);348break;349case JCS_EXT_BGR:350extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,351output_buf);352break;353case JCS_EXT_BGRX:354case JCS_EXT_BGRA:355extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,356output_buf);357break;358case JCS_EXT_XBGR:359case JCS_EXT_ABGR:360extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,361output_buf);362break;363case JCS_EXT_XRGB:364case JCS_EXT_ARGB:365extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,366output_buf);367break;368default:369h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,370output_buf);371break;372}373}374375376/*377* Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.378*/379380METHODDEF(void)381h2v2_merged_upsample (j_decompress_ptr cinfo,382JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,383JSAMPARRAY output_buf)384{385switch (cinfo->out_color_space) {386case JCS_EXT_RGB:387extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,388output_buf);389break;390case JCS_EXT_RGBX:391case JCS_EXT_RGBA:392extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,393output_buf);394break;395case JCS_EXT_BGR:396extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,397output_buf);398break;399case JCS_EXT_BGRX:400case JCS_EXT_BGRA:401extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,402output_buf);403break;404case JCS_EXT_XBGR:405case JCS_EXT_ABGR:406extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,407output_buf);408break;409case JCS_EXT_XRGB:410case JCS_EXT_ARGB:411extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,412output_buf);413break;414default:415h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,416output_buf);417break;418}419}420421422/*423* RGB565 conversion424*/425426#define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \427(((g) << 3) & 0x7E0) | ((b) >> 3))428#define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \429(((g) << 11) & 0xE000) | \430(((b) << 5) & 0x1F00))431432#define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)433#define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)434435#define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)436437#define WRITE_TWO_PIXELS_LE(addr, pixels) { \438((INT16*)(addr))[0] = (INT16)(pixels); \439((INT16*)(addr))[1] = (INT16)((pixels) >> 16); \440}441#define WRITE_TWO_PIXELS_BE(addr, pixels) { \442((INT16*)(addr))[1] = (INT16)(pixels); \443((INT16*)(addr))[0] = (INT16)((pixels) >> 16); \444}445446#define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))447#define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))448#define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))449450451/* Declarations for ordered dithering452*453* We use a 4x4 ordered dither array packed into 32 bits. This array is454* sufficent for dithering RGB888 to RGB565.455*/456457#define DITHER_MASK 0x3458#define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))459static const JLONG dither_matrix[4] = {4600x0008020A,4610x0C040E06,4620x030B0109,4630x0F070D05464};465466467/* Include inline routines for RGB565 conversion */468469#define PACK_SHORT_565 PACK_SHORT_565_LE470#define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE471#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE472#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le473#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le474#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le475#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le476#include "jdmrg565.c"477#undef PACK_SHORT_565478#undef PACK_TWO_PIXELS479#undef WRITE_TWO_PIXELS480#undef h2v1_merged_upsample_565_internal481#undef h2v1_merged_upsample_565D_internal482#undef h2v2_merged_upsample_565_internal483#undef h2v2_merged_upsample_565D_internal484485#define PACK_SHORT_565 PACK_SHORT_565_BE486#define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE487#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE488#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be489#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be490#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be491#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be492#include "jdmrg565.c"493#undef PACK_SHORT_565494#undef PACK_TWO_PIXELS495#undef WRITE_TWO_PIXELS496#undef h2v1_merged_upsample_565_internal497#undef h2v1_merged_upsample_565D_internal498#undef h2v2_merged_upsample_565_internal499#undef h2v2_merged_upsample_565D_internal500501502static INLINE boolean is_big_endian(void)503{504int test_value = 1;505if (*(char *)&test_value != 1)506return TRUE;507return FALSE;508}509510511METHODDEF(void)512h2v1_merged_upsample_565 (j_decompress_ptr cinfo,513JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,514JSAMPARRAY output_buf)515{516if (is_big_endian())517h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,518output_buf);519else520h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,521output_buf);522}523524525METHODDEF(void)526h2v1_merged_upsample_565D (j_decompress_ptr cinfo,527JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,528JSAMPARRAY output_buf)529{530if (is_big_endian())531h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,532output_buf);533else534h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,535output_buf);536}537538539METHODDEF(void)540h2v2_merged_upsample_565 (j_decompress_ptr cinfo,541JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,542JSAMPARRAY output_buf)543{544if (is_big_endian())545h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,546output_buf);547else548h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,549output_buf);550}551552553METHODDEF(void)554h2v2_merged_upsample_565D (j_decompress_ptr cinfo,555JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,556JSAMPARRAY output_buf)557{558if (is_big_endian())559h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,560output_buf);561else562h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,563output_buf);564}565566567/*568* Module initialization routine for merged upsampling/color conversion.569*570* NB: this is called under the conditions determined by use_merged_upsample()571* in jdmaster.c. That routine MUST correspond to the actual capabilities572* of this module; no safety checks are made here.573*/574575GLOBAL(void)576jinit_merged_upsampler (j_decompress_ptr cinfo)577{578my_upsample_ptr upsample;579580upsample = (my_upsample_ptr)581(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,582sizeof(my_upsampler));583cinfo->upsample = (struct jpeg_upsampler *) upsample;584upsample->pub.start_pass = start_pass_merged_upsample;585upsample->pub.need_context_rows = FALSE;586587upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;588589if (cinfo->max_v_samp_factor == 2) {590upsample->pub.upsample = merged_2v_upsample;591if (jsimd_can_h2v2_merged_upsample())592upsample->upmethod = jsimd_h2v2_merged_upsample;593else594upsample->upmethod = h2v2_merged_upsample;595if (cinfo->out_color_space == JCS_RGB565) {596if (cinfo->dither_mode != JDITHER_NONE) {597upsample->upmethod = h2v2_merged_upsample_565D;598} else {599upsample->upmethod = h2v2_merged_upsample_565;600}601}602/* Allocate a spare row buffer */603upsample->spare_row = (JSAMPROW)604(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,605(size_t) (upsample->out_row_width * sizeof(JSAMPLE)));606} else {607upsample->pub.upsample = merged_1v_upsample;608if (jsimd_can_h2v1_merged_upsample())609upsample->upmethod = jsimd_h2v1_merged_upsample;610else611upsample->upmethod = h2v1_merged_upsample;612if (cinfo->out_color_space == JCS_RGB565) {613if (cinfo->dither_mode != JDITHER_NONE) {614upsample->upmethod = h2v1_merged_upsample_565D;615} else {616upsample->upmethod = h2v1_merged_upsample_565;617}618}619/* No spare row needed */620upsample->spare_row = NULL;621}622623build_ycc_rgb_table(cinfo);624}625626#endif /* UPSAMPLE_MERGING_SUPPORTED */627628629