<commit_before>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "jpeg_internal.h"
#include "jpeg.h"

#include <liboil/liboil.h>
#include <liboil/liboildebug.h>

#define CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))
#define oil_argb(a,r,g,b) \
  ((oil_clamp_255(a)<<24) | \
   (oil_clamp_255(r)<<16) | \
   (oil_clamp_255(g)<<8) | \
   (oil_clamp_255(b)<<0))
#define oil_max(x,y) ((x)>(y)?(x):(y))
#define oil_min(x,y) ((x)<(y)?(x):(y))
#define oil_clamp_255(x) oil_max(0,oil_min((x),255))


static int16_t jfif_matrix[24] = {
  0,      0,      -8192,   -8192,
  16384,  0,      0,       0,
  0,      16384,  16384,   16384,
  0,      0,      -5638,   29032,
  0,      22970,  -11700,  0,
  0, 0, 0, 0
};


unsigned char * get_argb_444 (JpegDecoder *dec);
unsigned char * get_argb_422 (JpegDecoder *dec);
unsigned char * get_argb_422v (JpegDecoder *dec);
unsigned char * get_argb_420 (JpegDecoder *dec);

#if 0
static void imagescale2h_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2v_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2h2v_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void scanlinescale2_u8 (unsigned char *dest, unsigned char *src,
    int len);
#endif


int jpeg_decode_argb (uint8_t *data, int length, uint32_t **image,
    int *width, int *height)
{
  JpegDecoder *dec;
  int ret;

  dec = jpeg_decoder_new();

  jpeg_decoder_addbits (dec, data, length);
  ret = jpeg_decoder_decode(dec);

  if (!ret) return FALSE;

  jpeg_decoder_get_image_size (dec, width, height);
  *image = (uint32_t *)jpeg_decoder_get_argb_image (dec);

  return TRUE;
}

unsigned char *
jpeg_decoder_get_argb_image (JpegDecoder *dec)
{

  if (dec->n_components == 3) {
    if (dec->components[0].h_subsample == 1 &&
        dec->components[0].v_subsample == 1 &&
        dec->components[1].h_subsample == dec->components[2].h_subsample &&
        dec->components[1].v_subsample == dec->components[2].v_subsample) {<commit_msg>fix memleak<commit_after>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "jpeg_internal.h"
#include "jpeg.h"

#include <liboil/liboil.h>
#include <liboil/liboildebug.h>

#define CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))
#define oil_argb(a,r,g,b) \
  ((oil_clamp_255(a)<<24) | \
   (oil_clamp_255(r)<<16) | \
   (oil_clamp_255(g)<<8) | \
   (oil_clamp_255(b)<<0))
#define oil_max(x,y) ((x)>(y)?(x):(y))
#define oil_min(x,y) ((x)<(y)?(x):(y))
#define oil_clamp_255(x) oil_max(0,oil_min((x),255))


static int16_t jfif_matrix[24] = {
  0,      0,      -8192,   -8192,
  16384,  0,      0,       0,
  0,      16384,  16384,   16384,
  0,      0,      -5638,   29032,
  0,      22970,  -11700,  0,
  0, 0, 0, 0
};


unsigned char * get_argb_444 (JpegDecoder *dec);
unsigned char * get_argb_422 (JpegDecoder *dec);
unsigned char * get_argb_422v (JpegDecoder *dec);
unsigned char * get_argb_420 (JpegDecoder *dec);

#if 0
static void imagescale2h_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2v_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2h2v_u8 (unsigned char *dest, int d_rowstride,
    unsigned char *src, int src_rowstride, int width, int height);
static void scanlinescale2_u8 (unsigned char *dest, unsigned char *src,
    int len);
#endif


int jpeg_decode_argb (uint8_t *data, int length, uint32_t **image,
    int *width, int *height)
{
  JpegDecoder *dec;
  int ret;

  dec = jpeg_decoder_new();

  jpeg_decoder_addbits (dec, data, length);
  ret = jpeg_decoder_decode(dec);

  if (!ret) return FALSE;

  jpeg_decoder_get_image_size (dec, width, height);
  *image = (uint32_t *)jpeg_decoder_get_argb_image (dec);
  jpeg_decoder_free (dec);

  return TRUE;
}

unsigned char *
jpeg_decoder_get_argb_image (JpegDecoder *dec)
{

  if (dec->n_components == 3) {
    if (dec->components[0].h_subsample == 1 &&
        dec->components[0].v_subsample == 1 &&
        dec->components[1].h_subsample == dec->components[2].h_subsample &&
        dec->components[1].v_subsample == dec->components[2].v_subsample) {
{'fraction_non_alphanumeric': 0.15715260404821468, 'fraction_numerical': 0.057311803502387994, 'mean_word_length': 4.113953488372093, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '343963', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1763, 'n_words': 411}