| 1 |
// Copyright (c) 1993-2011 PrBoom developers (see AUTHORS) |
| 2 |
// Licence: GPLv2 or later (see COPYING) |
| 3 |
|
| 4 |
// Convert portable pixmap to Doom patch format |
| 5 |
|
| 6 |
#include "rd_graphic.h" |
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "rd_palette.h" |
| 13 |
#include "rd_util.h" |
| 14 |
|
| 15 |
// |
| 16 |
// parseppm |
| 17 |
// |
| 18 |
// try to read a ppm file header and get width/height of image |
| 19 |
// |
| 20 |
|
| 21 |
static unsigned char *parseppm(char *ppm, size_t size, const char *file, |
| 22 |
int *width, int *height) |
| 23 |
{ |
| 24 |
int maxcol, numpixels; |
| 25 |
char *pos = ppm; |
| 26 |
|
| 27 |
if (size < 2 || !(*pos++ == 'P' && *pos++ == '6')) |
| 28 |
die("Not a PPM: %s\n", file); |
| 29 |
|
| 30 |
// Ignore comments like "# Created by GIMP ..." in line 2 |
| 31 |
if (*pos++ == '\n' && *pos == '#') |
| 32 |
while (*pos != '\n') pos++; |
| 33 |
|
| 34 |
numpixels = *width = strtol(pos, &pos, 0); |
| 35 |
numpixels *= *height = strtol(pos, &pos, 0); |
| 36 |
maxcol = strtol(pos, &pos, 0); |
| 37 |
|
| 38 |
if (maxcol != 255 || !isspace(*pos++)) |
| 39 |
die("Invalid PPM header: %s\n", file); |
| 40 |
|
| 41 |
if (!numpixels || numpixels > (size-(ppm-pos))/3) |
| 42 |
die("Invalid PPM size: %s\n", file); |
| 43 |
|
| 44 |
return (unsigned char *)pos; |
| 45 |
} |
| 46 |
|
| 47 |
// |
| 48 |
// column_pixels_to_colours |
| 49 |
// |
| 50 |
// make an array of colour indices from a column of raw pixel data |
| 51 |
// (random access to which is easier while building a patch column) |
| 52 |
// |
| 53 |
|
| 54 |
static void pixels_to_colours(int *colours, unsigned char *pixels, |
| 55 |
int width, int height, int x) |
| 56 |
{ |
| 57 |
int i = height; |
| 58 |
int *colour = colours; |
| 59 |
unsigned char *rgb = &pixels[3*x]; |
| 60 |
|
| 61 |
while (--i>=0) |
| 62 |
{ |
| 63 |
*colour = palette_getindex(rgb); |
| 64 |
colour++; |
| 65 |
rgb += 3*width; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// |
| 70 |
// createcolumn |
| 71 |
// |
| 72 |
// make a doom patch column from an array of colour indices |
| 73 |
// |
| 74 |
|
| 75 |
static size_t createcolumn(unsigned char **column, int *colours, int height) |
| 76 |
{ |
| 77 |
size_t size = 256, length = 0; |
| 78 |
unsigned char *data = xmalloc(size); |
| 79 |
int i, y, top, transparent, opaque; |
| 80 |
|
| 81 |
for (y = 0; y < height; ) |
| 82 |
{ |
| 83 |
// we are at the start of a new post |
| 84 |
top = y; |
| 85 |
|
| 86 |
// count transparent pixels above post and opaque pixels in it |
| 87 |
transparent = opaque = 0; |
| 88 |
for (; y < height && colours[y] == -1; y++) transparent++; |
| 89 |
for (; y < height && colours[y] >= 0; y++) opaque++; |
| 90 |
|
| 91 |
if (opaque > 0) // this post has pixels |
| 92 |
{ |
| 93 |
while (size < length + opaque + 8) |
| 94 |
size *= 2, data = xrealloc(data, size); |
| 95 |
|
| 96 |
data[length++] = top + transparent; // column offset |
| 97 |
data[length++] = opaque; // length of column |
| 98 |
data[length++] = 0; // padding |
| 99 |
|
| 100 |
for (i = 0; i < opaque; i++) |
| 101 |
data[length++] = colours[top + transparent + i]; |
| 102 |
|
| 103 |
data[length++] = 0; // more padding |
| 104 |
} |
| 105 |
} |
| 106 |
data[length++] = 0xff; // end of column |
| 107 |
|
| 108 |
*column = data; |
| 109 |
return length; |
| 110 |
} |
| 111 |
|
| 112 |
// |
| 113 |
// ppm_to_patch |
| 114 |
// |
| 115 |
// convert an 8-bit portable pixmap to doom's patch format |
| 116 |
// insert_x and insert_y are the graphic/sprite insertion point |
| 117 |
// |
| 118 |
|
| 119 |
size_t ppm_to_patch(void **lumpdata, const char *filename, |
| 120 |
int insert_x, int insert_y) |
| 121 |
{ |
| 122 |
void *data; |
| 123 |
size_t size = read_or_die(&data, filename); |
| 124 |
|
| 125 |
int i, width, height, *column_colours; |
| 126 |
unsigned char *pixels, **columns, *patch; |
| 127 |
size_t *columnsizes, totalcolumnsize, offset; |
| 128 |
|
| 129 |
pixels = parseppm(data, size, filename, &width, &height); |
| 130 |
columns = xmalloc(width * sizeof(*columns)); |
| 131 |
columnsizes = xmalloc(width * sizeof(*columnsizes)); |
| 132 |
column_colours = xmalloc(height * sizeof(*column_colours)); |
| 133 |
|
| 134 |
for (totalcolumnsize = i = 0; i < width; i++) |
| 135 |
{ |
| 136 |
pixels_to_colours(column_colours, pixels, width, height, i); |
| 137 |
columnsizes[i] = createcolumn(&columns[i], column_colours, height); |
| 138 |
totalcolumnsize += columnsizes[i]; |
| 139 |
} |
| 140 |
|
| 141 |
patch = xmalloc(8+4*width+totalcolumnsize); |
| 142 |
|
| 143 |
((short *)patch)[0] = SHORT(width); |
| 144 |
((short *)patch)[1] = SHORT(height); |
| 145 |
((short *)patch)[2] = SHORT(insert_x); |
| 146 |
((short *)patch)[3] = SHORT(insert_y); |
| 147 |
|
| 148 |
for (offset = 8+4*width, i = 0; i < width; i++) |
| 149 |
{ |
| 150 |
((int *)(patch+8))[i] = LONG(offset); |
| 151 |
offset += columnsizes[i]; |
| 152 |
} |
| 153 |
|
| 154 |
for (offset = 8+4*width, i = 0; i < width; i++) |
| 155 |
{ |
| 156 |
memmove(patch + offset, columns[i], columnsizes[i]); |
| 157 |
offset += columnsizes[i]; |
| 158 |
free(columns[i]); |
| 159 |
} |
| 160 |
|
| 161 |
free(column_colours); |
| 162 |
free(columnsizes); |
| 163 |
free(columns); |
| 164 |
free(data); |
| 165 |
|
| 166 |
*lumpdata = patch; |
| 167 |
return 8+4*width+totalcolumnsize; |
| 168 |
} |
| 169 |
|
| 170 |
// |
| 171 |
// ppm_to_bitmap |
| 172 |
// |
| 173 |
// convert an 8-bit PPM to a raw map of Doom palette indices (for flats) |
| 174 |
// |
| 175 |
|
| 176 |
size_t ppm_to_bitmap(void **lumpdata, const char *filename) |
| 177 |
{ |
| 178 |
void *data; |
| 179 |
size_t size = read_or_die(&data, filename); |
| 180 |
|
| 181 |
int i, j, width, height; |
| 182 |
unsigned char *pixels, *bitmap; |
| 183 |
|
| 184 |
pixels = parseppm(data, size, filename, &width, &height); |
| 185 |
bitmap = xmalloc(width * height); |
| 186 |
|
| 187 |
for (j = 0; j < height; j++) |
| 188 |
for (i = 0; i < width; i++) |
| 189 |
bitmap[width*j+i] = palette_getindex(&pixels[3*(width*j+i)]); |
| 190 |
|
| 191 |
free(data); |
| 192 |
|
| 193 |
*lumpdata = bitmap; |
| 194 |
return width * height; |
| 195 |
} |