Skip to content

Commit 60b51a0

Browse files
committed
Miscellaneous non-QOI refactoring
1 parent a8bcef2 commit 60b51a0

File tree

6 files changed

+36
-54
lines changed

6 files changed

+36
-54
lines changed

src/bmpwrite.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -333,22 +333,22 @@ void generate_BMP_RGB_data (struct context * context, unsigned char * offset_poi
333333
if ((context -> source -> color_format & PLUM_COLOR_MASK) == PLUM_COLOR_32)
334334
data = context -> source -> data;
335335
else {
336-
data = ctxmalloc(context, sizeof *data * context -> source -> width * context -> source -> height);
337-
plum_convert_colors(data, context -> source -> data, (size_t) context -> source -> width * context -> source -> height,
338-
PLUM_COLOR_32, context -> source -> color_format);
336+
size_t size = (size_t) context -> source -> width * context -> source -> height;
337+
data = ctxmalloc(context, sizeof *data * size);
338+
plum_convert_colors(data, context -> source -> data, size, PLUM_COLOR_32, context -> source -> color_format);
339339
}
340340
size_t rowsize = (size_t) context -> source -> width * 3, padding = 0;
341341
if (rowsize & 3) {
342342
padding = 4 - (rowsize & 3);
343343
rowsize += padding;
344344
}
345-
unsigned char * out = append_output_node(context, rowsize * context -> source -> height);
345+
unsigned char * output = append_output_node(context, rowsize * context -> source -> height);
346346
uint_fast32_t row = context -> source -> height - 1;
347347
do {
348348
size_t pos = (size_t) row * context -> source -> width;
349349
for (uint_fast32_t remaining = context -> source -> width; remaining; pos ++, remaining --)
350-
out += byteappend(out, data[pos] >> 16, data[pos] >> 8, data[pos]);
351-
for (uint_fast32_t p = 0; p < padding; p ++) *(out ++) = 0;
350+
output += byteappend(output, data[pos] >> 16, data[pos] >> 8, data[pos]);
351+
for (uint_fast32_t p = 0; p < padding; p ++) *(output ++) = 0;
352352
} while (row --);
353353
if (data != context -> source -> data) ctxfree(context, data);
354354
}

src/framebounds.c

+26-41
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ struct plum_rectangle * get_frame_boundaries (struct context * context, bool anc
2121

2222
void adjust_frame_boundaries (const struct plum_image * image, struct plum_rectangle * restrict boundaries) {
2323
uint64_t empty_color = get_empty_color(image);
24+
#define checkframe(type) do \
25+
for (uint_fast32_t frame = 0; frame < image -> frames; frame ++) { \
26+
for (size_t remaining = (size_t) boundaries[frame].top * image -> width; remaining; remaining --) \
27+
if (notempty(type)) goto done ## type; \
28+
if (boundaries[frame].left || boundaries[frame].width != image -> width) \
29+
for (uint_fast32_t row = 0; row < boundaries[frame].height; row ++) { \
30+
for (uint_fast32_t col = 0; col < boundaries[frame].left; col ++) if (notempty(type)) goto done ## type; \
31+
index += boundaries[frame].width; \
32+
for (uint_fast32_t col = boundaries[frame].left + boundaries[frame].width; col < image -> width; col ++) \
33+
if (notempty(type)) goto done ## type; \
34+
} \
35+
else \
36+
index += (size_t) boundaries[frame].height * image -> width; \
37+
for (size_t remaining = (size_t) (image -> height - boundaries[frame].top - boundaries[frame].height) * image -> width; remaining; remaining --) \
38+
if (notempty(type)) goto done ## type; \
39+
continue; \
40+
done ## type: \
41+
boundaries[frame] = (struct plum_rectangle) {.left = 0, .top = 0, .width = image -> width, .height = image -> height}; \
42+
} \
43+
while (false)
2444
if (image -> palette) {
2545
bool empty[256];
2646
switch (image -> color_format & PLUM_COLOR_MASK) {
@@ -34,55 +54,20 @@ void adjust_frame_boundaries (const struct plum_image * image, struct plum_recta
3454
for (size_t p = 0; p <= image -> max_palette_index; p ++) empty[p] = image -> palette32[p] == empty_color;
3555
}
3656
size_t index = 0;
37-
for (uint_fast32_t frame = 0; frame < image -> frames; frame ++) {
38-
bool adjust = true;
39-
for (size_t remaining = (size_t) boundaries[frame].top * image -> width; remaining; remaining --)
40-
if (!empty[image -> data8[index ++]]) goto paldone;
41-
if (boundaries[frame].left || boundaries[frame].width != image -> width)
42-
for (uint_fast32_t row = 0; row < boundaries[frame].height; row ++) {
43-
for (uint_fast32_t col = 0; col < boundaries[frame].left; col ++) if (!empty[image -> data8[index ++]]) goto paldone;
44-
index += boundaries[frame].width;
45-
for (uint_fast32_t col = boundaries[frame].left + boundaries[frame].width; col < image -> width; col ++)
46-
if (!empty[image -> data8[index ++]]) goto paldone;
47-
}
48-
else
49-
index += (size_t) boundaries[frame].height * image -> width;
50-
for (size_t remaining = (size_t) (image -> height - boundaries[frame].top - boundaries[frame].height) * image -> width; remaining; remaining --)
51-
if (!empty[image -> data8[index ++]]) goto paldone;
52-
adjust = false;
53-
paldone:
54-
if (adjust) boundaries[frame] = (struct plum_rectangle) {.left = 0, .top = 0, .width = image -> width, .height = image -> height};
55-
}
57+
#define notempty(...) (!empty[image -> data8[index ++]])
58+
checkframe(pal);
5659
} else {
5760
size_t index = 0;
58-
#define checkframe(bits) do \
59-
for (uint_fast32_t frame = 0; frame < image -> frames; frame ++) { \
60-
bool adjust = true; \
61-
for (size_t remaining = (size_t) boundaries[frame].top * image -> width; remaining; remaining --) \
62-
if (image -> data ## bits[index ++] != empty_color) goto done ## bits; \
63-
if (boundaries[frame].left || boundaries[frame].width != image -> width) \
64-
for (uint_fast32_t row = 0; row < boundaries[frame].height; row ++) { \
65-
for (uint_fast32_t col = 0; col < boundaries[frame].left; col ++) if (image -> data ## bits[index ++] != empty_color) goto done ## bits; \
66-
index += boundaries[frame].width; \
67-
for (uint_fast32_t col = boundaries[frame].left + boundaries[frame].width; col < image -> width; col ++) \
68-
if (image -> data ## bits[index ++] != empty_color) goto done ## bits; \
69-
} \
70-
else \
71-
index += (size_t) boundaries[frame].height * image -> width; \
72-
for (size_t remaining = (size_t) (image -> height - boundaries[frame].top - boundaries[frame].height) * image -> width; remaining; remaining --) \
73-
if (image -> data ## bits[index ++] != empty_color) goto done ## bits; \
74-
adjust = false; \
75-
done ## bits: \
76-
if (adjust) boundaries[frame] = (struct plum_rectangle) {.left = 0, .top = 0, .width = image -> width, .height = image -> height}; \
77-
} \
78-
while (false)
61+
#undef notempty
62+
#define notempty(bits) (image -> data ## bits[index ++] != empty_color)
7963
switch (image -> color_format & PLUM_COLOR_MASK) {
8064
case PLUM_COLOR_16: checkframe(16); break;
8165
case PLUM_COLOR_64: checkframe(64); break;
8266
default: checkframe(32);
8367
}
84-
#undef checkframe
8568
}
69+
#undef notempty
70+
#undef checkframe
8671
}
8772

8873
bool image_rectangles_have_transparency (const struct plum_image * image, const struct plum_rectangle * rectangles) {

src/gifwrite.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ void generate_GIF_data (struct context * context) {
1212
if ((uint8_t) (depth >> 8) > overall) overall = depth >> 8;
1313
if ((uint8_t) (depth >> 16) > overall) overall = depth >> 16;
1414
if (overall > 8) overall = 8;
15-
header[4] = (overall - 1) << 4;
16-
header[5] = header[6] = 0;
15+
bytewrite(header + 4, (overall - 1) << 4, 0, 0);
1716
if (context -> source -> palette)
1817
generate_GIF_data_with_palette(context, header);
1918
else

src/inline.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static inline uint16_t bitextend16 (uint16_t value, unsigned width) {
4646
static inline void * append_output_node (struct context * context, size_t size) {
4747
struct data_node * node = ctxmalloc(context, sizeof *node + size);
4848
*node = (struct data_node) {.size = size, .previous = context -> output, .next = NULL};
49-
if (context -> output) context -> output -> next = node;
49+
if (node -> previous) node -> previous -> next = node;
5050
context -> output = node;
5151
return node -> data;
5252
}

src/pngcompress.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ size_t compute_uncompressed_PNG_block_size (const unsigned char * restrict data,
109109
if (length) {
110110
score += length - 1;
111111
if (score >= 16) break;
112-
} else if (score > 0)
112+
} else if (score)
113113
score --;
114114
append_PNG_reference(data, current_offset, references);
115115
}

src/pngwrite.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ void append_PNG_header_chunks (struct context * context, unsigned type, uint32_t
9696
unsigned char header[13];
9797
write_be32_unaligned(header, context -> image -> width);
9898
write_be32_unaligned(header + 4, context -> image -> height);
99-
header[8] = (type < 4) ? 1 << type : (8 << (type >= 6));
100-
header[9] = (type >= 4) ? 2 + 4 * (type & 1) : 3;
101-
bytewrite(header + 10, 0, 0, 0);
99+
bytewrite(header + 8, (type < 4) ? 1 << type : (8 << (type >= 6)), (type >= 4) ? 2 + 4 * (type & 1) : 3, 0, 0, 0);
102100
output_PNG_chunk(context, 0x49484452u, sizeof header, header); // IHDR
103101
unsigned char depthdata[4];
104102
write_le32_unaligned(depthdata, depth); // this will write each byte of depth in the expected position

0 commit comments

Comments
 (0)