source: abuse/tags/pd/imlib/glread.c @ 597

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