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

#include "Bitmap.h"
#include "Espace3UC.h"
#include "JPEGMain.h"
#include "Jpeg/jpeglib.h"

/* *********************************************************************** */
/*                 Module de gestion du dessin JPEG Bitmap                 */
/* *********************************************************************** */

int lectureJPEG(char *stst,struct espace *bt) { 
  unsigned i;
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE *input_file = NULL;
  if (stst != NULL) {
    if ((input_file = fopen(stst,"rb")) == NULL) {
      return(0); } }
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo,input_file);
  jpeg_read_header(&cinfo,TRUE);
  jpeg_start_decompress(&cinfo);
  new_espace(bt,cinfo.output_width,cinfo.output_height,24);
  i = 0;
  while (i < cinfo.output_height) {
    (void) jpeg_read_scanlines(&cinfo,&bt->p[cinfo.output_height-i-1],1);
    i++; }
  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(input_file);
  return(1);
}

Espace3UC *lectureJPEG(char *stst) {
  Espace3UC *euc; 
  unsigned i;
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE *input_file = NULL;
  if (stst != NULL) {
    if ((input_file = fopen(stst,"rb")) == NULL) {
      return(0); } }
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo,input_file);
  jpeg_read_header(&cinfo,TRUE);
  jpeg_start_decompress(&cinfo);
  euc = new Espace3UC(cinfo.output_width,cinfo.output_height);
  i = 0;
  while (i < cinfo.output_height) {
    (void) jpeg_read_scanlines(&cinfo,&euc->p[cinfo.output_height-i-1],1);
    i++; }
  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(input_file);
  return(euc);
}

int sauvegardeJPEG(char *stst,struct espace *e) {
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE *output_file = NULL;
  int i;
  if (stst != NULL) {
    if ((output_file = fopen(stst,"wb")) == NULL) {
      return(0); } }
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo,output_file);
  cinfo.image_width = e->dx; 
  cinfo.image_height = e->dy; 
  cinfo.input_components = 3; 
  cinfo.in_color_space = JCS_RGB; 
  jpeg_set_defaults(&cinfo);
  jpeg_start_compress(&cinfo,TRUE);
  i = 0;
  while (cinfo.next_scanline < cinfo.image_height) {
    (void) jpeg_write_scanlines(&cinfo,&e->p[cinfo.image_height-1-i],1);
    i++; }
  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  fclose(output_file);
  return(1);
}

int sauvegardeJPEG(char *stst,Espace3UC *euc) {
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE *output_file = NULL;
  int i;
  if (stst != NULL) {
    if ((output_file = fopen(stst,"wb")) == NULL) {
      return(0); } }
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo,output_file);
  cinfo.image_width = euc->dx; 
  cinfo.image_height = euc->dy; 
  cinfo.input_components = 3; 
  cinfo.in_color_space = JCS_RGB; 
  jpeg_set_defaults(&cinfo);
  jpeg_start_compress(&cinfo,TRUE);
  i = 0;
  while (cinfo.next_scanline < cinfo.image_height) {
    (void) jpeg_write_scanlines(&cinfo,&euc->p[cinfo.image_height-1-i],1);
    i++; }
  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  fclose(output_file);
  return(1);
}

/* *********************************************************************** */
