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

#include "PICTModeBtmp.h"
#include "Bitmap.h"
#include "PICTMain.h"
#include "PICTFct.h"
#include "Espace3UC.h"

static unsigned char    version[4] = { 0x00,0x11,0x02,0xFF } ;
static unsigned char    mess1[14]  = { 0x0C,0x00,0xFF,0xFE,0x00,0x00,0x00,0x48,
                                       0x00,0x00,0x00,0x48,0x00,0x00 } ;
static unsigned char    mess2[8]   = { 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x0A } ;

int     lecture_entier(FILE *num) ;
void    sauvegarde_PICT24(char *fichier,struct espace *e,int origine,int xi,int yi,int xf,int yf) ;

/* *********************************************************************** */
/*              Module de gestion du dessin PICT 8 bits Bitmap             */
/* *********************************************************************** */

int sauvegarde_entete(FILE *num)
  { int             i ;
    unsigned char   uc = 0x00 ;

    for ( i = 0 ; i < 512 ; i++ )
      fwrite(&uc,1,1,num) ;
    return(512) ;
}
/* ----------------------------------------------------------------------- */
int sauvegarde_entier(FILE *num,unsigned *entier)
  { fwrite(((char *) entier)+1,1,1,num) ;
    fwrite((char *) entier,1,1,num) ;
    return(2) ;
}
/* ----------------------------------------------------------------------- */
int lecture_entier(FILE *num)
  { int               res ;

    fread(((char *) (&res))+1,1,1,num) ;
    fread((char *) (&res),1,1,num) ;
    return(res) ;
}
/* ----------------------------------------------------------------------- */
int sauvegarde_rectangle(FILE *num,unsigned *xi,unsigned *yi,unsigned *xf,unsigned *yf)
  { sauvegarde_entier(num,yi) ;
    sauvegarde_entier(num,xi) ;
    sauvegarde_entier(num,yf) ;
    sauvegarde_entier(num,xf) ;
    return(8) ;
}
/* ----------------------------------------------------------------------- */
struct espace *lecture_image_PICT24(char *fichier)
  { FILE                    *num ;
    unsigned char           *c ;
    int                     dx,dy,y,dd,dep,x,xd,i ;
    struct espace           *bt ;
    struct c24bits          *p ;
    long                    dp ;

    num = fopen(fichier,"rb") ;
    fseek(num,0x00000206,SEEK_SET) ;
    dy = lecture_entier(num) ;
    dx = lecture_entier(num) ;
    bt =(struct espace *) calloc(1,sizeof(struct espace)) ;
    if ( new_espace(bt,dx,dy,24) ) {
      p =(struct c24bits *) calloc(dx+1,sizeof(struct c24bits)) ;
      dd = ((dx * 3 - 1)/128)+1+dx*3 ;
      c =(unsigned char *) calloc(dd,sizeof(unsigned char)) ;
      dep = (dx < 63 ) ? 1 : 2 ;
      for ( y = 0 ; y < dy ; y++ ) {
        dp =(long) 634 + dep +(long) y * (dd+dep) ;
        fseek(num,dp,SEEK_SET) ;
        fread(c,dd,1,num) ;
        for ( x = i = 0 ; x < dd ; x += 129,i++ ) {
          xd = ( x+129 < dd ) ? 128 : dd - x - 1 ;
          memmove(&c[x-i],&c[x+1],xd) ; }
        for ( i = 0 ; i < 3 ; i++ )
          for ( x = 0 ; x < dx ; x++ )
            ((unsigned char *) &p[x])[i] = c[i*dx+x] ;
        sauve_ligne(y,(unsigned char *) p,bt) ; }
      free(p) ;
      free(c) ; }
    fclose(num) ;
    return(bt) ;
}
/* ----------------------------------------------------------------------- */
/*struct espace *lecture_PICT24_compacte(char *stst)
  { btpict =(struct espace *) calloc(sizeof(struct espace)) ;
    resolution_image(stst,&dx_pict,&dy_pict) ;
    new_espace(btpict,dx_pict,dy_pict,24) ;
    initgraphmode(put_pixel_bitmap,get_pixel_bitmap,cherche_ligne_bitmap,sauve_ligne_bitmap,init_PICT_bitmap,fini_PICT_bitmap) ;
    affiche_image_PICT24(stst,0,0,32000,32000) ;
    return(btpict) ;
}*/
/* ----------------------------------------------------------------------- */
void lecture_PICT24_compacte(char *stst,struct espace *bt)
  { btpict = bt ;
    resolution_image(stst,&dx_pict,&dy_pict) ;
    new_espace(btpict,dx_pict,dy_pict,24) ;
    initgraphmode(put_pixel_bitmap,get_pixel_bitmap,cherche_ligne_bitmap,sauve_ligne_bitmap,init_PICT_bitmap,fini_PICT_bitmap) ;
    affiche_image_PICT24(stst,0,0,32000,32000) ;
}
/* ----------------------------------------------------------------------- */
Espace3UC *lecture_PICT24_compacte(char *stst)
  { struct espace bt;
    btpict = &bt ;
    resolution_image(stst,&dx_pict,&dy_pict) ;
    Espace3UC *euc = new Espace3UC(dx_pict,dy_pict) ;
    new_espace(btpict,dx_pict,dy_pict,24) ;
    initgraphmode(put_pixel_bitmap,get_pixel_bitmap,cherche_ligne_bitmap,sauve_ligne_bitmap,init_PICT_bitmap,fini_PICT_bitmap) ;
    affiche_image_PICT24(stst,0,0,32000,32000) ;
    for ( int i = 0 ; i < euc->dy ; i++ ) {
      cherche_ligne(i,euc->p[i],&bt); }
    liberation_espace(btpict);
    return(euc);
}
/* ----------------------------------------------------------------------- */
unsigned char test_fichier_PICT24(char *fichier)
  { FILE                    *num ;
    unsigned char           c ;
    num = fopen(fichier,"rb") ;
    fseek(num,0x00000235,SEEK_SET) ;
    fread(&c,1,1,num) ;
    fclose(num) ;
    return (c == 0x9A) ;
}
/* ----------------------------------------------------------------------- */
unsigned char test_fichier_PICT8(char *fichier)
  { FILE                    *num ;
    unsigned char           c ;

    num = fopen(fichier,"rb") ;
    fseek(num,0x00000235,SEEK_SET) ;
    fread(&c,1,1,num) ;
    fclose(num) ;
    return (c == 0x98) ;
}
/* ----------------------------------------------------------------------- */
void sauvegarde_PICT24_compacte(char *stst,struct espace *e)
  { btpict = e ;
    dx_pict = e->dx ;
    dy_pict = e->dy ;
    initgraphmode(put_pixel_bitmap,get_pixel_bitmap,cherche_ligne_bitmap,sauve_ligne_bitmap,init_PICT_bitmap,fini_PICT_bitmap) ;
    sauvegarde_image_PICT24_new(stst,0,0,e->dx-1,e->dy-1) ;
}
/* ----------------------------------------------------------------------- */
void sauvegarde_PICT24_compacte(char *stst,Espace3UC *euc)
  { struct espace e;
    new_espace(&e,euc->dx,euc->dy,24);
    for ( int i = 0 ; i < e.dy ; i++ ) {
      sauve_ligne(i,euc->p[i],&e); }
    btpict = &e;
    dx_pict = e.dx;
    dy_pict = e.dy;
    initgraphmode(put_pixel_bitmap,get_pixel_bitmap,cherche_ligne_bitmap,sauve_ligne_bitmap,init_PICT_bitmap,fini_PICT_bitmap) ;
    sauvegarde_image_PICT24_new(stst,0,0,e.dx-1,e.dy-1) ;
    liberation_espace(&e);
}

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