Back to index

plt-scheme  4.2.1
Defines | Typedefs | Functions | Variables
wx_bmp.cc File Reference
#include <stdlib.h>
#include "wx_image.h"

Go to the source code of this file.

Defines

#define BI_RGB   0
#define BI_RLE8   1
#define BI_RLE4   2

Typedefs

typedef unsigned char uchar

Functions

static int loadBMP1 (FILE *, byte *, int, int)
static int loadBMP4 (FILE *, byte *, int, int, int)
static int loadBMP8 (FILE *, byte *, int, int, int)
static int loadBMP24 (FILE *, byte *, int, int)
static unsigned int getshort (FILE *)
static unsigned int getint (FILE *)
static void putshort (FILE *, int)
static void putint (FILE *, int)
static void writeBMP1 (FILE *, byte *, int, int)
static void writeBMP4 (FILE *, byte *, int, int)
static void writeBMP8 (FILE *, byte *, int, int)
static void writeBMP24 (FILE *, byte *, int, int)
static int bmpError (char *, char *)

Variables

static long filesize
static byte pc2nc [256]
static byte r1 [256]
static byte g1 [256]
static byte b1 [256]

Define Documentation

#define BI_RGB   0

Definition at line 78 of file wx_bmp.cc.

#define BI_RLE4   2

Definition at line 80 of file wx_bmp.cc.

#define BI_RLE8   1

Definition at line 79 of file wx_bmp.cc.


Typedef Documentation

typedef unsigned char uchar

Definition at line 472 of file wx_bmp.cc.


Function Documentation

static int bmpError ( char *  fname,
char *  st 
) [static]

Definition at line 805 of file wx_bmp.cc.

{
  fprintf(stderr, "wxImage: %s: %s\n", fname, st);
  return 0;
}
static unsigned int getint ( FILE *  fp) [static]

Definition at line 520 of file wx_bmp.cc.

{
  int c, c1, c2, c3;
  c = getc(fp);  c1 = getc(fp);  c2 = getc(fp);  c3 = getc(fp);
  return ((unsigned int) c) +
         (((unsigned int) c1) << 8) + 
        (((unsigned int) c2) << 16) +
        (((unsigned int) c3) << 24);
}
static unsigned int getshort ( FILE *  fp) [static]

Definition at line 511 of file wx_bmp.cc.

{
  int c, c1;
  c = getc(fp);  c1 = getc(fp);
  return ((unsigned int) c) + (((unsigned int) c1) << 8);
}
static int loadBMP1 ( FILE *  fp,
byte pic8,
int  w,
int  h 
) [static]

Definition at line 291 of file wx_bmp.cc.

{
  int   i,j,c,bitnum,padw;
  byte *pp;

  c = 0;
  padw = ((w + 31)/32) * 32;  /* 'w', padded to be a multiple of 32 */

  for (i=h-1; i>=0; i--) {
    pp = pic8 + (i * w);
    for (j=bitnum=0; j<padw; j++,bitnum++) {
      if ((bitnum&7) == 0) { /* read the next byte */
       c = getc(fp);
       bitnum = 0;
      }
      
      if (j<w) {
       *pp++ = (c & 0x80) ? 1 : 0;
       c <<= 1;
      }
    }
    if (ferror(fp)) break;
  }

  return (ferror(fp));
}  
static int loadBMP24 ( FILE *  fp,
byte pic24,
int  w,
int  h 
) [static]

Definition at line 475 of file wx_bmp.cc.

{
  int   i,j,padb,k;
  byte *pp;
  uchar *line;

  padb = (4 - ((w*3) % 4)) & 0x03;  /* # of pad bytes to read at EOscanline */

  line = new WXGC_ATOMIC uchar[(w * 3) + padb];

  for (i=h-1; i>=0; i--) {
    pp = pic24 + (i * w * 3);

    fread(line, (w * 3) + padb, 1, fp);
    k = 0;
    
    for (j=0; j<w; j++) {
      int rc, grc, bc;
      /* BMP data is ordered backward: BGR */
      bc = line[k++];
      grc = line[k++];
      rc = line[k++];
      *pp++ = rc;
      *pp++ = grc;
      *pp++ = bc;
    }

    if (ferror(fp)) break;
  }

  return (ferror(fp));
}  
static int loadBMP4 ( FILE *  fp,
byte pic8,
int  w,
int  h,
int  comp 
) [static]

Definition at line 321 of file wx_bmp.cc.

{
  int   i,j,c,c1,x,y,nybnum,padw,rv;
  byte *pp;


  rv = 0;
  c = c1 = 0;

  if (comp == BI_RGB) {   /* read uncompressed data */
    padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */

    for (i=h-1; i>=0; i--) {
      pp = pic8 + (i * w);
      for (j=nybnum=0; j<padw; j++,nybnum++) {
       if ((nybnum & 1) == 0) { /* read next byte */
         c = getc(fp);
         nybnum = 0;
       }

       if (j<w) {
         *pp++ = (c & 0xf0) >> 4;
         c <<= 4;
       }
      }
      if (ferror(fp)) break;
    }
  }

  else if (comp == BI_RLE4) {  /* read RLE4 compressed data */
    x = y = 0;  
    pp = pic8 + x + (h-y-1)*w;

    while (y<h) {
      c = getc(fp);  if (c == EOF) { rv = 1;  break; }

      if (c) {                                   /* encoded mode */
       c1 = getc(fp);
       for (i=0; i<c; i++,x++,pp++) {
         *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
       }
      }

      else {    /* c==0x00  :  escape codes */
       c = getc(fp);  if (c == EOF) { rv = 1;  break; }

       if      (c == 0x00) {                    /* end of line */
         x=0;  y++;  pp = pic8 + x + (h-y-1)*w;
       } 

       else if (c == 0x01) break;               /* end of pic8 */

       else if (c == 0x02) {                    /* delta */
         c = getc(fp);  x += c;
         c = getc(fp);  y += c;
         pp = pic8 + x + (h-y-1)*w;
       }

       else {                                   /* absolute mode */
         for (i=0; i<c; i++, x++, pp++) {
           if ((i&1) == 0) c1 = getc(fp);
           *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
         }
         
         if (((c&3)==1) || ((c&3)==2)) getc(fp);  /* read pad byte */
       }
      }  /* escape processing */
      if (ferror(fp)) break;
    }  /* while */
  }
  
  else {
    fprintf(stderr,"unknown BMP compression type 0x%0x\n", comp);
  }

  if (ferror(fp)) rv = 1;
  return rv;
}  
static int loadBMP8 ( FILE *  fp,
byte pic8,
int  w,
int  h,
int  comp 
) [static]

Definition at line 403 of file wx_bmp.cc.

{
  int   i,j,c,c1,padw,x,y,rv;
  byte *pp;

  rv = 0;

  if (comp == BI_RGB) {   /* read uncompressed data */
    padw = ((w + 3)/4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */

    for (i=h-1; i>=0; i--) {
      pp = pic8 + (i * w);

      for (j=0; j<padw; j++) {
       c = getc(fp);  if (c==EOF) rv = 1;
       if (j<w) *pp++ = c;
      }
      if (ferror(fp)) break;
    }
  }

  else if (comp == BI_RLE8) {  /* read RLE8 compressed data */
    x = y = 0;  
    pp = pic8 + x + (h-y-1)*w;

    while (y<h) {
      c = getc(fp);  if (c == EOF) { rv = 1;  break; }

      if (c) {                                   /* encoded mode */
       c1 = getc(fp);
       for (i=0; i<c; i++,x++,pp++) { *pp = c1; }
      }

      else {    /* c==0x00  :  escape codes */
       c = getc(fp);  if (c == EOF) { rv = 1;  break; }

       if      (c == 0x00) {                    /* end of line */
         x=0;  y++;  pp = pic8 + x + (h-y-1)*w;
       } 

       else if (c == 0x01) break;               /* end of pic8 */

       else if (c == 0x02) {                    /* delta */
         c = getc(fp);  x += c;
         c = getc(fp);  y += c;
         pp = pic8 + x + (h-y-1)*w;
       }

       else {                                   /* absolute mode */
         for (i=0; i<c; i++, x++, pp++) {
           c1 = getc(fp);
           *pp = c1;
         }
         
         if (c & 1) getc(fp);  /* odd length run: read an extra pad byte */
       }
      }  /* escape processing */
      if (ferror(fp)) break;
    }  /* while */
  }
  
  else {
    fprintf(stderr,"unknown BMP compression type 0x%0x\n", comp);
  }

  if (ferror(fp)) rv = 1;
  return rv;
}  
static void putint ( FILE *  fp,
int  i 
) [static]

Definition at line 542 of file wx_bmp.cc.

{
  int c, c1, c2, c3;
  c  = ((unsigned int ) i)      & 0xff;  
  c1 = (((unsigned int) i)>>8)  & 0xff;
  c2 = (((unsigned int) i)>>16) & 0xff;
  c3 = (((unsigned int) i)>>24) & 0xff;

  putc(c, fp);   putc(c1,fp);  putc(c2,fp);  putc(c3,fp);
}
static void putshort ( FILE *  fp,
int  i 
) [static]

Definition at line 532 of file wx_bmp.cc.

{
  int c, c1;

  c = ((unsigned int ) i) & 0xff;  c1 = (((unsigned int) i)>>8) & 0xff;
  putc(c, fp);   putc(c1,fp);
}
static void writeBMP1 ( FILE *  fp,
byte pic8,
int  w,
int  h 
) [static]

Definition at line 704 of file wx_bmp.cc.

{
  int   i,j,c,bitnum,padw;
  byte *pp;

  padw = ((w + 31)/32) * 32;  /* 'w', padded to be a multiple of 32 */

  for (i=h-1; i>=0; i--) {
    pp = pic8 + (i * w);  

    for (j=bitnum=c=0; j<=padw; j++,bitnum++) {
      if (bitnum == 8) { /* write the next byte */
       putc(c,fp);
       bitnum = c = 0;
      }
      
      c <<= 1;

      if (j<w) {
       c |= (pc2nc[*pp++] & 0x01);
      }
    }
  }
}  
static void writeBMP24 ( FILE *  fp,
byte pic24,
int  w,
int  h 
) [static]

Definition at line 779 of file wx_bmp.cc.

{
  int   i,j,padb;
  byte *pp;

  padb = (4 - ((w*3) % 4)) & 0x03;  /* # of pad bytes to write at EOscanline */

  for (i=h-1; i>=0; i--) {
    pp = pic24 + (i * w * 3);

    for (j=0; j<w; j++) {
      putc(*pp++, fp);
      putc(*pp++, fp);
      putc(*pp++, fp);
    }

    for (j=0; j<padb; j++) { putc(0, fp); }
  }
}  
static void writeBMP4 ( FILE *  fp,
byte pic8,
int  w,
int  h 
) [static]

Definition at line 732 of file wx_bmp.cc.

{
  int   i,j,c,nybnum,padw;
  byte *pp;


  padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */

  for (i=h-1; i>=0; i--) {
    pp = pic8 + (i * w);

    for (j=nybnum=c=0; j<=padw; j++,nybnum++) {
      if (nybnum == 2) { /* write next byte */
       putc((c&0xff), fp);
       nybnum = c = 0;
      }

      c <<= 4;

      if (j<w) {
       c |= (pc2nc[*pp] & 0x0f);
       pp++;
      }
    }
  }
}  
static void writeBMP8 ( FILE *  fp,
byte pic8,
int  w,
int  h 
) [static]

Definition at line 762 of file wx_bmp.cc.

{
  int   i,j,padw;
  byte *pp;

  padw = ((w + 3)/4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */

  for (i=h-1; i>=0; i--) {
    pp = pic8 + (i * w);

    for (j=0; j<w; j++) { putc(pc2nc[*pp++], fp); }
    for ( ; j<padw; j++) { putc(0, fp); }
  }
}  

Variable Documentation

byte b1[256] [static]

Definition at line 556 of file wx_bmp.cc.

long filesize [static]

Definition at line 83 of file wx_bmp.cc.

byte g1[256] [static]

Definition at line 556 of file wx_bmp.cc.

byte pc2nc[256] [static]

Definition at line 556 of file wx_bmp.cc.

byte r1[256] [static]

Definition at line 556 of file wx_bmp.cc.