[80] | 1 | /********************************************************************** <BR>
|
---|
| 2 | This file is part of Crack dot Com's free source code release of
|
---|
| 3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
| 4 | information about compiling & licensing issues visit this URL</a>
|
---|
| 5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
| 6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
| 7 | ***********************************************************************/
|
---|
| 8 |
|
---|
| 9 | #include "loaders/load.hh"
|
---|
| 10 | #include "image/image.hh"
|
---|
| 11 | #include "error/error.hh"
|
---|
| 12 | #include "palette/pal.hh"
|
---|
| 13 | #include "status/status.hh"
|
---|
| 14 | #include "file/file.hh"
|
---|
| 15 | #include "memory/malloc.hh"
|
---|
| 16 | #include "error/error.hh"
|
---|
| 17 | #include "palette/pal.hh"
|
---|
| 18 |
|
---|
| 19 | class i4_pcx_loader_class : public i4_image_loader_class
|
---|
| 20 | {
|
---|
| 21 | public :
|
---|
| 22 | w16 get_short(w32 offset, void *data)
|
---|
| 23 | {
|
---|
| 24 | w8 buf[2];
|
---|
| 25 | buf[0]=*(((w8 *)data)+offset);
|
---|
| 26 | buf[1]=*(((w8 *)data)+offset+1);
|
---|
| 27 | return (buf[1]<<8)|buf[0];
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | w16 max_header_size() { return 4; }
|
---|
| 32 |
|
---|
| 33 | virtual i4_bool recognize_header(w8 *buf)
|
---|
| 34 | {
|
---|
| 35 | if (buf[0]==10 && // manufacturer
|
---|
| 36 | buf[2]==1 && // encoding
|
---|
| 37 | buf[3]==8) // bits per pixel
|
---|
| 38 | return i4_T;
|
---|
| 39 | else return i4_F;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | int read_PCX8_line(i4_file_class *fp, w8 *start, sw16 skip, w16 w)
|
---|
| 44 | {
|
---|
| 45 | int n=0,i;
|
---|
| 46 | w8 c;
|
---|
| 47 | do
|
---|
| 48 | {
|
---|
| 49 | if (!fp->read(&c,1)) return 0;
|
---|
| 50 | if ((c&0xc0)==0xc0)
|
---|
| 51 | {
|
---|
| 52 | i=c&0x3f;
|
---|
| 53 | if (!fp->read(&c,1)) return 0;
|
---|
| 54 | while (i--)
|
---|
| 55 | {
|
---|
| 56 | *start=c;
|
---|
| 57 | start+=skip;
|
---|
| 58 | n++;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | else
|
---|
| 62 | {
|
---|
| 63 | *start=c;
|
---|
| 64 | start+=skip;
|
---|
| 65 | n++;
|
---|
| 66 | }
|
---|
| 67 | } while (n<w);
|
---|
| 68 | return 1;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | virtual i4_image_class *load(i4_file_class *fp,
|
---|
| 74 | i4_status_class *status)
|
---|
| 75 | {
|
---|
| 76 | w8 header[128];
|
---|
| 77 | if (fp->read(header,128)!=128) return 0;
|
---|
| 78 | w16 w=get_short(8,header)-get_short(4,header)+1;
|
---|
| 79 | w16 h=get_short(10,header)-get_short(6,header)+1;
|
---|
| 80 | if (w>5000 || h>5000)
|
---|
| 81 | i4_error("w(%d) or h(%d) proabbly bad",w,h);
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | i4_image_class *im=i4_create_image(w,h, i4_pal_man.default_8());
|
---|
| 85 | w16 bpl=im->bpl;
|
---|
| 86 | w8 *data=(w8 *)im->data;
|
---|
| 87 |
|
---|
| 88 | w16 y;
|
---|
| 89 | for (y=0;y<h;y++,data+=bpl)
|
---|
| 90 | {
|
---|
| 91 | if (status)
|
---|
| 92 | status->update(y/(float)h);
|
---|
| 93 |
|
---|
| 94 | if (!read_PCX8_line(fp,data,1,w))
|
---|
| 95 | {
|
---|
| 96 | delete im;
|
---|
| 97 | return 0;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | w8 palette_confirm;
|
---|
| 102 | if (!fp->read(&palette_confirm,1) || !(palette_confirm==12))
|
---|
| 103 | {
|
---|
| 104 | i4_warning("PCX load expect palette confirm to be 12, is this 256 color?");
|
---|
| 105 | delete im;
|
---|
| 106 | return 0;
|
---|
| 107 | }
|
---|
| 108 | else
|
---|
| 109 | {
|
---|
| 110 | w8 pal[256*3];
|
---|
| 111 | if (fp->read(pal,256*3)!=256*3)
|
---|
| 112 | {
|
---|
| 113 | delete im;
|
---|
| 114 | return 0;
|
---|
| 115 | }
|
---|
| 116 | w32 word_pal[256],x;
|
---|
| 117 | for (x=0;x<256;x++) {
|
---|
| 118 | // if (pal[x*3]==254 && pal[x*3+1]==2 && pal[x*3+2]==166) {
|
---|
| 119 | // pal[x*3]=0;
|
---|
| 120 | // pal[x*3+1]=0;
|
---|
| 121 | // pal[x*3+2]=0;
|
---|
| 122 | // }
|
---|
| 123 | word_pal[x]=(pal[x*3]<<16)|(pal[x*3+1]<<8)|pal[x*3+2];
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | i4_pixel_format fmt;
|
---|
| 127 | fmt.pixel_depth=I4_8BIT;
|
---|
| 128 | fmt.lookup=word_pal;
|
---|
| 129 |
|
---|
| 130 | im->set_pal(i4_pal_man.register_pal(&fmt));
|
---|
| 131 | }
|
---|
| 132 | return im;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | } i4_pcx_loader_instance;
|
---|
| 136 |
|
---|