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

Last change on this file since 57 was 56, checked in by Sam Hocevar, 14 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 3.2 KB
Line 
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
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
28struct GifScreen {
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;
36} GifScreen;
37
38struct gif_screen {
39  uint16_t w,h;
40  uint8_t color_info,background,reserved;
41} gif_screen;
42
43struct gif_image {
44  uint16_t xoff,yoff,w,h;
45  uint8_t color_info;
46} gif_image;
47
48image *read_gif(char *fn, palette *&pal)
49{
50  char buf[100],er;
51  uint8_t sep;
52  unsigned int ncolors;
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      {
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)
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            { 
83              if (fread((uint8_t *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
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!=',');
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);
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);
105                CHECK(ncolors<=256);
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              }
112
113              if (!er)
114              {
115                make_block(sizeof(image));
116                im=new image(gif_image.w+1,gif_image.h);
117                decode_gif_data(im,fp);
118                fclose(fp);
119              }
120
121            } else er=imREAD_ERROR;
122          }
123
124        } else er=imREAD_ERROR;
125      } else er=imINCORRECT_FILETYPE;
126    } else er=imREAD_ERROR;
127    fclose(fp);
128  }
129  set_error(er);
130  return im;
131}
Note: See TracBrowser for help on using the repository browser.