Skip to content

Commit 4b4bb94

Browse files
committed
Move color conversion code to s3util
1 parent 600cd62 commit 4b4bb94

8 files changed

+24
-107
lines changed

invert.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ void invert_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_excepti
66

77
int pixel_count = s3dat_width(res->res)*s3dat_height(res->res);
88

9-
s3dat_color_t* bmp_data = s3dat_bmpdata(res->res);
9+
s3util_color_t* bmp_data = s3dat_bmpdata(res->res);
1010
for(uint16_t i = 0;i != pixel_count;i++) {
1111
bmp_data[i].red = 256-bmp_data[i].red;
1212
bmp_data[i].green = 256-bmp_data[i].green;

src/bitmap.c

+6-69
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
167167

168168
uint8_t stat = 0;
169169
for(uint32_t y = 0;y != bitmap->height;y++) {
170-
s3dat_color_t* color;
170+
s3util_color_t* color;
171171

172172
for(uint32_t x = 0;x != bitmap->width;x++) {
173173
color = bitmap->data+(y*bitmap->width+x);
@@ -225,7 +225,7 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
225225
for(uint32_t x = 0;x != bitmap->width;x++) {
226226
if(bitmap->data[y*bitmap->width+x].alpha > 127) {
227227
stat = 2;
228-
s3dat_internal_8b_to_native(bitmap->data+(y*bitmap->width+x), data, bitmap->type);
228+
s3util_internal_8b_to_native(bitmap->data + (y * bitmap->width + x), data, bitmap->type);
229229
data += pixel_size;
230230
current_data++;
231231
} else {
@@ -256,72 +256,9 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
256256
}
257257
}
258258

259-
s3dat_color_t s3dat_internal_ex(void* addr, s3dat_color_type type) {
260-
s3dat_color_t color = {0, 0, 0, 0xFF};
261-
if(type == s3dat_alpha1) return color;
259+
s3util_color_t s3dat_internal_error_color = {0, 0, 0, 0};
262260

263-
264-
double d58 = 255.0/31.0;
265-
double d68 = 255.0/63.0;
266-
267-
if(type == s3dat_gray5) {
268-
color.red = color.green = color.blue = (int)((*((uint8_t*)addr) & 0x1F)*d58);
269-
return color;
270-
}
271-
272-
uint16_t raw = s3util_le16p(addr);
273-
274-
if(type == s3dat_rgb555) {
275-
color.red = (uint8_t)(((raw >> 10) & 0x1F)*d58);
276-
color.green = (uint8_t)(((raw >> 5) & 0x1F)*d58);
277-
} else {
278-
color.red = (uint8_t)(((raw >> 11)& 0x1F)*d58);
279-
color.green = (uint8_t)(((raw >> 5) & 0x3F)*d68);
280-
}
281-
color.blue = (uint8_t)((raw & 0x1F)*d58);
282-
283-
return color;
284-
}
285-
286-
void s3dat_internal_8b_to_native(s3dat_color_t* color, void* to, s3dat_color_type type) {
287-
if(type == s3dat_alpha1) return;
288-
289-
uint8_t* ptr8 = to;
290-
uint16_t* ptr16 = to;
291-
292-
double d85 = 31.0/255.0;
293-
double d86 = 63.0/255.0;
294-
295-
if(type == s3dat_gray5) {
296-
uint8_t gray5 = ((color->red+color->green+color->blue)/3)*d85;
297-
*ptr8 = (gray5 & 0x1F);
298-
return;
299-
}
300-
301-
uint16_t red, green;
302-
303-
uint16_t blue = (uint16_t)(color->blue*d85);
304-
305-
if(type == s3dat_rgb555) {
306-
red = color->red*d85;
307-
green = color->green*d85;
308-
309-
red = (red) << 10;
310-
green = (green) << 5;
311-
} else {
312-
red = color->red*d85;
313-
green = color->green*d86;
314-
315-
red = (red) << 11;
316-
green = (green) << 5;
317-
}
318-
319-
*ptr16 = s3util_le16(red + green + blue);
320-
}
321-
322-
s3dat_color_t s3dat_internal_error_color = {0, 0, 0, 0};
323-
324-
s3dat_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3util_exception_t** throws) {
261+
s3util_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3util_exception_t** throws) {
325262
if(palette > handle->palette_index->len) {
326263
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_OUT_OF_RANGE, __FILE__, __func__, __LINE__);
327264
return s3dat_internal_error_color;
@@ -339,7 +276,7 @@ s3dat_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uin
339276
return s3dat_internal_error_color;
340277
}
341278

342-
return s3dat_internal_ex(&color, handle->green_6b ? s3dat_rgb565 : s3dat_rgb555);
279+
return s3util_native_to_8b(&color, handle->green_6b ? s3util_rgb565 : s3util_rgb555);
343280
}
344281

345282
uint16_t s3dat_width(s3dat_ref_t* bmp) {
@@ -373,7 +310,7 @@ uint32_t* s3dat_gui_meta(s3dat_ref_t* bmp) {
373310
return &bmp->data.bmp->gui_type;
374311
}
375312

376-
s3dat_color_t* s3dat_bmpdata(s3dat_ref_t* bmp) {
313+
s3util_color_t* s3dat_bmpdata(s3dat_ref_t* bmp) {
377314
if(!s3dat_is_bitmap(bmp)) return NULL;
378315
return bmp->data.bmp->data;
379316
}

src/extract.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ void s3dat_unpack_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_e
142142
s3dat_ref_t* palette = s3dat_new_bitmap(handle, handle->palette_line_length, 8, throws);
143143
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
144144

145-
palette->data.bmp->type = handle->green_6b ? s3dat_rgb565 : s3dat_rgb555;
145+
palette->data.bmp->type = handle->green_6b ? s3util_rgb565 : s3util_rgb555;
146146

147147
uint32_t colors = handle->palette_line_length*8;
148148
uint16_t* color_addr = package->data;
149149
for(uint32_t i = 0;i != colors;i++) {
150-
palette->data.bmp->data[i] = s3dat_internal_ex(color_addr++, palette->data.bmp->type);
150+
palette->data.bmp->data[i] = s3util_native_to_8b(color_addr++, palette->data.bmp->type);
151151
}
152152
new_ref = palette;
153153
} else if(res->type == s3dat_string) {
@@ -168,17 +168,17 @@ void s3dat_unpack_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_e
168168
uint16_t landscape_type = 0;
169169
uint32_t gui_type = 0;
170170

171-
s3dat_color_type color_type;
171+
s3util_color_type color_type;
172172
uint32_t pixel_size;
173173

174174
if(res->type == s3dat_torso) {
175-
color_type = s3dat_gray5;
175+
color_type = s3util_gray5;
176176
pixel_size = 1;
177177
} else if(res->type == s3dat_shadow) {
178-
color_type = s3dat_alpha1;
178+
color_type = s3util_alpha1;
179179
pixel_size = 0;
180180
} else {
181-
color_type = handle->green_6b ? s3dat_rgb565 : s3dat_rgb555;
181+
color_type = handle->green_6b ? s3util_rgb565 : s3util_rgb555;
182182
pixel_size = 2;
183183
}
184184

@@ -230,7 +230,7 @@ void s3dat_unpack_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_e
230230
uint16_t x = 0;
231231
uint16_t y = 0;
232232

233-
s3dat_color_t trans_color = {0, 0, 0, 0};
233+
s3util_color_t trans_color = {0, 0, 0, 0};
234234

235235
while(y < height) {
236236
uint16_t meta = s3util_le16p(data_ptr);
@@ -245,7 +245,7 @@ void s3dat_unpack_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_e
245245

246246
uint8_t datalen = meta & 0xFF;
247247
while(datalen > 0) {
248-
image->data.bmp->data[y*width+x] = s3dat_internal_ex(data_ptr, color_type);
248+
image->data.bmp->data[y*width+x] = s3util_native_to_8b(data_ptr, color_type);
249249
data_ptr += pixel_size;
250250
datalen--;
251251
x++;

src/s3dat.h

+2-19
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ typedef enum {
5151
s3dat_landscape_big2 = 0x501,
5252
} s3dat_landscape_type;
5353

54-
typedef enum {
55-
s3dat_alpha1,
56-
s3dat_rgb565,
57-
s3dat_rgb555,
58-
s3dat_gray5,
59-
s3dat_unknown_color, // s3dat_new_bitmap in s3dat_ext.h
60-
} s3dat_color_type;
61-
62-
6354
//internal types
6455
typedef struct s3dat_animation_t s3dat_animation_t;
6556
typedef struct s3dat_string_t s3dat_string_t;
@@ -76,7 +67,6 @@ typedef struct s3dat_ref_t s3dat_ref_t;
7667
typedef struct s3dat_res_t s3dat_res_t;
7768

7869
typedef struct s3dat_frame_t s3dat_frame_t;
79-
typedef struct s3dat_color_t s3dat_color_t;
8070

8171

8272
struct s3dat_frame_t {
@@ -98,13 +88,6 @@ struct s3dat_frame_t {
9888
uint16_t flag2;
9989
};
10090

101-
struct s3dat_color_t {
102-
uint8_t red;
103-
uint8_t green;
104-
uint8_t blue;
105-
uint8_t alpha;
106-
};
107-
10891
void s3dat_writefile_name(s3dat_t* handle, char* name, s3util_exception_t** throws);
10992
void s3dat_writefile_fd(s3dat_t* handle, uint32_t* file, s3util_exception_t** throws);
11093
void s3dat_writefile_ioset(s3dat_t* handle, void* io_arg, s3util_ioset_t* ioset, bool use_openclose_func, s3util_exception_t** throws);
@@ -138,7 +121,7 @@ s3dat_ref_t* s3dat_extract_palette(s3dat_t* handle, uint16_t palette, s3util_exc
138121
s3dat_ref_t* s3dat_extract_sound(s3dat_t* handle, uint16_t soundtype, uint32_t altindex, s3util_exception_t** throws);
139122

140123

141-
s3dat_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3util_exception_t** throws);
124+
s3util_color_t s3dat_extract_palette_color(s3dat_t* handle, uint16_t palette, uint8_t brightness, uint32_t x, s3util_exception_t** throws);
142125

143126
void s3dat_add_cache(s3dat_t* parent, s3util_exception_t** throws);
144127
void s3dat_add_utf8_encoding(s3dat_t* handle, s3util_exception_t** throws);
@@ -173,7 +156,7 @@ int16_t* s3dat_xoff(s3dat_ref_t* bmp);
173156
int16_t* s3dat_yoff(s3dat_ref_t* bmp);
174157
uint16_t* s3dat_landscape_meta(s3dat_ref_t* bmp);
175158
uint32_t* s3dat_gui_meta(s3dat_ref_t* bmp);
176-
s3dat_color_t* s3dat_bmpdata(s3dat_ref_t* bmp);
159+
s3util_color_t* s3dat_bmpdata(s3dat_ref_t* bmp);
177160

178161
//animation
179162
bool s3dat_is_animation(s3dat_ref_t* ani);

src/s3dat_internal.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ struct s3dat_bitmap_t {
3434
int16_t yoff;
3535

3636
s3dat_t* src;
37-
s3dat_color_type type;
37+
s3util_color_type type;
3838

39-
s3dat_color_t* data;
39+
s3util_color_t* data;
4040

4141
};
4242

@@ -149,9 +149,8 @@ void s3dat_internal_delete_index32(s3dat_t* handle, s3dat_index32_t* index);
149149
void s3dat_internal_delete_seq(s3dat_t* handle, s3dat_seq_index_t* seq);
150150
void s3dat_internal_delete_seq32(s3dat_t* handle, s3dat_seq_index32_t* seq);
151151

152-
void s3dat_internal_read_bitmap_data(s3dat_t* handle, s3dat_color_type type, uint16_t width, uint16_t height, s3dat_color_t** re_pixdata, s3util_exception_t** throws);
152+
void s3dat_internal_read_bitmap_data(s3dat_t* handle, s3util_color_type type, uint16_t width, uint16_t height, s3util_color_t** re_pixdata, s3util_exception_t** throws);
153153
void s3dat_internal_read_bitmap_header(s3dat_t* handle, s3dat_content_type type, uint32_t from, uint16_t* width, uint16_t* height, uint16_t* xoff, uint16_t* yoff, s3util_exception_t** throws);
154-
s3dat_color_t s3dat_internal_ex(void* addr, s3dat_color_type type);
155154

156155
void s3dat_internal_extract_string(s3dat_t* handle, uint16_t text, uint16_t language, s3dat_ref_t** to, s3util_exception_t** throws);
157156
void s3dat_internal_extract_bitmap(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws);
@@ -162,8 +161,6 @@ void s3dat_pack_bitmap(s3dat_t* handle, s3dat_bitmap_t* bitmap, s3dat_content_ty
162161
void s3dat_pack_string(s3dat_t* handle, s3dat_string_t* string, s3dat_packed_t* packed, s3util_exception_t** throws);
163162
void s3dat_pack_sound(s3dat_t* handle, s3dat_sound_t* sound, s3dat_packed_t* packed, s3util_exception_t** throws);
164163

165-
void s3dat_internal_8b_to_native(s3dat_color_t* color, void* to, s3dat_color_type type);
166-
167164
void s3dat_unpack_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws);
168165
void s3dat_read_packed_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws);
169166
void s3dat_utf8_encoding_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws);

src/structure.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ s3dat_ref_t* s3dat_new_bitmap(s3dat_t* parent, uint16_t width, uint16_t height,
150150

151151
ref->data.bmp->width = width;
152152
ref->data.bmp->height = height;
153-
ref->data.bmp->type = s3dat_unknown_color;
154-
ref->data.bmp->data = s3util_alloc_func(s3dat_memset(parent), width*height*sizeof(s3dat_color_t), throws);
153+
ref->data.bmp->type = s3util_unknown_color;
154+
ref->data.bmp->data = s3util_alloc_func(s3dat_memset(parent), width*height*sizeof(s3util_color_t), throws);
155155
if(*throws != NULL) {
156156
s3dat_unref(ref);
157157
s3util_add_to_stack(s3dat_memset(parent), throws, __FILE__, __func__, __LINE__);

src/write.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void s3dat_pack_palette(s3dat_t* handle, s3dat_bitmap_t* palette, s3dat_packed_t
331331

332332
uint16_t* ptr16 = packed->data;
333333
for(uint32_t i = 0;i != pixel_count;i++) {
334-
s3dat_internal_8b_to_native(palette->data+i, ptr16+i, palette->type);
334+
s3util_internal_8b_to_native(palette->data + i, ptr16 + i, palette->type);
335335
ptr16[i] = s3util_le16(ptr16[i]);
336336
}
337337
}

0 commit comments

Comments
 (0)