home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / bmpheader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-25  |  2.4 KB  |  103 lines

  1. #include <stdio.h>
  2. #include <libc.h>
  3.  
  4. #include "bmp.h"
  5.  
  6. bmpHeader *loadBmpHeader(FILE *fp, int *errcode)
  7.      /* ¥à¥¡¥⁄¥º⁄«⁄إ㥈¥¹¬ö˚ú⁄ù˘²⁄ì¡£
  8.     ¥¤¥Ø¡…⁄‹¦fl⁄›⁄¿¬ò„ñ⁄ˇ NULL⁄‹˚á⁄Œ¡¢errcode⁄¸⁄‰⁄˛˝ÿ˝‡⁄‹˘⁄º¡£
  9.     ¥à¥¡¥⁄¥º¥é¥⁄¥ú¥¿⁄ˇ¥±¥ò¥ˆ¥¨¬ö˚ú⁄˛¹Ł˘‹⁄ù»ã⁄•⁄˘˚á⁄º¡£ */
  10. {
  11.     long    ftype, boffs, wid, hei, xp, yp, planes, colors;
  12.     long    cmp=NoComp;
  13.     short    bits = 0;
  14.     bmpHeader *bh;
  15.  
  16.     wid = hei = planes = bits = xp = yp = colors = 0;
  17.     *errcode = 0;
  18.     if (getc(fp) != 'B' || getc(fp) != 'M') {
  19.         *errcode = Err_FORMAT;
  20.         return NULL;
  21.     }
  22.     (void) fseek(fp, 8L, SEEK_CUR); /* Skip hot spot */
  23.     boffs = get_long(fp);
  24.     ftype = get_long(fp);
  25.     if (ftype == OS2) {
  26.         wid = get_short(fp);
  27.         hei = get_short(fp);
  28.         planes = get_short(fp);
  29.         bits = get_short(fp);
  30.     }else if (ftype == WIN3) {
  31.         wid = get_long(fp);
  32.         hei = get_long(fp);
  33.         planes = get_short(fp);
  34.         bits = get_short(fp);
  35.         cmp = get_long(fp);
  36.         (void) get_long(fp);
  37.         xp = get_long(fp);
  38.         yp = get_long(fp);
  39.         colors = get_long(fp);
  40.         (void) get_long(fp);
  41.     }else { /* ERROR */
  42.         *errcode = Err_FORMAT;
  43.         return NULL;
  44.     }
  45.     if(feof(fp)) {
  46.         *errcode = Err_SHORT;
  47.         return NULL;
  48.     }
  49.     if (xp < 0 || yp < 0) {
  50.         *errcode = Err_FORMAT;
  51.         return NULL;
  52.     }
  53.     if (planes != 1 || wid > MAXWidth
  54.         || (bits != 1 && bits != 4 && bits != 8 && bits != 24)) {
  55.         *errcode = Err_IMPLEMENT;
  56.         return NULL;
  57.     }
  58.     bh = (bmpHeader *)malloc(sizeof(bmpHeader));
  59.     bh->type = ftype;
  60.     bh->bits = bits;
  61.     bh->x = wid;
  62.     bh->y = hei;
  63.     bh->xpm = xp;
  64.     bh->ypm = yp;
  65.     bh->bitoffset = boffs;
  66.     bh->comp = cmp;
  67.     return bh;
  68. }
  69.  
  70. void freeBmpHeader(bmpHeader *bh)
  71. {
  72.     if (bh) {
  73.         if (bh->palette) free((void *)bh->palette);
  74.         free((void *)bh);
  75.     }
  76. }
  77.  
  78. commonInfo *bmpInfo(bmpHeader *bh, int bits, BOOL mono)
  79.      /* Convert bmp-header into commonInfo */
  80. {
  81.     commonInfo *cinf;
  82.  
  83.     if (bh == NULL) return NULL;
  84.     cinf = (commonInfo *)malloc(sizeof(commonInfo));
  85.     cinf->width  = bh->x;
  86.     cinf->height = bh->y;
  87.     cinf->type   = Type_bmp;
  88.     cinf->bits   = bits;
  89.     cinf->alpha  = NO;
  90.     cinf->isplanar = YES;
  91.     cinf->numcolors = mono ? 1 : 3;
  92.     cinf->cspace = mono ? NX_OneIsWhiteColorSpace : NX_RGBColorSpace;
  93.     cinf->xbytes = byte_length(bits, bh->x);
  94.     cinf->palette = bh->palette;
  95.     bh->palette = NULL;
  96.     cinf->palsteps = 1 << bh->bits;
  97.     sprintf(cinf->memo, "%d x %d  %dbit%s %s %s%s",
  98.         bh->x, bh->y, bits, ((bits > 1) ? "s" : ""),
  99.         ((bh->bits > 8) ? "FullColor" : "Palette"),
  100.         (bh->type == OS2)?"OS2":"WIN3", (bh->comp ? "(RLE)":""));
  101.     return cinf;
  102. }
  103.