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

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#include "macs.hpp"
2#include "image.hpp"
3#include "palette.hpp"
4#include "video.hpp"
5#include "system.h"
6#include "dprint.hpp"
7#include <stdio.h>
8
9image *read_glfont(char *fn)
10{
11  image *im,*sub;
12  unsigned short length,y;
13  unsigned char size,first,width,height,gsize,last;
14  FILE *fp;
15  fp=fopen(fn,"rb");
16  if (!fp) return NULL;
17  fread(&length,1,2,fp);  length=int_to_local(length);
18  fread(&size,1,1,fp);
19  fread(&first,1,1,fp);
20  if (size+first>255) { set_error(imFILE_CORRUPTED); fclose(fp); return NULL; }
21  fread(&width,1,1,fp);
22  fread(&height,1,1,fp);
23  fread(&gsize,1,1,fp);
24  make_block(sizeof(image));
25  im=new image(width*32,height*8);
26  make_block(sizeof(image));
27  sub=new image(width,height);
28  im->clear();  // in case all the fonts aren't in the file, clear extra area
29  last=first+size-1;
30  while (first<=last)
31  {
32    for (y=0;(int)y<(int)height;y++)
33    {
34      fread(sub->scan_line(y),1,gsize/height,fp);
35      sub->unpack_scanline(y);
36    }
37    sub->put_image(im,(first%32)*width,(first/32)*height);
38    first++;
39  }
40  delete sub;
41  return im;
42}
43
44image *read_pic(char *fn, palette *&pal)
45{
46  image *im;
47  char x[4],bpp;
48  unsigned char *sl,esc,c,n,marker,vmode;
49  unsigned short w,h,len,bufsize,blocks,sn,esize,edesc;
50  int xx,yy;
51  FILE *fp;
52  im=NULL;
53  fp=fopen(fn,"rb");
54
55  fread(&x[0],1,2,fp);
56  fread(&w,1,2,fp);
57  fread(&h,1,2,fp);
58  w=int_to_local(w);  h=int_to_local(h);
59  fread(x,1,4,fp);
60  fread(&bpp,1,1,fp);
61  fread(&marker,1,1,fp);
62  if (marker!=0xff)
63  { fclose(fp); set_error(imFILE_CORRUPTED); return NULL; }
64
65  im=new image(w,h);
66
67  fread(&vmode,1,1,fp);
68  fread(&edesc,1,2,fp);
69  edesc=int_to_local(edesc);
70  fread(&esize,1,2,fp);
71  esize=int_to_local(esize);
72  if (esize==768 && !pal)
73  { pal=new palette(1<<bpp);
74    fread(pal->addr(),1,(1<<bpp)*3,fp);
75    pal->shift(2);
76  }
77  else if (esize)
78    fseek(fp,esize,SEEK_CUR);
79  fread(&blocks,1,2,fp);
80  blocks=int_to_local(blocks);
81
82  yy=h; xx=w;
83
84  while (blocks-- && w>=1 && yy>=0)
85  {
86    fread(&bufsize,1,2,fp);
87    bufsize=int_to_local(bufsize);
88    fread(&len,1,2,fp);
89    len=int_to_local(len);
90    fread(&esc,1,1,fp);
91    while (yy>=0 && len)
92    {
93      fread(&c,1,1,fp);
94      if (c!=esc)
95      {
96        if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy);
97        if (yy==h) dprintf("bufsize=%d\n",bufsize); CHECK(yy<h); }
98        sl[xx++]=c;     len--;
99      }
100      else
101      {
102        fread(&n,1,1,fp);
103        if (n!=0)
104        {
105          fread(&c,1,1,fp);
106          while (n-- && yy>=0 && len)
107          {
108            if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy);
109              if (yy==h) dprintf("bufsize=%d\n",bufsize); CHECK(yy<h); }
110            sl[xx++]=c; len--;
111          }
112        }
113        else
114        {
115          fread(&sn,1,2,fp);
116          sn=int_to_local(sn);
117          fread(&c,1,1,fp);
118          while (sn-- && yy>=0 && len)
119          {
120            if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy); CHECK(yy<h); }
121            sl[xx++]=c; len--;
122          }
123        }
124
125      }
126    }
127  }
128  fclose(fp);
129  return im;
130}
131
132image *read_clp(char *fn)
133{
134  palette *pal=NULL;
135  image *im=read_pic(fn,pal);
136  if (pal) delete pal;
137  return im;
138}
139
Note: See TracBrowser for help on using the repository browser.