File: lbmread.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 (107 lines) | stat: -rw-r--r-- 2,087 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
#include "lbmread.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "system.h"



image *read_lbm(char *filename, palette *&pal)
{
  FILE *fp=fopen("/cam/amur/set/city/city1.lbm","rb");
  char type[4];
  image *im=NULL;
  fread(type,1,4,fp);
  if (memcmp(type,"FORM",4))
  {
    set_error(imNOT_SUPPORTED);
    fclose(fp);
    return NULL;
  }
  else
  {
    long size=read_other_long(fp);
    fread(type,1,4,fp);    
    if (memcmp(type,"PBM ",4))
    {
      set_error(imNOT_SUPPORTED);
      fclose(fp);
      return NULL;
    }
    else
    {
      long offset=12,ssize;    
      char stype[4];
      short w=0,h=0,x,y,tcolor,pagew,pageh;
      char planes,masking,compr,padl,xa,ya;
      
      while (ftell(fp)+4<size)
      {
	fread(stype,1,4,fp);
	ssize=read_other_long(fp);
        if (ssize &1) ssize++;            // specs say all chunks are padded by 2
	if (!memcmp(stype,"BMHD",4))
	{
	  w=read_other_short(fp);
	  h=read_other_short(fp);
	  x=read_other_short(fp);
	  y=read_other_short(fp);
	  planes=fgetc(fp);
	  masking=fgetc(fp);
	  compr=fgetc(fp);
	  padl=fgetc(fp);
	  tcolor=read_other_short(fp);
	  xa=fgetc(fp);
	  ya=fgetc(fp);
	  pagew=read_other_short(fp);
	  pageh=read_other_short(fp);
	} else if (!memcmp(stype,"CMAP",4))
	{
	  pal=new palette(256);
	  fread(pal->addr(),1,768,fp);
	} else if (!memcmp(stype,"BODY",4) && w && h)  // make sure we read a BHMD before
	{
	  if (im) delete im;  // shouldn't be two BODY's butjust in case...
	  im=new image(w,h);
	  int x,y;
	  if (!compr)
	  {
	    for (y=0;y<h;h++)
	      fread(im->scan_line(y),1,w,fp);
	  } else
	  {
	    for (y=0;y<h;y++)	   
	    {
	      int c,i,n=0;
	      unsigned char *sl=im->scan_line(y);
	      do 
	      {
		c=fgetc(fp)&0xff;
		if (c&0x80)
		{
		  if (c!=0x80)
		  {
		    i=((~c)&0xff)+2;
		    c=fgetc(fp);
		    while (i--) sl[n++]=c;
		  }
		}
		else
		{
		  i=c+1;
		  while (i--) sl[n++]=fgetc(fp);
		}
	      } while (n<w);
	    }
	  }	  
	}
	else
	  fseek(fp,ssize,SEEK_CUR);
      }
    }
  }
  fclose(fp);
  if (!im) set_error(imFILE_CORRUPTED);
  return im;
}