source: abuse/tags/pd/macabuse/imlib/gifread.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#include "gifread.hpp"
2#include "palette.hpp"
3#include "image.hpp"
4#include "video.hpp"
5#include "linked.hpp"
6#include "gifdecod.hpp"
7#include "system.h"
8#include "dos.h"
9#include <math.h>
10#include <string.h>
11#include <stdio.h>
12#include "dir.h"
13#include "macs.hpp"
14
15
16struct {
17        unsigned short int      Width;
18        unsigned short int      Height;
19        unsigned char   ColorMap[3][256];
20        unsigned short int      BitPixel;
21        unsigned short int      ColorResolution;
22        unsigned short int      Background;
23        unsigned short int      AspectRatio;
24} GifScreen;
25
26struct {
27  unsigned short int w,h;
28  unsigned char color_info,background,reserved;
29} gif_screen;
30
31struct {
32  unsigned short int xoff,yoff,w,h;
33  unsigned char color_info;
34} gif_image;
35
36image *read_gif(char *fn, palette *&pal)
37{
38  char buf[100],er;
39  unsigned char sep;
40  int ncolors;
41  FILE *fp;
42  image *im;
43  clear_errors();
44  fp=fopen(fn,"rb");
45  er=0;
46  im=NULL;
47  if (fp==NULL) er=imFILE_NOT_FOUND;
48  else
49  {
50    if (fread(buf,1,6,fp)==6)
51    {
52      buf[6]=0;
53      if (!strcmp("GIF87a",buf))
54      {
55        fread((char *)&gif_screen.w,2,1,fp);
56        gif_screen.w=int_to_local(gif_screen.w);
57        fread((char *)&gif_screen.h,2,1,fp);
58        gif_screen.h=int_to_local(gif_screen.h);
59        fread((char *)&gif_screen.color_info,1,1,fp);
60        fread((char *)&gif_screen.background,1,1,fp);
61        if (fread((char *)&gif_screen.reserved,1,1,fp)==1)
62        {
63          if (gif_screen.color_info&128)
64          {
65            ncolors=2<<(gif_screen.color_info&0x0f);
66            make_block(sizeof(palette));
67//          pal=new palette(ncolors);
68            pal=new palette(256);
69            if (pal)
70            { 
71              if (fread((char *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
72            } else er=imMEMORY_ERROR;
73          }
74          if (!er)
75          { do
76            {
77              if (fread((char *)&sep,1,1,fp)!=1)
78                er=imREAD_ERROR;
79            } while (!er && sep!=',');
80            fread((char *)&gif_image.xoff,2,1,fp);
81            gif_image.xoff=int_to_local(gif_image.xoff);
82            fread((char *)&gif_image.yoff,2,1,fp);
83            gif_image.yoff=int_to_local(gif_image.yoff);
84            fread((char *)&gif_image.w,2,1,fp);
85            gif_image.w=int_to_local(gif_image.w);
86            fread((char *)&gif_image.h,2,1,fp);
87            gif_image.h=int_to_local(gif_image.h);
88            if (!er && (fread((char *)&gif_image.color_info,1,1,fp)==1))
89            {
90              if (gif_image.color_info&128)
91              {
92                ncolors=2<<(gif_image.color_info&0x0f);
93                CHECK(ncolors<=256);
94                make_block(sizeof(palette));
95                pal = new palette(ncolors);
96                if (pal)
97                { if (fread((char *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
98                } else er=imMEMORY_ERROR;
99              }
100
101              if (!er)
102              {
103                make_block(sizeof(image));
104                im=new image(gif_image.w+1,gif_image.h);
105                decode_gif_data(im,fp);
106                fclose(fp);
107              }
108
109            } else er=imREAD_ERROR;
110          }
111
112        } else er=imREAD_ERROR;
113      } else er=imINCORRECT_FILETYPE;
114    } else er=imREAD_ERROR;
115    fclose(fp);
116  }
117  set_error(er);
118  return im;
119}
Note: See TracBrowser for help on using the repository browser.