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 | #ifndef _JPG_LOAD_HH_
|
---|
10 | #define _JPG_LOAD_HH_
|
---|
11 |
|
---|
12 | //#define JPEG_INTERNALS
|
---|
13 | //TODO clean up memory management
|
---|
14 | //convert types and memory and file stuff to I4
|
---|
15 | #include "file/buf_file.hh"
|
---|
16 | #include "loaders/load.hh"
|
---|
17 | #include "image/image.hh"
|
---|
18 | #include "loaders/jpg/cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
---|
19 | #include "loaders/jpg/jversion.h" /* for version message */
|
---|
20 | #include "status/status.hh"
|
---|
21 | #include "image/image.hh"
|
---|
22 |
|
---|
23 | #include <ctype.h> /* to declare isprint() */
|
---|
24 |
|
---|
25 | static const char * const cdjpeg_message_table[] = {
|
---|
26 | #include "loaders/jpg/cderror.h"
|
---|
27 | NULL
|
---|
28 | };
|
---|
29 |
|
---|
30 | static inline w32 read_colorbgr(w8 *cp)
|
---|
31 | {
|
---|
32 | w8 buf[4];
|
---|
33 |
|
---|
34 | *(w32 *)buf=*((w32 *)cp);
|
---|
35 | // if (!buf[3]) return 0;
|
---|
36 | return (((w32)buf[0]<<16) |
|
---|
37 | ((w32)buf[1]<<8) |
|
---|
38 | ((w32)buf[2]));
|
---|
39 | }
|
---|
40 |
|
---|
41 | LOCAL(unsigned int)
|
---|
42 | jpeg_getc (j_decompress_ptr cinfo)
|
---|
43 | /* Read next byte */
|
---|
44 | {
|
---|
45 | struct jpeg_source_mgr * datasrc = cinfo->src;
|
---|
46 |
|
---|
47 | if (datasrc->bytes_in_buffer == 0) {
|
---|
48 | if (! (*datasrc->fill_input_buffer) (cinfo))
|
---|
49 | ERREXIT(cinfo, JERR_CANT_SUSPEND);
|
---|
50 | }
|
---|
51 | datasrc->bytes_in_buffer--;
|
---|
52 | return GETJOCTET(*datasrc->next_input_byte++);
|
---|
53 | }
|
---|
54 |
|
---|
55 | METHODDEF(boolean)
|
---|
56 | COM_handler (j_decompress_ptr cinfo)
|
---|
57 | {
|
---|
58 | boolean traceit = (cinfo->err->trace_level >= 1);
|
---|
59 | INT32 length;
|
---|
60 | unsigned int ch;
|
---|
61 | unsigned int lastch = 0;
|
---|
62 |
|
---|
63 | length = jpeg_getc(cinfo) << 8;
|
---|
64 | length += jpeg_getc(cinfo);
|
---|
65 | length -= 2; /* discount the length word itself */
|
---|
66 |
|
---|
67 | if (traceit)
|
---|
68 | fprintf(stderr, "Comment, length %ld:\n", (long) length);
|
---|
69 |
|
---|
70 | while (--length >= 0) {
|
---|
71 | ch = jpeg_getc(cinfo);
|
---|
72 | if (traceit) {
|
---|
73 | /* Emit the character in a readable form.
|
---|
74 | * Nonprintables are converted to \nnn form,
|
---|
75 | * while \ is converted to \\.
|
---|
76 | * Newlines in CR, CR/LF, or LF form will be printed as one newline.
|
---|
77 | */
|
---|
78 | if (ch == '\r') {
|
---|
79 | fprintf(stderr, "\n");
|
---|
80 | } else if (ch == '\n') {
|
---|
81 | if (lastch != '\r')
|
---|
82 | fprintf(stderr, "\n");
|
---|
83 | } else if (ch == '\\') {
|
---|
84 | fprintf(stderr, "\\\\");
|
---|
85 | } else if (isprint(ch)) {
|
---|
86 | putc(ch, stderr);
|
---|
87 | } else {
|
---|
88 | fprintf(stderr, "\\%03o", ch);
|
---|
89 | }
|
---|
90 | lastch = ch;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (traceit)
|
---|
95 | fprintf(stderr, "\n");
|
---|
96 |
|
---|
97 | return TRUE;
|
---|
98 | }
|
---|
99 |
|
---|
100 | class i4_jpg_loader_class : public i4_image_loader_class
|
---|
101 | {
|
---|
102 | public :
|
---|
103 | // returns the maximum size you need to identify your file format
|
---|
104 | virtual w16 max_header_size() { return 10; }
|
---|
105 | virtual i4_bool recognize_header(w8 *buf)
|
---|
106 | {
|
---|
107 | if(buf[0]==0xff && //marker
|
---|
108 | buf[1]==0xd8 && //soi seg
|
---|
109 | buf[2]==0xff && //marker
|
---|
110 | buf[3]==0xe0 && //app0 seg
|
---|
111 | buf[6]=='J' && buf[7]=='F' &&
|
---|
112 | buf[8]=='I' && buf[9]=='F')
|
---|
113 | return i4_T;
|
---|
114 | else return i4_F;
|
---|
115 | }
|
---|
116 |
|
---|
117 | i4_bool special_load24(i4_file_class *fp,w8 *dst_texture, sw32 *dst_width, sw32 *dst_height);
|
---|
118 |
|
---|
119 | i4_bool special_load16(i4_file_class *fp,
|
---|
120 | w16 *dst_texture,
|
---|
121 | sw32 base_width,
|
---|
122 | const i4_pal *pal);
|
---|
123 |
|
---|
124 | i4_bool convert_to_raw16(i4_file_class *fp,
|
---|
125 | FILE *dst_file,
|
---|
126 | i4_status_class *status,
|
---|
127 | const i4_pal *pal,
|
---|
128 | sw32 expected_width,
|
---|
129 | sw32 expected_height);
|
---|
130 |
|
---|
131 | // assume fp is at the start of the file
|
---|
132 | virtual i4_image_class *load(i4_file_class *fp,
|
---|
133 | i4_status_class *status);
|
---|
134 |
|
---|
135 | struct jpeg_decompress_struct cinfo;
|
---|
136 | struct jpeg_error_mgr jerr;
|
---|
137 |
|
---|
138 | };
|
---|
139 |
|
---|
140 | extern i4_jpg_loader_class i4_jpg_loader_instance;
|
---|
141 |
|
---|
142 | #endif
|
---|