File: gifread.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (119 lines) | stat: -rw-r--r-- 2,949 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "gifread.hpp"
#include "palette.hpp"
#include "image.hpp"
#include "video.hpp"
#include "linked.hpp"
#include "gifdecod.hpp"
#include "system.h"
#include "dos.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "dir.h"
#include "macs.hpp"


struct {
	unsigned short int	Width;
	unsigned short int	Height;
	unsigned char	ColorMap[3][256];
	unsigned short int	BitPixel;
	unsigned short int	ColorResolution;
	unsigned short int	Background;
	unsigned short int	AspectRatio;
} GifScreen;

struct {
  unsigned short int w,h;
  unsigned char color_info,background,reserved;
} gif_screen;

struct {
  unsigned short int xoff,yoff,w,h;
  unsigned char color_info;
} gif_image;

image *read_gif(char *fn, palette *&pal)
{
  char buf[100],er;
  unsigned char sep;
  int ncolors;
  FILE *fp;
  image *im;
  clear_errors();
  fp=fopen(fn,"rb");
  er=0;
  im=NULL;
  if (fp==NULL) er=imFILE_NOT_FOUND;
  else
  {
    if (fread(buf,1,6,fp)==6)
    {
      buf[6]=0;
      if (!strcmp("GIF87a",buf))
      {
        fread((char *)&gif_screen.w,2,1,fp);
        gif_screen.w=int_to_local(gif_screen.w);
        fread((char *)&gif_screen.h,2,1,fp);
        gif_screen.h=int_to_local(gif_screen.h);
        fread((char *)&gif_screen.color_info,1,1,fp);
        fread((char *)&gif_screen.background,1,1,fp);
        if (fread((char *)&gif_screen.reserved,1,1,fp)==1)
	{
	  if (gif_screen.color_info&128)
	  {
	    ncolors=2<<(gif_screen.color_info&0x0f);
	    make_block(sizeof(palette));
//	    pal=new palette(ncolors);
	    pal=new palette(256);
	    if (pal)
	    {  
              if (fread((char *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
	    } else er=imMEMORY_ERROR;
	  }
	  if (!er)
	  { do
	    {
	      if (fread((char *)&sep,1,1,fp)!=1)
		er=imREAD_ERROR;
	    } while (!er && sep!=',');
            fread((char *)&gif_image.xoff,2,1,fp);
            gif_image.xoff=int_to_local(gif_image.xoff);
            fread((char *)&gif_image.yoff,2,1,fp);
            gif_image.yoff=int_to_local(gif_image.yoff);
            fread((char *)&gif_image.w,2,1,fp);
            gif_image.w=int_to_local(gif_image.w);
            fread((char *)&gif_image.h,2,1,fp);
            gif_image.h=int_to_local(gif_image.h);
	    if (!er && (fread((char *)&gif_image.color_info,1,1,fp)==1))
	    {
	      if (gif_image.color_info&128)
	      {
		ncolors=2<<(gif_image.color_info&0x0f);
                CHECK(ncolors<=256);
		make_block(sizeof(palette));
		pal = new palette(ncolors);
		if (pal)
		{ if (fread((char *)pal->addr(),1,ncolors*3,fp)!=ncolors*3) er=imREAD_ERROR;
		} else er=imMEMORY_ERROR;
	      }

	      if (!er)
	      {
		make_block(sizeof(image));
		im=new image(gif_image.w+1,gif_image.h);
		decode_gif_data(im,fp);
		fclose(fp);
	      }

	    } else er=imREAD_ERROR;
	  }

	} else er=imREAD_ERROR;
      } else er=imINCORRECT_FILETYPE;
    } else er=imREAD_ERROR;
    fclose(fp);
  }
  set_error(er);
  return im;
}