0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdmerge.c
0N/A *
0N/A * Copyright (C) 1994-1996, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains code for merged upsampling/color conversion.
0N/A *
0N/A * This file combines functions from jdsample.c and jdcolor.c;
0N/A * read those files first to understand what's going on.
0N/A *
0N/A * When the chroma components are to be upsampled by simple replication
0N/A * (ie, box filtering), we can save some work in color conversion by
0N/A * calculating all the output pixels corresponding to a pair of chroma
0N/A * samples at one time. In the conversion equations
0N/A * R = Y + K1 * Cr
0N/A * G = Y + K2 * Cb + K3 * Cr
0N/A * B = Y + K4 * Cb
0N/A * only the Y term varies among the group of pixels corresponding to a pair
0N/A * of chroma samples, so the rest of the terms can be calculated just once.
0N/A * At typical sampling ratios, this eliminates half or three-quarters of the
0N/A * multiplications needed for color conversion.
0N/A *
0N/A * This file currently provides implementations for the following cases:
0N/A * YCbCr => RGB color conversion only.
0N/A * Sampling ratios of 2h1v or 2h2v.
0N/A * No scaling needed at upsample time.
0N/A * Corner-aligned (non-CCIR601) sampling alignment.
0N/A * Other special cases could be added, but in most applications these are
0N/A * the only common cases. (For uncommon cases we fall back on the more
0N/A * general code in jdsample.c and jdcolor.c.)
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A#ifdef UPSAMPLE_MERGING_SUPPORTED
0N/A
0N/A
0N/A/* Private subobject */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_upsampler pub; /* public fields */
0N/A
0N/A /* Pointer to routine to do actual upsampling/conversion of one row group */
0N/A JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
0N/A JSAMPARRAY output_buf));
0N/A
0N/A /* Private state for YCC->RGB conversion */
0N/A int * Cr_r_tab; /* => table for Cr to R conversion */
0N/A int * Cb_b_tab; /* => table for Cb to B conversion */
0N/A INT32 * Cr_g_tab; /* => table for Cr to G conversion */
0N/A INT32 * Cb_g_tab; /* => table for Cb to G conversion */
0N/A
0N/A /* For 2:1 vertical sampling, we produce two output rows at a time.
0N/A * We need a "spare" row buffer to hold the second output row if the
0N/A * application provides just a one-row buffer; we also use the spare
0N/A * to discard the dummy last row if the image height is odd.
0N/A */
0N/A JSAMPROW spare_row;
0N/A boolean spare_full; /* T if spare buffer is occupied */
0N/A
0N/A JDIMENSION out_row_width; /* samples per output row */
0N/A JDIMENSION rows_to_go; /* counts rows remaining in image */
0N/A} my_upsampler;
0N/A
0N/Atypedef my_upsampler * my_upsample_ptr;
0N/A
0N/A#define SCALEBITS 16 /* speediest right-shift on some machines */
0N/A#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
0N/A#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
0N/A
0N/A
0N/A/*
0N/A * Initialize tables for YCC->RGB colorspace conversion.
0N/A * This is taken directly from jdcolor.c; see that file for more info.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Abuild_ycc_rgb_table (j_decompress_ptr cinfo)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A int i;
0N/A INT32 x;
0N/A SHIFT_TEMPS
0N/A
0N/A upsample->Cr_r_tab = (int *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(int));
0N/A upsample->Cb_b_tab = (int *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(int));
0N/A upsample->Cr_g_tab = (INT32 *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(INT32));
0N/A upsample->Cb_g_tab = (INT32 *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(INT32));
0N/A
0N/A for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
0N/A /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
0N/A /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
0N/A /* Cr=>R value is nearest int to 1.40200 * x */
0N/A upsample->Cr_r_tab[i] = (int)
0N/A RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
0N/A /* Cb=>B value is nearest int to 1.77200 * x */
0N/A upsample->Cb_b_tab[i] = (int)
0N/A RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
0N/A /* Cr=>G value is scaled-up -0.71414 * x */
0N/A upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
0N/A /* Cb=>G value is scaled-up -0.34414 * x */
0N/A /* We also add in ONE_HALF so that need not do it in inner loop */
0N/A upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Initialize for an upsampling pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_merged_upsample (j_decompress_ptr cinfo)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A
0N/A /* Mark the spare buffer empty */
0N/A upsample->spare_full = FALSE;
0N/A /* Initialize total-height counter for detecting bottom of image */
0N/A upsample->rows_to_go = cinfo->output_height;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Control routine to do upsampling (and color conversion).
0N/A *
0N/A * The control routine just handles the row buffering considerations.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Amerged_2v_upsample (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
0N/A JDIMENSION in_row_groups_avail,
0N/A JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
0N/A JDIMENSION out_rows_avail)
0N/A/* 2:1 vertical sampling case: may need a spare row. */
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A JSAMPROW work_ptrs[2];
0N/A JDIMENSION num_rows; /* number of rows returned to caller */
0N/A
0N/A if (upsample->spare_full) {
0N/A /* If we have a spare row saved from a previous cycle, just return it. */
0N/A jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
0N/A 1, upsample->out_row_width);
0N/A num_rows = 1;
0N/A upsample->spare_full = FALSE;
0N/A } else {
0N/A /* Figure number of rows to return to caller. */
0N/A num_rows = 2;
0N/A /* Not more than the distance to the end of the image. */
0N/A if (num_rows > upsample->rows_to_go)
0N/A num_rows = upsample->rows_to_go;
0N/A /* And not more than what the client can accept: */
0N/A out_rows_avail -= *out_row_ctr;
0N/A if (num_rows > out_rows_avail)
0N/A num_rows = out_rows_avail;
0N/A /* Create output pointer array for upsampler. */
0N/A work_ptrs[0] = output_buf[*out_row_ctr];
0N/A if (num_rows > 1) {
0N/A work_ptrs[1] = output_buf[*out_row_ctr + 1];
0N/A } else {
0N/A work_ptrs[1] = upsample->spare_row;
0N/A upsample->spare_full = TRUE;
0N/A }
0N/A /* Now do the upsampling. */
0N/A (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
0N/A }
0N/A
0N/A /* Adjust counts */
0N/A *out_row_ctr += num_rows;
0N/A upsample->rows_to_go -= num_rows;
0N/A /* When the buffer is emptied, declare this input row group consumed */
0N/A if (! upsample->spare_full)
0N/A (*in_row_group_ctr)++;
0N/A}
0N/A
0N/A
0N/AMETHODDEF(void)
0N/Amerged_1v_upsample (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
0N/A JDIMENSION in_row_groups_avail,
0N/A JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
0N/A JDIMENSION out_rows_avail)
0N/A/* 1:1 vertical sampling case: much easier, never need a spare row. */
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A
0N/A /* Just do the upsampling. */
0N/A (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
0N/A output_buf + *out_row_ctr);
0N/A /* Adjust counts */
0N/A (*out_row_ctr)++;
0N/A (*in_row_group_ctr)++;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * These are the routines invoked by the control routines to do
0N/A * the actual upsampling/conversion. One row group is processed per call.
0N/A *
0N/A * Note: since we may be writing directly into application-supplied buffers,
0N/A * we have to be honest about the output width; we can't assume the buffer
0N/A * has been rounded up to an even width.
0N/A */
0N/A
0N/A
0N/A/*
0N/A * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v1_merged_upsample (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
0N/A JSAMPARRAY output_buf)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A register int y, cred, cgreen, cblue;
0N/A int cb, cr;
0N/A register JSAMPROW outptr;
0N/A JSAMPROW inptr0, inptr1, inptr2;
0N/A JDIMENSION col;
0N/A /* copy these pointers into registers if possible */
0N/A register JSAMPLE * range_limit = cinfo->sample_range_limit;
0N/A int * Crrtab = upsample->Cr_r_tab;
0N/A int * Cbbtab = upsample->Cb_b_tab;
0N/A INT32 * Crgtab = upsample->Cr_g_tab;
0N/A INT32 * Cbgtab = upsample->Cb_g_tab;
0N/A SHIFT_TEMPS
0N/A
0N/A inptr0 = input_buf[0][in_row_group_ctr];
0N/A inptr1 = input_buf[1][in_row_group_ctr];
0N/A inptr2 = input_buf[2][in_row_group_ctr];
0N/A outptr = output_buf[0];
0N/A /* Loop for each pair of output pixels */
0N/A for (col = cinfo->output_width >> 1; col > 0; col--) {
0N/A /* Do the chroma part of the calculation */
0N/A cb = GETJSAMPLE(*inptr1++);
0N/A cr = GETJSAMPLE(*inptr2++);
0N/A cred = Crrtab[cr];
0N/A cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
0N/A cblue = Cbbtab[cb];
0N/A /* Fetch 2 Y values and emit 2 pixels */
0N/A y = GETJSAMPLE(*inptr0++);
0N/A outptr[RGB_RED] = range_limit[y + cred];
0N/A outptr[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr += RGB_PIXELSIZE;
0N/A y = GETJSAMPLE(*inptr0++);
0N/A outptr[RGB_RED] = range_limit[y + cred];
0N/A outptr[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr += RGB_PIXELSIZE;
0N/A }
0N/A /* If image width is odd, do the last output column separately */
0N/A if (cinfo->output_width & 1) {
0N/A cb = GETJSAMPLE(*inptr1);
0N/A cr = GETJSAMPLE(*inptr2);
0N/A cred = Crrtab[cr];
0N/A cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
0N/A cblue = Cbbtab[cb];
0N/A y = GETJSAMPLE(*inptr0);
0N/A outptr[RGB_RED] = range_limit[y + cred];
0N/A outptr[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr[RGB_BLUE] = range_limit[y + cblue];
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v2_merged_upsample (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
0N/A JSAMPARRAY output_buf)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A register int y, cred, cgreen, cblue;
0N/A int cb, cr;
0N/A register JSAMPROW outptr0, outptr1;
0N/A JSAMPROW inptr00, inptr01, inptr1, inptr2;
0N/A JDIMENSION col;
0N/A /* copy these pointers into registers if possible */
0N/A register JSAMPLE * range_limit = cinfo->sample_range_limit;
0N/A int * Crrtab = upsample->Cr_r_tab;
0N/A int * Cbbtab = upsample->Cb_b_tab;
0N/A INT32 * Crgtab = upsample->Cr_g_tab;
0N/A INT32 * Cbgtab = upsample->Cb_g_tab;
0N/A SHIFT_TEMPS
0N/A
0N/A inptr00 = input_buf[0][in_row_group_ctr*2];
0N/A inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
0N/A inptr1 = input_buf[1][in_row_group_ctr];
0N/A inptr2 = input_buf[2][in_row_group_ctr];
0N/A outptr0 = output_buf[0];
0N/A outptr1 = output_buf[1];
0N/A /* Loop for each group of output pixels */
0N/A for (col = cinfo->output_width >> 1; col > 0; col--) {
0N/A /* Do the chroma part of the calculation */
0N/A cb = GETJSAMPLE(*inptr1++);
0N/A cr = GETJSAMPLE(*inptr2++);
0N/A cred = Crrtab[cr];
0N/A cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
0N/A cblue = Cbbtab[cb];
0N/A /* Fetch 4 Y values and emit 4 pixels */
0N/A y = GETJSAMPLE(*inptr00++);
0N/A outptr0[RGB_RED] = range_limit[y + cred];
0N/A outptr0[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr0[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr0 += RGB_PIXELSIZE;
0N/A y = GETJSAMPLE(*inptr00++);
0N/A outptr0[RGB_RED] = range_limit[y + cred];
0N/A outptr0[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr0[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr0 += RGB_PIXELSIZE;
0N/A y = GETJSAMPLE(*inptr01++);
0N/A outptr1[RGB_RED] = range_limit[y + cred];
0N/A outptr1[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr1[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr1 += RGB_PIXELSIZE;
0N/A y = GETJSAMPLE(*inptr01++);
0N/A outptr1[RGB_RED] = range_limit[y + cred];
0N/A outptr1[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr1[RGB_BLUE] = range_limit[y + cblue];
0N/A outptr1 += RGB_PIXELSIZE;
0N/A }
0N/A /* If image width is odd, do the last output column separately */
0N/A if (cinfo->output_width & 1) {
0N/A cb = GETJSAMPLE(*inptr1);
0N/A cr = GETJSAMPLE(*inptr2);
0N/A cred = Crrtab[cr];
0N/A cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
0N/A cblue = Cbbtab[cb];
0N/A y = GETJSAMPLE(*inptr00);
0N/A outptr0[RGB_RED] = range_limit[y + cred];
0N/A outptr0[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr0[RGB_BLUE] = range_limit[y + cblue];
0N/A y = GETJSAMPLE(*inptr01);
0N/A outptr1[RGB_RED] = range_limit[y + cred];
0N/A outptr1[RGB_GREEN] = range_limit[y + cgreen];
0N/A outptr1[RGB_BLUE] = range_limit[y + cblue];
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Module initialization routine for merged upsampling/color conversion.
0N/A *
0N/A * NB: this is called under the conditions determined by use_merged_upsample()
0N/A * in jdmaster.c. That routine MUST correspond to the actual capabilities
0N/A * of this module; no safety checks are made here.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_merged_upsampler (j_decompress_ptr cinfo)
0N/A{
0N/A my_upsample_ptr upsample;
0N/A
0N/A upsample = (my_upsample_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_upsampler));
0N/A cinfo->upsample = (struct jpeg_upsampler *) upsample;
0N/A upsample->pub.start_pass = start_pass_merged_upsample;
0N/A upsample->pub.need_context_rows = FALSE;
0N/A
0N/A upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
0N/A
0N/A if (cinfo->max_v_samp_factor == 2) {
0N/A upsample->pub.upsample = merged_2v_upsample;
0N/A upsample->upmethod = h2v2_merged_upsample;
0N/A /* Allocate a spare row buffer */
0N/A upsample->spare_row = (JSAMPROW)
0N/A (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
0N/A } else {
0N/A upsample->pub.upsample = merged_1v_upsample;
0N/A upsample->upmethod = h2v1_merged_upsample;
0N/A /* No spare row needed */
0N/A upsample->spare_row = NULL;
0N/A }
0N/A
0N/A build_ycc_rgb_table(cinfo);
0N/A}
0N/A
0N/A#endif /* UPSAMPLE_MERGING_SUPPORTED */