<commit_before>/*
 *  OpenSlide, a library for reading whole slide image files
 *
 *  Copyright (c) 2007-2013 Carnegie Mellon University
 *  Copyright (c) 2011 Google, Inc.
 *  All rights reserved.
 *
 *  OpenSlide is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, version 2.1.
 *
 *  OpenSlide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with OpenSlide. If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "openslide-private.h"

#include <openjpeg.h>

// TODO: replace with tables
static void write_pixel_ycbcr(uint32_t *dest,
                              uint8_t c0, uint8_t c1, uint8_t c2) {
  double R = c0 + 1.402 * (c2 - 128);
  double G = c0 - 0.34414 * (c1 - 128) - 0.71414 * (c2 - 128);
  double B = c0 + 1.772 * (c1 - 128);

  R = CLAMP(R, 0, 255);
  G = CLAMP(G, 0, 255);
  B = CLAMP(B, 0, 255);

  *dest = 255 << 24 | ((uint8_t) R << 16) | ((uint8_t) G << 8) | ((uint8_t) B);
}

static void write_pixel_rgb(uint32_t *dest,
                            uint8_t c0, uint8_t c1, uint8_t c2) {
  *dest = 255 << 24 | c0 << 16 | c1 << 8 | c2;
}

static void unpack_argb(enum _openslide_jp2k_colorspace space,
                        opj_image_comp_t *comps,
                        uint32_t *dest,
                        int32_t w, int32_t h) {
  // TODO: too slow, and with duplicated code!

  int c0_sub_x = w / comps[0].w;
  int c1_sub_x = w / comps[1].w;
  int c2_sub_x = w / comps[2].w;
  int c0_sub_y = h / comps[0].h;
  int c1_sub_y = h / comps[1].h;
  int c2_sub_y = h / comps[2].h;

  int i = 0;

  switch (space) {
  case OPENSLIDE_JP2K_YCBCR:
    for (int y = 0; y < h; y++) {<commit_msg>jp2k: Use precomputed tables in YCbCr-RGB conversion<commit_after>/*
 *  OpenSlide, a library for reading whole slide image files
 *
 *  Copyright (c) 2007-2013 Carnegie Mellon University
 *  Copyright (c) 2011 Google, Inc.
 *  All rights reserved.
 *
 *  OpenSlide is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, version 2.1.
 *
 *  OpenSlide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with OpenSlide. If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "openslide-private.h"

#include <math.h>
#include <openjpeg.h>

static GOnce ycbcr_tables_inited = G_ONCE_INIT;
static int16_t R_Cr[256];
static int16_t G_CbCr[256][256];
static int16_t B_Cb[256];

static void *init_ycbcr_tables(void *arg G_GNUC_UNUSED) {
  for (int i = 0; i < 256; i++) {
    R_Cr[i] = round(1.402 * (i - 128));
    B_Cb[i] = round(1.772 * (i - 128));
    for (int j = 0; j < 256; j++) {
      G_CbCr[i][j] = round(-0.34414 * (i - 128) - 0.71414 * (j - 128));
    }
  }
  return NULL;
}

static void write_pixel_ycbcr(uint32_t *dest,
                              uint8_t c0, uint8_t c1, uint8_t c2) {
  int16_t R = c0 + R_Cr[c2];
  int16_t G = c0 + G_CbCr[c1][c2];
  int16_t B = c0 + B_Cb[c1];

  R = CLAMP(R, 0, 255);
  G = CLAMP(G, 0, 255);
  B = CLAMP(B, 0, 255);

  *dest = 255 << 24 | ((uint8_t) R << 16) | ((uint8_t) G << 8) | ((uint8_t) B);
}

static void write_pixel_rgb(uint32_t *dest,
                            uint8_t c0, uint8_t c1, uint8_t c2) {
  *dest = 255 << 24 | c0 << 16 | c1 << 8 | c2;
}

static void unpack_argb(enum _openslide_jp2k_colorspace space,
                        opj_image_comp_t *comps,
                        uint32_t *dest,
                        int32_t w, int32_t h) {
  // TODO: too slow, and with duplicated code!

  int c0_sub_x = w / comps[0].w;
  int c1_sub_x = w / comps[1].w;
  int c2_sub_x = w / comps[2].w;
  int c0_sub_y = h / comps[0].h;
  int c1_sub_y = h / comps[1].h;
  int c2_sub_y = h / comps[2].h;

  int i = 0;

  switch (space) {
  case OPENSLIDE_JP2K_YCBCR:
    g_once(&ycbcr_tables_inited, init_ycbcr_tables, NULL);
    for (int y = 0; y < h; y++) {
{'fraction_non_alphanumeric': 0.1207085064509075, 'fraction_numerical': 0.061884977039142794, 'mean_word_length': 2.587450980392157, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105527', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1736, 'n_words': 551}