shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdmerge.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1994-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB</ossman@cendio.se>
shun-iwasawa 82a8f5
 * Copyright (C) 2009, 2011, 2014-2015, 2020, D. R. Commander.
shun-iwasawa 82a8f5
 * Copyright (C) 2013, Linaro Limited.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains code for merged upsampling/color conversion.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file combines functions from jdsample.c and jdcolor.c;
shun-iwasawa 82a8f5
 * read those files first to understand what's going on.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * When the chroma components are to be upsampled by simple replication
shun-iwasawa 82a8f5
 * (ie, box filtering), we can save some work in color conversion by
shun-iwasawa 82a8f5
 * calculating all the output pixels corresponding to a pair of chroma
shun-iwasawa 82a8f5
 * samples at one time.  In the conversion equations
shun-iwasawa 82a8f5
 *      R = Y           + K1 * Cr
shun-iwasawa 82a8f5
 *      G = Y + K2 * Cb + K3 * Cr
shun-iwasawa 82a8f5
 *      B = Y + K4 * Cb
shun-iwasawa 82a8f5
 * only the Y term varies among the group of pixels corresponding to a pair
shun-iwasawa 82a8f5
 * of chroma samples, so the rest of the terms can be calculated just once.
shun-iwasawa 82a8f5
 * At typical sampling ratios, this eliminates half or three-quarters of the
shun-iwasawa 82a8f5
 * multiplications needed for color conversion.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file currently provides implementations for the following cases:
shun-iwasawa 82a8f5
 *      YCbCr => RGB color conversion only.
shun-iwasawa 82a8f5
 *      Sampling ratios of 2h1v or 2h2v.
shun-iwasawa 82a8f5
 *      No scaling needed at upsample time.
shun-iwasawa 82a8f5
 *      Corner-aligned (non-CCIR601) sampling alignment.
shun-iwasawa 82a8f5
 * Other special cases could be added, but in most applications these are
shun-iwasawa 82a8f5
 * the only common cases.  (For uncommon cases we fall back on the more
shun-iwasawa 82a8f5
 * general code in jdsample.c and jdcolor.c.)
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jdmerge.h"
shun-iwasawa 82a8f5
#include "jsimd.h"
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef UPSAMPLE_MERGING_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define SCALEBITS       16      /* speediest right-shift on some machines */
shun-iwasawa 82a8f5
#define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
shun-iwasawa 82a8f5
#define FIX(x)          ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Include inline routines for colorspace extensions */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_RGB_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_RGB_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_RGB_BLUE
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_RGB_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extrgb_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extrgb_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_RGBX_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_RGBX_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_RGBX_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  3
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_RGBX_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extrgbx_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extrgbx_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_BGR_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_BGR_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_BGR_BLUE
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_BGR_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extbgr_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extbgr_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_BGRX_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_BGRX_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_BGRX_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  3
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_BGRX_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extbgrx_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extbgrx_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_XBGR_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_XBGR_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_XBGR_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  0
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_XBGR_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extxbgr_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extxbgr_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define RGB_RED  EXT_XRGB_RED
shun-iwasawa 82a8f5
#define RGB_GREEN  EXT_XRGB_GREEN
shun-iwasawa 82a8f5
#define RGB_BLUE  EXT_XRGB_BLUE
shun-iwasawa 82a8f5
#define RGB_ALPHA  0
shun-iwasawa 82a8f5
#define RGB_PIXELSIZE  EXT_XRGB_PIXELSIZE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_internal  extxrgb_h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_internal  extxrgb_h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
#include "jdmrgext.c"
shun-iwasawa 82a8f5
#undef RGB_RED
shun-iwasawa 82a8f5
#undef RGB_GREEN
shun-iwasawa 82a8f5
#undef RGB_BLUE
shun-iwasawa 82a8f5
#undef RGB_ALPHA
shun-iwasawa 82a8f5
#undef RGB_PIXELSIZE
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize tables for YCC->RGB colorspace conversion.
shun-iwasawa 82a8f5
 * This is taken directly from jdcolor.c; see that file for more info.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
build_ycc_rgb_table(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
  JLONG x;
shun-iwasawa 82a8f5
  SHIFT_TEMPS
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  upsample->Cr_r_tab = (int *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(int));
shun-iwasawa 82a8f5
  upsample->Cb_b_tab = (int *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(int));
shun-iwasawa 82a8f5
  upsample->Cr_g_tab = (JLONG *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
shun-iwasawa 82a8f5
  upsample->Cb_g_tab = (JLONG *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
shun-iwasawa 82a8f5
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
shun-iwasawa 82a8f5
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
shun-iwasawa 82a8f5
    /* Cr=>R value is nearest int to 1.40200 * x */
shun-iwasawa 82a8f5
    upsample->Cr_r_tab[i] = (int)
shun-iwasawa 82a8f5
                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
shun-iwasawa 82a8f5
    /* Cb=>B value is nearest int to 1.77200 * x */
shun-iwasawa 82a8f5
    upsample->Cb_b_tab[i] = (int)
shun-iwasawa 82a8f5
                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
shun-iwasawa 82a8f5
    /* Cr=>G value is scaled-up -0.71414 * x */
shun-iwasawa 82a8f5
    upsample->Cr_g_tab[i] = (-FIX(0.71414)) * x;
shun-iwasawa 82a8f5
    /* Cb=>G value is scaled-up -0.34414 * x */
shun-iwasawa 82a8f5
    /* We also add in ONE_HALF so that need not do it in inner loop */
shun-iwasawa 82a8f5
    upsample->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize for an upsampling pass.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_pass_merged_upsample(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Mark the spare buffer empty */
shun-iwasawa 82a8f5
  upsample->spare_full = FALSE;
shun-iwasawa 82a8f5
  /* Initialize total-height counter for detecting bottom of image */
shun-iwasawa 82a8f5
  upsample->rows_to_go = cinfo->output_height;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Control routine to do upsampling (and color conversion).
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * The control routine just handles the row buffering considerations.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
merged_2v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                   JDIMENSION *in_row_group_ctr,
shun-iwasawa 82a8f5
                   JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
shun-iwasawa 82a8f5
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
shun-iwasawa 82a8f5
/* 2:1 vertical sampling case: may need a spare row. */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
  JSAMPROW work_ptrs[2];
shun-iwasawa 82a8f5
  JDIMENSION num_rows;          /* number of rows returned to caller */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (upsample->spare_full) {
shun-iwasawa 82a8f5
    /* If we have a spare row saved from a previous cycle, just return it. */
shun-iwasawa 82a8f5
    JDIMENSION size = upsample->out_row_width;
shun-iwasawa 82a8f5
    if (cinfo->out_color_space == JCS_RGB565)
shun-iwasawa 82a8f5
      size = cinfo->output_width * 2;
shun-iwasawa 82a8f5
    jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
shun-iwasawa 82a8f5
                      size);
shun-iwasawa 82a8f5
    num_rows = 1;
shun-iwasawa 82a8f5
    upsample->spare_full = FALSE;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* Figure number of rows to return to caller. */
shun-iwasawa 82a8f5
    num_rows = 2;
shun-iwasawa 82a8f5
    /* Not more than the distance to the end of the image. */
shun-iwasawa 82a8f5
    if (num_rows > upsample->rows_to_go)
shun-iwasawa 82a8f5
      num_rows = upsample->rows_to_go;
shun-iwasawa 82a8f5
    /* And not more than what the client can accept: */
shun-iwasawa 82a8f5
    out_rows_avail -= *out_row_ctr;
shun-iwasawa 82a8f5
    if (num_rows > out_rows_avail)
shun-iwasawa 82a8f5
      num_rows = out_rows_avail;
shun-iwasawa 82a8f5
    /* Create output pointer array for upsampler. */
shun-iwasawa 82a8f5
    work_ptrs[0] = output_buf[*out_row_ctr];
shun-iwasawa 82a8f5
    if (num_rows > 1) {
shun-iwasawa 82a8f5
      work_ptrs[1] = output_buf[*out_row_ctr + 1];
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      work_ptrs[1] = upsample->spare_row;
shun-iwasawa 82a8f5
      upsample->spare_full = TRUE;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* Now do the upsampling. */
shun-iwasawa 82a8f5
    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Adjust counts */
shun-iwasawa 82a8f5
  *out_row_ctr += num_rows;
shun-iwasawa 82a8f5
  upsample->rows_to_go -= num_rows;
shun-iwasawa 82a8f5
  /* When the buffer is emptied, declare this input row group consumed */
shun-iwasawa 82a8f5
  if (!upsample->spare_full)
shun-iwasawa 82a8f5
    (*in_row_group_ctr)++;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                   JDIMENSION *in_row_group_ctr,
shun-iwasawa 82a8f5
                   JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
shun-iwasawa 82a8f5
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
shun-iwasawa 82a8f5
/* 1:1 vertical sampling case: much easier, never need a spare row. */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Just do the upsampling. */
shun-iwasawa 82a8f5
  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
shun-iwasawa 82a8f5
                         output_buf + *out_row_ctr);
shun-iwasawa 82a8f5
  /* Adjust counts */
shun-iwasawa 82a8f5
  (*out_row_ctr)++;
shun-iwasawa 82a8f5
  (*in_row_group_ctr)++;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * These are the routines invoked by the control routines to do
shun-iwasawa 82a8f5
 * the actual upsampling/conversion.  One row group is processed per call.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Note: since we may be writing directly into application-supplied buffers,
shun-iwasawa 82a8f5
 * we have to be honest about the output width; we can't assume the buffer
shun-iwasawa 82a8f5
 * has been rounded up to an even width.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                     JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
    extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                         output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
    extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
    extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                         output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
    extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
    extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                  output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                     JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  switch (cinfo->out_color_space) {
shun-iwasawa 82a8f5
  case JCS_EXT_RGB:
shun-iwasawa 82a8f5
    extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                         output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_RGBX:
shun-iwasawa 82a8f5
  case JCS_EXT_RGBA:
shun-iwasawa 82a8f5
    extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGR:
shun-iwasawa 82a8f5
    extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                         output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_BGRX:
shun-iwasawa 82a8f5
  case JCS_EXT_BGRA:
shun-iwasawa 82a8f5
    extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XBGR:
shun-iwasawa 82a8f5
  case JCS_EXT_ABGR:
shun-iwasawa 82a8f5
    extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case JCS_EXT_XRGB:
shun-iwasawa 82a8f5
  case JCS_EXT_ARGB:
shun-iwasawa 82a8f5
    extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                          output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                  output_buf);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * RGB565 conversion
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565_LE(r, g, b) \
shun-iwasawa 82a8f5
  ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
shun-iwasawa 82a8f5
#define PACK_SHORT_565_BE(r, g, b) \
shun-iwasawa 82a8f5
  (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS_LE(l, r)    ((r << 16) | l)
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS_BE(l, r)    ((l << 16) | r)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define WRITE_TWO_PIXELS_LE(addr, pixels) { \
shun-iwasawa 82a8f5
  ((INT16 *)(addr))[0] = (INT16)(pixels); \
shun-iwasawa 82a8f5
  ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#define WRITE_TWO_PIXELS_BE(addr, pixels) { \
shun-iwasawa 82a8f5
  ((INT16 *)(addr))[1] = (INT16)(pixels); \
shun-iwasawa 82a8f5
  ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
shun-iwasawa 82a8f5
#define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
shun-iwasawa 82a8f5
#define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Declarations for ordered dithering
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We use a 4x4 ordered dither array packed into 32 bits.  This array is
shun-iwasawa 82a8f5
 * sufficient for dithering RGB888 to RGB565.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define DITHER_MASK       0x3
shun-iwasawa 82a8f5
#define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
shun-iwasawa 82a8f5
static const JLONG dither_matrix[4] = {
shun-iwasawa 82a8f5
  0x0008020A,
shun-iwasawa 82a8f5
  0x0C040E06,
shun-iwasawa 82a8f5
  0x030B0109,
shun-iwasawa 82a8f5
  0x0F070D05
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Include inline routines for RGB565 conversion */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565  PACK_SHORT_565_LE
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_LE
shun-iwasawa 82a8f5
#define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_LE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_le
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_le
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_le
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_le
shun-iwasawa 82a8f5
#include "jdmrg565.c"
shun-iwasawa 82a8f5
#undef PACK_SHORT_565
shun-iwasawa 82a8f5
#undef PACK_TWO_PIXELS
shun-iwasawa 82a8f5
#undef WRITE_TWO_PIXELS
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_565_internal
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_565D_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_565_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_565D_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PACK_SHORT_565  PACK_SHORT_565_BE
shun-iwasawa 82a8f5
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_BE
shun-iwasawa 82a8f5
#define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_BE
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_be
shun-iwasawa 82a8f5
#define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_be
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_be
shun-iwasawa 82a8f5
#define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_be
shun-iwasawa 82a8f5
#include "jdmrg565.c"
shun-iwasawa 82a8f5
#undef PACK_SHORT_565
shun-iwasawa 82a8f5
#undef PACK_TWO_PIXELS
shun-iwasawa 82a8f5
#undef WRITE_TWO_PIXELS
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_565_internal
shun-iwasawa 82a8f5
#undef h2v1_merged_upsample_565D_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_565_internal
shun-iwasawa 82a8f5
#undef h2v2_merged_upsample_565D_internal
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static INLINE boolean is_big_endian(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int test_value = 1;
shun-iwasawa 82a8f5
  if (*(char *)&test_value != 1)
shun-iwasawa 82a8f5
    return TRUE;
shun-iwasawa 82a8f5
  return FALSE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                         JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                output_buf);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                output_buf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                 output_buf);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                 output_buf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                         JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                output_buf);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                output_buf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (is_big_endian())
shun-iwasawa 82a8f5
    h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                 output_buf);
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
shun-iwasawa 82a8f5
                                 output_buf);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Module initialization routine for merged upsampling/color conversion.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * NB: this is called under the conditions determined by use_merged_upsample()
shun-iwasawa 82a8f5
 * in jdmaster.c.  That routine MUST correspond to the actual capabilities
shun-iwasawa 82a8f5
 * of this module; no safety checks are made here.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jinit_merged_upsampler(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_merged_upsample_ptr upsample;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  upsample = (my_merged_upsample_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(my_merged_upsampler));
shun-iwasawa 82a8f5
  cinfo->upsample = (struct jpeg_upsampler *)upsample;
shun-iwasawa 82a8f5
  upsample->pub.start_pass = start_pass_merged_upsample;
shun-iwasawa 82a8f5
  upsample->pub.need_context_rows = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->max_v_samp_factor == 2) {
shun-iwasawa 82a8f5
    upsample->pub.upsample = merged_2v_upsample;
shun-iwasawa 82a8f5
    if (jsimd_can_h2v2_merged_upsample())
shun-iwasawa 82a8f5
      upsample->upmethod = jsimd_h2v2_merged_upsample;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      upsample->upmethod = h2v2_merged_upsample;
shun-iwasawa 82a8f5
    if (cinfo->out_color_space == JCS_RGB565) {
shun-iwasawa 82a8f5
      if (cinfo->dither_mode != JDITHER_NONE) {
shun-iwasawa 82a8f5
        upsample->upmethod = h2v2_merged_upsample_565D;
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        upsample->upmethod = h2v2_merged_upsample_565;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* Allocate a spare row buffer */
shun-iwasawa 82a8f5
    upsample->spare_row = (JSAMPROW)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    upsample->pub.upsample = merged_1v_upsample;
shun-iwasawa 82a8f5
    if (jsimd_can_h2v1_merged_upsample())
shun-iwasawa 82a8f5
      upsample->upmethod = jsimd_h2v1_merged_upsample;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      upsample->upmethod = h2v1_merged_upsample;
shun-iwasawa 82a8f5
    if (cinfo->out_color_space == JCS_RGB565) {
shun-iwasawa 82a8f5
      if (cinfo->dither_mode != JDITHER_NONE) {
shun-iwasawa 82a8f5
        upsample->upmethod = h2v1_merged_upsample_565D;
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        upsample->upmethod = h2v1_merged_upsample_565;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* No spare row needed */
shun-iwasawa 82a8f5
    upsample->spare_row = NULL;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  build_ycc_rgb_table(cinfo);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* UPSAMPLE_MERGING_SUPPORTED */