1 // ==========================================================
2 // KOALA Loader
3 //
4 // Design and implementation by
5 // - Floris van den Berg (flvdberg@wxs.nl)
6 //
7 // This file is part of FreeImage 3
8 //
9 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
10 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
11 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
12 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
13 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
14 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
15 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
16 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
17 // THIS DISCLAIMER.
18 //
19 // Use at your own risk!
20 // ==========================================================
21
22 #include "FreeImage.h"
23 #include "Utilities.h"
24
25 // ----------------------------------------------------------
26 // Constants + headers
27 // ----------------------------------------------------------
28
29 #ifdef _WIN32
30 #pragma pack(push, 1)
31 #else
32 #pragma pack(1)
33 #endif
34
35 typedef struct tagKOALA {
36 BYTE image[8000]; // pixmap image
37 BYTE colour1[1000]; // first colourmap (colour 1 and 2)
38 BYTE colour2[1000]; // second colourmap (colour 3)
39 BYTE background; // background colour
40 } koala_t;
41
42 struct colour_t {
43 int r;
44 int g;
45 int b;
46 };
47
48 #ifdef _WIN32
49 #pragma pack(pop)
50 #else
51 #pragma pack()
52 #endif
53
54 // ----------------------------------------------------------
55
56 #define CBM_WIDTH 320
57 #define CBM_HEIGHT 200
58
59 // ----------------------------------------------------------
60
61 const colour_t c64colours[16] = {
62 { 0, 0, 0 }, // Black
63 { 255, 255, 255 }, // White
64 { 170, 17, 17 }, // Red
65 { 12, 204, 204 }, // Cyan
66 { 221, 51, 221 }, // Purple
67 { 0, 187, 0 }, // Green
68 { 0, 0, 204 }, // Blue
69 { 255, 255, 140 }, // Yellow
70 { 204, 119, 34 }, // Orange
71 { 136, 68, 0 }, // Brown
72 { 255, 153, 136 }, // Light red
73 { 92, 92, 92 }, // Gray 1
74 { 170, 170, 170 }, // Gray 2
75 { 140, 255, 178 }, // Light green
76 { 39, 148, 255 }, // Light blue
77 { 196, 196, 196 } // Gray 3
78 };
79
80 // ==========================================================
81 // Plugin Interface
82 // ==========================================================
83
84 static int s_format_id;
85
86 // ==========================================================
87 // Plugin Implementation
88 // ==========================================================
89
90 const char * DLL_CALLCONV
Format()91 Format() {
92 return "KOALA";
93 }
94
95 const char * DLL_CALLCONV
Description()96 Description() {
97 return "C64 Koala Graphics";
98 }
99
100 const char * DLL_CALLCONV
Extension()101 Extension() {
102 return "koa";
103 }
104
105 const char * DLL_CALLCONV
RegExpr()106 RegExpr() {
107 return NULL;
108 }
109
110 static const char * DLL_CALLCONV
MimeType()111 MimeType() {
112 return "image/x-koala";
113 }
114
115 static BOOL DLL_CALLCONV
Validate(FreeImageIO * io,fi_handle handle)116 Validate(FreeImageIO *io, fi_handle handle) {
117 BYTE koala_signature[] = { 0x00, 0x60 };
118 BYTE signature[2] = { 0, 0 };
119
120 io->read_proc(signature, 1, sizeof(koala_signature), handle);
121
122 return (memcmp(koala_signature, signature, sizeof(koala_signature)) == 0);
123 }
124
125 static BOOL DLL_CALLCONV
SupportsExportDepth(int depth)126 SupportsExportDepth(int depth) {
127 return FALSE;
128 }
129
130 static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type)131 SupportsExportType(FREE_IMAGE_TYPE type) {
132 return FALSE;
133 }
134
135 // ----------------------------------------------------------
136
137 FIBITMAP * DLL_CALLCONV
Load(FreeImageIO * io,fi_handle handle,int page,int flags,void * data)138 Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
139 if (handle) {
140 koala_t image;
141
142 // read the load address
143
144 unsigned char load_address[2]; // highbit, lowbit
145
146 io->read_proc(&load_address, 1, 2, handle);
147
148 // if the load address is correct, skip it. otherwise ignore the load address
149
150 if ((load_address[0] != 0x00) || (load_address[1] != 0x60)) {
151 ((BYTE *)&image)[0] = load_address[0];
152 ((BYTE *)&image)[1] = load_address[1];
153
154 io->read_proc((BYTE *)&image + 2, 1, 10001 - 2, handle);
155 } else {
156 io->read_proc(&image, 1, 10001, handle);
157 }
158
159 // build DIB in memory
160
161 FIBITMAP *dib = FreeImage_Allocate(CBM_WIDTH, CBM_HEIGHT, 4);
162
163 if (dib) {
164 // write out the commodore 64 color palette
165
166 RGBQUAD *palette = FreeImage_GetPalette(dib);
167
168 for (int i = 0; i < 16; i++) {
169 palette[i].rgbBlue = (BYTE)c64colours[i].b;
170 palette[i].rgbGreen = (BYTE)c64colours[i].g;
171 palette[i].rgbRed = (BYTE)c64colours[i].r;
172 }
173
174 // write out bitmap data
175
176 BYTE pixel_mask[4] = { 0xc0, 0x30, 0x0c, 0x03 };
177 BYTE pixel_displacement[4] = { 6, 4, 2, 0 };
178 int pixel, index, colourindex;
179 unsigned char found_color = 0;
180
181 for (int y = 0; y < 200; y++) {
182 for (int x = 0; x < 160; x++) {
183 // Get value of pixel at (x,y)
184
185 index = (x / 4) * 8 + (y % 8) + (y / 8) * CBM_WIDTH;
186 colourindex = (x / 4) + (y / 8) * 40;
187 pixel = (image.image[index] & pixel_mask[x % 4]) >> pixel_displacement[x % 4];
188
189 // Retrieve RGB values
190
191 switch (pixel) {
192 case 0: // Background
193 found_color = image.background;
194 break;
195
196 case 1: // Colour 1
197 found_color = image.colour1[colourindex] >> 4;
198 break;
199
200 case 2: // Colour 2
201 found_color = image.colour1[colourindex] & 0xf;
202 break;
203
204 case 3: // Colour 3
205 found_color = image.colour2[colourindex] & 0xf;
206 break;
207 };
208
209 *(FreeImage_GetScanLine(dib, CBM_HEIGHT - y - 1) + x) = (found_color << 4) | found_color;
210 }
211 }
212
213 return dib;
214 }
215 }
216
217 return NULL;
218 }
219
220 // ==========================================================
221 // Init
222 // ==========================================================
223
224 void DLL_CALLCONV
InitKOALA(Plugin * plugin,int format_id)225 InitKOALA(Plugin *plugin, int format_id) {
226 s_format_id = format_id;
227
228 plugin->format_proc = Format;
229 plugin->description_proc = Description;
230 plugin->extension_proc = Extension;
231 plugin->regexpr_proc = RegExpr;
232 plugin->open_proc = NULL;
233 plugin->close_proc = NULL;
234 plugin->pagecount_proc = NULL;
235 plugin->pagecapability_proc = NULL;
236 plugin->load_proc = Load;
237 plugin->save_proc = NULL;
238 plugin->validate_proc = Validate;
239 plugin->mime_proc = MimeType;
240 plugin->supports_export_bpp_proc = SupportsExportDepth;
241 plugin->supports_export_type_proc = SupportsExportType;
242 plugin->supports_icc_profiles_proc = NULL;
243 }
244