source: abuse/trunk/src/imlib/gifread.cpp @ 128

Last change on this file since 128 was 128, checked in by Sam Hocevar, 15 years ago
  • Minor refactoring in imlib.
File size: 3.4 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <math.h>
13#include <string.h>
14#include <stdio.h>
15
[2]16#include "gifread.hpp"
17#include "palette.hpp"
18#include "image.hpp"
19#include "video.hpp"
20#include "linked.hpp"
21#include "gifdecod.hpp"
22#include "system.h"
23#include "dos.h"
24#include "dir.h"
25#include "macs.hpp"
26
27
[40]28struct GifScreen {
[124]29    uint16_t    Width;
30    uint16_t    Height;
31    uint8_t     ColorMap[3][256];
32    uint16_t    BitPixel;
33    uint16_t    ColorResolution;
34    uint16_t    Background;
35    uint16_t    AspectRatio;
[2]36} GifScreen;
37
[40]38struct gif_screen {
[17]39  uint16_t w,h;
40  uint8_t color_info,background,reserved;
[2]41} gif_screen;
42
[40]43struct gif_image {
[17]44  uint16_t xoff,yoff,w,h;
45  uint8_t color_info;
[2]46} gif_image;
47
48image *read_gif(char *fn, palette *&pal)
49{
50  char buf[100],er;
[17]51  uint8_t sep;
[4]52  unsigned int ncolors;
[2]53  FILE *fp;
54  image *im;
55  clear_errors();
56  fp=fopen(fn,"rb");
57  er=0;
58  im=NULL;
59  if (fp==NULL) er=imFILE_NOT_FOUND;
60  else
61  {
62    if (fread(buf,1,6,fp)==6)
63    {
64      buf[6]=0;
65      if (!strcmp("GIF87a",buf))
66      {
[17]67        fread((uint8_t *)&gif_screen.w,2,1,fp);
68        gif_screen.w=uint16_to_local(gif_screen.w);
69        fread((uint8_t *)&gif_screen.h,2,1,fp);
70        gif_screen.h=uint16_to_local(gif_screen.h);
71        fread((uint8_t *)&gif_screen.color_info,1,1,fp);
72        fread((uint8_t *)&gif_screen.background,1,1,fp);
73        if (fread((uint8_t *)&gif_screen.reserved,1,1,fp)==1)
[124]74    {
75      if (gif_screen.color_info&128)
76      {
77        ncolors=2<<(gif_screen.color_info&0x0f);
78        make_block(sizeof(palette));
79//        pal=new palette(ncolors);
80        pal=new palette(256);
81        if (pal)
82        {
[17]83              if (fread((uint8_t *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
[124]84        } else er=imMEMORY_ERROR;
85      }
86      if (!er)
87      { do
88        {
89          if (fread((uint8_t *)&sep,1,1,fp)!=1)
90        er=imREAD_ERROR;
91        } while (!er && sep!=',');
[17]92            fread((uint8_t *)&gif_image.xoff,2,1,fp);
93            gif_image.xoff=uint16_to_local(gif_image.xoff);
94            fread((uint8_t *)&gif_image.yoff,2,1,fp);
95            gif_image.yoff=uint16_to_local(gif_image.yoff);
96            fread((uint8_t *)&gif_image.w,2,1,fp);
97            gif_image.w=uint16_to_local(gif_image.w);
98            fread((uint8_t *)&gif_image.h,2,1,fp);
99            gif_image.h=uint16_to_local(gif_image.h);
[124]100        if (!er && (fread((uint8_t *)&gif_image.color_info,1,1,fp)==1))
101        {
102          if (gif_image.color_info&128)
103          {
104        ncolors=2<<(gif_image.color_info&0x0f);
[2]105                CHECK(ncolors<=256);
[124]106        make_block(sizeof(palette));
107        pal = new palette(ncolors);
108        if (pal)
109        { if (fread((uint8_t *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
110        } else er=imMEMORY_ERROR;
111          }
[2]112
[124]113          if (!er)
114          {
115        make_block(sizeof(image));
116        im=new image(gif_image.w+1,gif_image.h);
[128]117        im->lock();
[124]118        decode_gif_data(im,fp);
[128]119        im->unlock();
[124]120        fclose(fp);
121          }
[2]122
[124]123        } else er=imREAD_ERROR;
124      }
[2]125
[124]126    } else er=imREAD_ERROR;
[2]127      } else er=imINCORRECT_FILETYPE;
128    } else er=imREAD_ERROR;
129    fclose(fp);
130  }
131  set_error(er);
132  return im;
133}
Note: See TracBrowser for help on using the repository browser.