fix compile errors
[wdl/wdl-ol.git] / WDL / lice / lice_pcx.cpp
blob207543ea8e1410e0263107b7349541f586a8204d
1 /*
2 Cockos WDL - LICE - Lightweight Image Compositing Engine
3 Copyright (C) 2007 and later, Cockos Incorporated
4 File: lice_pcx.cpp (PCX loading for LICE)
5 See lice.h for license and other information
6 */
8 #include "lice.h"
9 #include "../wdltypes.h"
11 #include <stdio.h>
13 // note: you'd never really want to use PCX files, but in case you do...
15 LICE_IBitmap *LICE_LoadPCX(const char *filename, LICE_IBitmap *_bmp)
17 FILE *fp = fopen(filename,"rb");
18 if(!fp) return 0;
20 fgetc(fp);
21 if (fgetc(fp) != 5) { fclose(fp); return NULL; }
22 if (fgetc(fp) != 1) { fclose(fp); return NULL; }
23 if (fgetc(fp) != 8) { fclose(fp); return NULL; }
25 int sx = fgetc(fp); sx += fgetc(fp)<<8;
26 int sy = fgetc(fp); sy += fgetc(fp)<<8;
27 int ex = fgetc(fp); ex += fgetc(fp)<<8;
28 int ey = fgetc(fp); ey += fgetc(fp)<<8;
31 unsigned char pal[768];
32 fseek(fp,-769,SEEK_END);
33 if (fgetc(fp) != 12) { fclose(fp); return NULL; }
34 fread(pal,1,768,fp);
35 if (feof(fp)) { fclose(fp); return NULL; }
38 LICE_IBitmap *usebmp = NULL;
39 if (_bmp) (usebmp=_bmp)->resize(ex-sx+1,ey-sy+1);
40 else usebmp = new WDL_NEW LICE_MemBitmap(ex-sx+1,ey-sy+1);
41 if (!usebmp || usebmp->getWidth() != (ex-sx+1) || usebmp->getHeight() != (ey-sy+1))
43 if (usebmp != _bmp) delete usebmp;
44 fclose(fp);
45 return NULL;
48 fseek(fp,128,SEEK_SET);
50 LICE_Clear(usebmp,0);
51 int y = usebmp->getHeight();
52 int w = usebmp->getWidth();
53 int rowspan = usebmp->getRowSpan();
54 LICE_pixel *pout = usebmp->getBits();
55 if (usebmp->isFlipped())
57 pout += rowspan*(y-1);
58 rowspan=-rowspan;
60 while (y--)
62 int xpos = 0;
63 while (xpos < w)
65 int c = fgetc(fp);
66 if (c&~255) break;
67 if ((c & 192) == 192)
69 int oc = (fgetc(fp))&255;
70 LICE_pixel t=LICE_RGBA(pal[oc*3],pal[oc*3+1],pal[oc*3+2],255);
72 c&=63;
73 while (c-- && xpos<w) pout[xpos++] = t;
75 else pout[xpos++] = LICE_RGBA(pal[c*3],pal[c*3+1],pal[c*3+2],255);
77 pout+=rowspan;
79 fclose(fp);
81 return usebmp;
85 class LICE_PCXLoader
87 public:
88 _LICE_ImageLoader_rec rec;
89 LICE_PCXLoader()
91 rec.loadfunc = loadfunc;
92 rec.get_extlist = get_extlist;
93 rec._next = LICE_ImageLoader_list;
94 LICE_ImageLoader_list = &rec;
97 static LICE_IBitmap *loadfunc(const char *filename, bool checkFileName, LICE_IBitmap *bmpbase)
99 if (checkFileName)
101 const char *p=filename;
102 while (*p)p++;
103 while (p>filename && *p != '\\' && *p != '/' && *p != '.') p--;
104 if (stricmp(p,".pcx")) return 0;
106 return LICE_LoadPCX(filename,bmpbase);
108 static const char *get_extlist()
110 return "PCX files (*.PCX)\0*.PCX\0";
115 LICE_PCXLoader LICE_pcxldr;