
//
// Modifications History
//
//$LB (10/11/2004) : i have entirely recoded the JComp and JDecomp functions from the JPEG load ans save source code,
//                   to match with the 24bits pixels process, to improve performance,
//                   and to get a source code that doesn't give the impression to come from the outter space... :)
//
//



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>

#include "x/Version.h"
#include "x/scolplugin.h"

#include "objstr.h"

#include "objects/bitmap.h"

#include "jpeglib.h"
#include <setjmp.h>


#include "mjpeg.h"



METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  my_error_ptr myerr = (my_error_ptr) cinfo->err;

  longjmp(myerr->setjmp_buffer, 1);
}




int quality = 75;


int JCompInit (mmachine m)
{
	quality = MMget (m, 0) >>1;
	if (quality < 1) quality = 1;
	if (quality > 99) quality = 99;

	return 0;
}



//
//fun  [S I I] S
//
int JComp (mmachine m)
{
  struct jpeg_compress_struct cinfo;
  struct my_error_mgr jerr;
  FILE * outfile; /* source file */
  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
  int row_stride; /* physical row width in output buffer */
  OBJBITMAP_BUFFER dst;
  int x,y,bpl,k,size;
  char *buf;
  PtrObjBitmap B;
  char* result;


/****************************/
 #ifdef DEBUG_LIB2DOS
   MMechostr (0 , "\nJComp");
#endif
/****************************/


  buf=NULL;



  //$BLG: v4.6a5 - Modif (No - suspected some buffer overflow and try to force Binary mode - Pb still here)
  if ((outfile = tmpfile()) == NULL)
  //if ((outfile = fopen("c:/jcomp.dat","w+b")) == NULL)
  {
	  MMechostr(MSKDEBUG,"jpeg :tmpfile was not created\n");
	  MMpull(m); MMpull(m); MMpull(m);
      return MMpush(m,NIL);
  }



  B = (PtrObjBitmap) malloc (sizeof(struct ObjBitmap));
  B->TailleH = MMpull(m)>>1;
  B->TailleW = MMpull(m)>>1;
  B->BPP = 24;
  B->BytesPP = B->BPP>>3;
  B->handler = ObjBitmap_New ( B , NULL ) ;
  free (B->bits);
  B->bits = MMstartstr (m, MMpull(m)>>1);
        
  dst = (OBJBITMAP_BUFFER)B->bits;
  x = B->TailleW; y = B->TailleH;
  bpl = B->BPL;
  ////////////////////////////////////////


  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;

  if (setjmp(jerr.setjmp_buffer))
  {
	MMechostr(MSKDEBUG,"jpeg : error\n");
	MMechostr(MSKDEBUG,(char*)jerr.pub.jpeg_message_table[jerr.pub.msg_code]
		,jerr.pub.msg_parm.i[0],jerr.pub.msg_parm.i[1]);

    jpeg_destroy_compress(&cinfo);
    fclose(outfile);
	if (buf) free(buf);
    MMpull(m);
    return MMpush(m,NIL);
  }

  jpeg_create_compress(&cinfo);

  //
  result = (char*) malloc (bpl * y);
  //

  jpeg_stdio_dest(&cinfo, outfile);

  cinfo.image_width = x; 	/* image width and height, in pixels */
  cinfo.image_height = y;
  cinfo.input_components = 3;		/* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

  jpeg_start_compress(&cinfo, TRUE);
  row_stride = x * 3;	/* JSAMPLEs per row in image_buffer */

  buf=(char*)malloc(row_stride);
  if (buf)
  {
	  while (cinfo.next_scanline < cinfo.image_height)
		{
			convertWrite24to24(buf,dst,x);

			row_pointer[0] = buf;
			jpeg_write_scanlines(&cinfo, row_pointer, 1);
			dst+=(bpl); 
		}
	  jpeg_finish_compress(&cinfo);

	  free(buf);
  }
  else MMset(m,0,-1*2);
 
  jpeg_destroy_compress(&cinfo);

  size = ftell(outfile);  
MMechostr(0,"JCOMP: %d\n",size); // Sometimes false 
MMechostr(0,"JCOMP: %d\n",bpl);  // OK
MMechostr(0,"JCOMP: %d\n",x);
MMechostr(0,"JCOMP: %d\n",y);

  fseek (outfile, 0, SEEK_SET);

  fread (result, sizeof(char), size, outfile);
  rmtmp ();

  k = Mpushstrblocn (m, result, size);

  

  free (B);
  
  free (result);

 return k;
}









