/*
 * Photoyam — CMYK JPEG reader/writer for the WebAssembly build
 * (public/source/lcms2/build.sh), on top of the IJG JPEG library.
 * Browsers decode CMYK JPEGs without exposing the ink values and no browser
 * encoder writes them, so this tiny module does both:
 *   jc_decode      any JPEG → raw samples (CMYK files stay CMYK, values as
 *                  stored: Adobe files keep their inverted convention)
 *   jc_encode_cmyk 4-channel samples → baseline CMYK JPEG with an Adobe APP14
 *                  marker (transform 0, no chroma subsampling)
 * "This software is based in part on the work of the Independent JPEG Group."
 * This file: Copyright (c) Photoyam, MIT license.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <setjmp.h>
#include "jpeglib.h"

#define EXPORT __attribute__((used, visibility("default")))

struct jc_err { struct jpeg_error_mgr pub; jmp_buf jb; };
static void on_error(j_common_ptr c) { longjmp(((struct jc_err*)c->err)->jb, 1); }
static void on_message(j_common_ptr c) { (void)c; }

/* info: [0]=width [1]=height [2]=components out [3]=saw Adobe marker [4]=Adobe transform
 * [5]=jpeg colour space. Returns a malloc'ed buffer (free with jc_free) or NULL. */
EXPORT uint8_t* jc_decode(const uint8_t* buf, uint32_t len, int32_t* info) {
  struct jpeg_decompress_struct ci;
  struct jc_err err;
  uint8_t* volatile out = NULL;
  ci.err = jpeg_std_error(&err.pub);
  err.pub.error_exit = on_error;
  err.pub.output_message = on_message;
  if (setjmp(err.jb)) {
    jpeg_destroy_decompress(&ci);
    free(out);
    return NULL;
  }
  jpeg_create_decompress(&ci);
  jpeg_mem_src(&ci, (unsigned char*)buf, len);
  jpeg_read_header(&ci, TRUE);
  if (ci.num_components == 4) ci.out_color_space = JCS_CMYK;
  else if (ci.num_components == 1) ci.out_color_space = JCS_GRAYSCALE;
  else ci.out_color_space = JCS_RGB;
  jpeg_start_decompress(&ci);
  size_t stride = (size_t)ci.output_width * ci.output_components;
  out = (uint8_t*)malloc(stride * ci.output_height);
  if (!out) { jpeg_destroy_decompress(&ci); return NULL; }
  while (ci.output_scanline < ci.output_height) {
    JSAMPROW row = out + stride * ci.output_scanline;
    jpeg_read_scanlines(&ci, &row, 1);
  }
  info[0] = (int32_t)ci.output_width;
  info[1] = (int32_t)ci.output_height;
  info[2] = ci.output_components;
  info[3] = ci.saw_Adobe_marker;
  info[4] = ci.Adobe_transform;
  info[5] = ci.jpeg_color_space;
  jpeg_finish_decompress(&ci);
  jpeg_destroy_decompress(&ci);
  return out;
}

/* Encode width*height*4 CMYK samples (as they should be stored — pass inverted
 * ink for the Adobe convention). outLen receives the size. */
EXPORT uint8_t* jc_encode_cmyk(const uint8_t* px, int width, int height, int quality, uint32_t* outLen) {
  struct jpeg_compress_struct ci;
  struct jc_err err;
  unsigned char* volatile mem = NULL;
  unsigned long size = 0;
  ci.err = jpeg_std_error(&err.pub);
  err.pub.error_exit = on_error;
  err.pub.output_message = on_message;
  if (setjmp(err.jb)) {
    jpeg_destroy_compress(&ci);
    free(mem);
    return NULL;
  }
  jpeg_create_compress(&ci);
  jpeg_mem_dest(&ci, (unsigned char**)&mem, &size);
  ci.image_width = (JDIMENSION)width;
  ci.image_height = (JDIMENSION)height;
  ci.input_components = 4;
  ci.in_color_space = JCS_CMYK;
  jpeg_set_defaults(&ci);
  jpeg_set_colorspace(&ci, JCS_CMYK);
  jpeg_set_quality(&ci, quality, TRUE);
  ci.optimize_coding = TRUE;
  ci.write_Adobe_marker = TRUE;
  jpeg_start_compress(&ci, TRUE);
  size_t stride = (size_t)width * 4;
  while (ci.next_scanline < ci.image_height) {
    JSAMPROW row = (JSAMPROW)(px + stride * ci.next_scanline);
    jpeg_write_scanlines(&ci, &row, 1);
  }
  jpeg_finish_compress(&ci);
  jpeg_destroy_compress(&ci);
  *outLen = (uint32_t)size;
  return (uint8_t*)mem;
}

EXPORT void jc_free(void* p) { free(p); }
