forked from Plombo/palapply-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palapply.c
383 lines (338 loc) · 11 KB
/
palapply.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2018-2019 Bryan Cain
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Converts an RGBA png to an indexed image using a given palette, and creates an alpha
// mask if needed.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <png.h>
#include <zlib.h>
#include "SDL_image.h"
#include "palapply.h"
#ifndef _WIN32
#define stricmp strcasecmp
#endif
typedef struct {
uint32_t w;
uint32_t h;
bool hasAlphaChannel;
uint32_t *pixels;
} Image32;
static png_color pal[256];
static int pal_ncolors; // number of colors in palette (1-256)
static bool readPaletteFromACT(const char *path)
{
FILE *fp = fopen(path, "rb");
if (fp == NULL)
{
return false;
}
for (int i = 0; i < 256; i++)
{
if (fread(&pal[i].red, 1, 1, fp) != 1 ||
fread(&pal[i].green, 1, 1, fp) != 1 ||
fread(&pal[i].blue, 1, 1, fp) != 1)
{
fclose(fp);
return false;
}
}
pal_ncolors = 256;
fclose(fp);
return true;
}
static bool readPaletteFromImage(const char *path)
{
SDL_Surface *image = IMG_Load(path);
if (!image)
{
printf("Error: %s\n", SDL_GetError());
return false;
}
else if (!image->format->palette)
{
printf("Error: palette image %s is not indexed\n", path);
SDL_FreeSurface(image);
return false;
}
else
{
pal_ncolors = image->format->palette->ncolors;
for (int i = 0; i < pal_ncolors; i++)
{
pal[i].red = image->format->palette->colors[i].r;
pal[i].green = image->format->palette->colors[i].g;
pal[i].blue = image->format->palette->colors[i].b;
}
printf("read palette with %i colors from %s\n", pal_ncolors, path);
SDL_FreeSurface(image);
return true;
}
}
bool readPalette(const char *path)
{
bool result;
const char *extension = strrchr(path, '.');
if (extension != NULL && stricmp(extension, ".act") == 0)
{
result = readPaletteFromACT(path);
}
else
{
result = readPaletteFromImage(path);
}
// ACT and GIF palettes are always power-of-two sizes, so they're padded out with unused blacks or whites.
// PNG has no such limitation, so we can reduce the size of our output images by removing the padding from the
// palette with a simple algorithm: while the last two colors in the palette are the same, reduce the size of the
// palette by 1. This removes all but 1 of the padding entries while ensuring that no colors are lost.
if (result == true)
{
while (pal_ncolors > 2 && 0 == memcmp(&pal[pal_ncolors - 2], &pal[pal_ncolors - 1], 3))
{
--pal_ncolors;
}
}
return result;
}
SDL_Surface *readSourceImage(const char *path)
{
SDL_Surface *image = IMG_Load(path);
if (!image)
{
printf("Error: %s\n", SDL_GetError());
return NULL;
}
if (image->format->Amask)
{
printf("has alpha channel\n");
}
else
{
printf("no alpha channel\n");
}
SDL_Surface *image32 = SDL_CreateRGBSurface(0, image->w, image->h, 32,
0xFF, 0xFF00, 0xFF0000, image->format->Amask ? 0xFF000000 : 0);
SDL_SetSurfaceBlendMode(image, SDL_BLENDMODE_NONE);
SDL_BlitSurface(image, NULL, image32, NULL);
SDL_FreeSurface(image);
// If there's technically an "alpha channel" but every pixel is 100% opaque, there isn't really an alpha channel.
if (image32->format->Amask && alphaType(image32) == ALPHA_NONE)
{
image32->format->Amask = 0;
}
return image32;
}
// saves image as indexed PNG using nearest-color algorithm
bool saveIndexedPNG(const char *path, SDL_Surface *screen)
{
uint32_t *source;
int i, x, y;
png_structp png_ptr;
png_infop info_ptr;
FILE *fp;
uint8_t *line;
fp = fopen(path, "wb");
if (!fp) return false;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) return false;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return false;
}
png_init_io(png_ptr, fp);
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
png_set_IHDR(png_ptr, info_ptr, screen->w, screen->h,
8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_PLTE(png_ptr, info_ptr, pal, pal_ncolors);
png_write_info(png_ptr, info_ptr);
line = (uint8_t*) malloc(screen->w);
source = screen->pixels;
for (y = 0; y < screen->h; y++)
{
source = (uint32_t *)(screen->pixels + (y * screen->pitch));
for (i = 0, x = 0; x < screen->w; x++)
{
uint32_t color = 0;
uint8_t r = 0, g = 0, b = 0, a = 0, nearest = 1;
color = source[x];
r = color & 0xff;
g = (color >> 8) & 0xff;
b = (color >> 16) & 0xff;
a = (color >> 24) & 0xff;
if (screen->format->Amask && a == 0) nearest = 0;
else
{
int j;
int nearest_dist_sq = 9999999;
/* If the source has an alpha mask, don't use the transparent color (0) for any
* pixels that aren't completely transparent. */
for (j = screen->format->Amask ? 1 : 0; j < pal_ncolors; j++)
{
int rdist = r - pal[j].red;
int gdist = g - pal[j].green;
int bdist = b - pal[j].blue;
int dist_sq = rdist*rdist + gdist*gdist + bdist*bdist;
if (dist_sq < nearest_dist_sq)
{
nearest_dist_sq = dist_sq;
nearest = j;
}
}
}
line[i++] = nearest;
}
png_write_row(png_ptr, line);
}
free(line);
line = NULL;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
// saves alpha mask of image
bool saveMask(const char* filename, SDL_Surface *screen)
{
uint32_t *source;
int i, x, y;
png_structp png_ptr;
png_infop info_ptr;
FILE *fp;
uint8_t *line;
fp = fopen(filename, "wb");
if (!fp) return false;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) return false;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return false;
}
png_init_io(png_ptr, fp);
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
png_set_IHDR(png_ptr, info_ptr, screen->w, screen->h,
8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
line = (uint8_t*) malloc(screen->w);
for (y = 0; y < screen->h; y++)
{
source = (uint32_t *)(screen->pixels + (y * screen->pitch));
for (i = 0, x = 0; x < screen->w; x++)
{
uint32_t color = source[x];
uint8_t a = (color >> 24) & 0xff;
line[i++] = a;
}
png_write_row(png_ptr, line);
}
free(line);
line = NULL;
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}
// returns true if and only if alpha channel of img has at least one alpha value that isn't 0 or 255
AlphaType alphaType(SDL_Surface *img)
{
uint32_t x, y, *color;
AlphaType alphaType = ALPHA_NONE;
for (y = 0; y < img->h; y++)
{
color = img->pixels + (y * img->pitch);
for (x = 0; x < img->w; x++)
{
uint32_t alpha = ((*color) >> 24) & 0xff;
if (alpha == 0)
{
alphaType = ALPHA_SIMPLE;
}
else if (alpha != 255)
{
return ALPHA_MASK_NEEDED;
}
color++;
}
}
return alphaType;
}
int commandLineMain(int argc, char **argv)
{
if (argc != 4 && argc != 5) // alpha masking is optional
{
fprintf(stderr, "Usage: %s palette source result [result_mask]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "palette: an indexed PNG with the target palette\n");
fprintf(stderr, "source: the PNG to apply the palette to and generate the mask from;\n"
" can be RGB, indexed, or grayscale, with or without alpha\n");
fprintf(stderr, "result: path to which to save the resulting image as an indexed PNG\n");
fprintf(stderr, "result_mask: path to which to save the resulting alpha mask as a grayscale PNG\n");
fprintf(stderr, "\n");
fprintf(stderr, "The result_mask parameter can be omitted to skip producing an alpha mask.\n");
fprintf(stderr, "Note that result and result_mask will be overwritten if the paths already exist.\n");
return 1;
}
if (!readPalette(argv[1]))
{
fprintf(stderr, "error: failed to load palette image '%s'\n", argv[1]);
goto error;
}
SDL_Surface *img = readSourceImage(argv[2]);
if (img)
{
printf("read image %s\n", argv[2]);
}
else
{
fprintf(stderr, "error: failed to load image %s\n", argv[2]);
goto error;
}
if (!saveIndexedPNG(argv[3], img))
{
fprintf(stderr, "error: failed to save result '%s'\n", argv[3]);
goto error;
} else printf("saved result to '%s'\n", argv[3]);
if (img->format->Amask)
{
if (alphaType(img) == ALPHA_MASK_NEEDED)
{
if (argc < 5)
{
fprintf(stderr, "warning: source has non-trivial alpha, but no mask filename given");
}
else if (!saveMask(argv[4], img))
{
fprintf(stderr, "error: failed to save alpha mask '%s'\n", argv[4]);
goto error;
}
else printf("saved alpha mask to '%s'\n", argv[4]);
}
else printf("no alpha mask needed (simple alpha channel)\n");
} else printf("no alpha mask needed (source has no alpha channel)\n");
SDL_FreeSurface(img);
return 0;
error:
return 1;
}