Skip to content

Commit dc00226

Browse files
committed
gifio: write block data directly into output buffer
1 parent b881aec commit dc00226

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

shared-module/gifio/GifWriter.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ void shared_module_gifio_gifwriter_add_frame(gifio_gifwriter_t *self, const mp_b
175175
int pixel_count = self->width * self->height;
176176
int blocks = (pixel_count + BLOCK_SIZE - 1) / BLOCK_SIZE;
177177

178-
uint8_t block_data[2 + BLOCK_SIZE];
179-
block_data[1] = 0x80;
178+
uint8_t *data = self->data + self->cur;
180179

181180
if (self->colorspace == DISPLAYIO_COLORSPACE_L8) {
182181
mp_get_index(&mp_type_memoryview, bufinfo->len, MP_OBJ_NEW_SMALL_INT(pixel_count - 1), false);
@@ -186,11 +185,11 @@ void shared_module_gifio_gifwriter_add_frame(gifio_gifwriter_t *self, const mp_b
186185
assert(pixel_count >= 0);
187186
int block_size = MIN(BLOCK_SIZE, pixel_count);
188187
pixel_count -= block_size;
189-
block_data[0] = 1 + block_size;
188+
*data++ = 1 + block_size;
189+
*data++ = 0x80;
190190
for (int j = 0; j < block_size; j++) {
191-
block_data[j + 2] = (*pixels++) >> 1;
191+
*data++ = (*pixels++) >> 1;
192192
}
193-
write_data(self, block_data, 2 + block_size);
194193
}
195194
} else {
196195
mp_get_index(&mp_type_memoryview, bufinfo->len, MP_OBJ_NEW_SMALL_INT(2 * pixel_count - 1), false);
@@ -200,7 +199,8 @@ void shared_module_gifio_gifwriter_add_frame(gifio_gifwriter_t *self, const mp_b
200199
int block_size = MIN(BLOCK_SIZE, pixel_count);
201200
pixel_count -= block_size;
202201

203-
block_data[0] = 1 + block_size;
202+
*data++ = 1 + block_size;
203+
*data++ = 0x80;
204204
for (int j = 0; j < block_size; j++) {
205205
int pixel = *pixels++;
206206
if (self->byteswap) {
@@ -209,12 +209,13 @@ void shared_module_gifio_gifwriter_add_frame(gifio_gifwriter_t *self, const mp_b
209209
int red = (pixel >> (11 + (5 - 2))) & 0x3;
210210
int green = (pixel >> (5 + (6 - 3))) & 0x7;
211211
int blue = (pixel >> (0 + (5 - 2))) & 0x3;
212-
block_data[j + 2] = (red << 5) | (green << 2) | blue;
212+
*data++ = (red << 5) | (green << 2) | blue;
213213
}
214-
write_data(self, block_data, 2 + block_size);
215214
}
216215
}
217216

217+
self->cur = data - self->data;
218+
218219
write_data(self, (uint8_t []) {0x01, 0x81, 0x00}, 3); // end code
219220
flush_data(self);
220221
handle_error(self);

0 commit comments

Comments
 (0)