Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Jeff Epler for Adafruit Industries
|
|
|
|
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
|
|
|
|
* Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/gc.h"
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "shared-module/gifio/GifWriter.h"
|
|
|
|
#include "shared-bindings/gifio/GifWriter.h"
|
|
|
|
#include "shared-bindings/displayio/ColorConverter.h"
|
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
|
|
|
|
#define BLOCK_SIZE (126) // (2^7) - 2 // (DO NOT CHANGE!)
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
static void handle_error(gifio_gifwriter_t *self) {
|
|
|
|
if (self->error != 0) {
|
|
|
|
mp_raise_OSError(self->error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_data(gifio_gifwriter_t *self) {
|
|
|
|
if (self->cur == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int error = 0;
|
|
|
|
self->file_proto->write(self->file, self->data, self->cur, &error);
|
|
|
|
self->cur = 0;
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
if (error != 0) {
|
2021-10-26 12:11:28 -04:00
|
|
|
self->error = error;
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
// These "write" calls _MUST_ have enough buffer space available! This is
|
|
|
|
// ensured by allocating the proper buffer size in construct.
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
static void write_data(gifio_gifwriter_t *self, const void *data, size_t size) {
|
2021-10-26 12:11:28 -04:00
|
|
|
assert(self->cur + size <= self->size);
|
|
|
|
memcpy(self->data + self->cur, data, size);
|
|
|
|
self->cur += size;
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_byte(gifio_gifwriter_t *self, uint8_t value) {
|
|
|
|
write_data(self, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_long(gifio_gifwriter_t *self, uint32_t value) {
|
|
|
|
write_data(self, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_word(gifio_gifwriter_t *self, uint16_t value) {
|
|
|
|
write_data(self, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_gifio_gifwriter_construct(gifio_gifwriter_t *self, mp_obj_t *file, int width, int height, displayio_colorspace_t colorspace, bool loop, bool own_file) {
|
|
|
|
self->file = file;
|
|
|
|
self->file_proto = mp_proto_get_or_throw(MP_QSTR_protocol_stream, file);
|
|
|
|
if (self->file_proto->is_text) {
|
|
|
|
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
|
|
|
|
}
|
|
|
|
self->width = width;
|
|
|
|
self->height = height;
|
|
|
|
self->colorspace = colorspace;
|
|
|
|
self->own_file = own_file;
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
size_t nblocks = (width * height + 125) / 126;
|
|
|
|
self->size = nblocks * 128 + 4;
|
|
|
|
self->data = gc_alloc(self->size, 0, false);
|
|
|
|
self->cur = 0;
|
|
|
|
self->error = 0;
|
|
|
|
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
write_data(self, "GIF89a", 6);
|
|
|
|
write_word(self, width);
|
|
|
|
write_word(self, height);
|
|
|
|
write_data(self, (uint8_t []) {0xF6, 0x00, 0x00}, 3);
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
switch (colorspace) {
|
|
|
|
case DISPLAYIO_COLORSPACE_RGB565:
|
|
|
|
case DISPLAYIO_COLORSPACE_RGB565_SWAPPED:
|
|
|
|
case DISPLAYIO_COLORSPACE_BGR565:
|
|
|
|
case DISPLAYIO_COLORSPACE_BGR565_SWAPPED:
|
|
|
|
case DISPLAYIO_COLORSPACE_L8:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mp_raise_TypeError(translate("unsupported colorspace for GifWriter"));
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool color = (colorspace != DISPLAYIO_COLORSPACE_L8);
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
bool bgr = (colorspace == DISPLAYIO_COLORSPACE_BGR565 || colorspace == DISPLAYIO_COLORSPACE_BGR565_SWAPPED);
|
|
|
|
self->byteswap = (colorspace == DISPLAYIO_COLORSPACE_RGB565_SWAPPED || colorspace == DISPLAYIO_COLORSPACE_BGR565_SWAPPED);
|
|
|
|
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
if (color) {
|
|
|
|
for (int i = 0; i < 128; i++) {
|
|
|
|
int red = (int)(((((i & 0x60) >> 5) * 255) + 1.5) / 3);
|
|
|
|
int green = (int)(((((i & 0x1C) >> 2) * 255) + 3.5) / 7);
|
|
|
|
int blue = (int)((((i & 0x3) * 255) + 1.5) / 3);
|
2021-10-26 12:11:28 -04:00
|
|
|
if (bgr) {
|
|
|
|
write_data(self, (uint8_t []) {blue, red, green}, 3);
|
|
|
|
} else {
|
|
|
|
write_data(self, (uint8_t []) {red, green, blue}, 3);
|
|
|
|
}
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 128; i++) {
|
|
|
|
int gray = (int)(((i * 255) + 63.5) / 127);
|
|
|
|
write_data(self, (uint8_t []) {gray, gray, gray}, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loop) {
|
|
|
|
write_data(self, (uint8_t []) {'!', 0xFF, 0x0B}, 3);
|
|
|
|
write_data(self, "NETSCAPE2.0", 11);
|
|
|
|
write_data(self, (uint8_t []) {0x03, 0x01, 0x00, 0x00, 0x00}, 5);
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
flush_data(self);
|
|
|
|
handle_error(self);
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool shared_module_gifio_gifwriter_deinited(gifio_gifwriter_t *self) {
|
|
|
|
return !self->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_gifio_gifwriter_check_for_deinit(gifio_gifwriter_t *self) {
|
|
|
|
if (shared_module_gifio_gifwriter_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_gifio_gifwriter_deinit(gifio_gifwriter_t *self) {
|
|
|
|
if (!shared_module_gifio_gifwriter_deinited(self)) {
|
|
|
|
shared_module_gifio_gifwriter_close(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_gifio_gifwriter_add_frame(gifio_gifwriter_t *self, const mp_buffer_info_t *bufinfo, int16_t delay) {
|
|
|
|
if (delay) {
|
|
|
|
write_data(self, (uint8_t []) {'!', 0xF9, 0x04, 0x04}, 4);
|
|
|
|
write_word(self, delay);
|
|
|
|
write_word(self, 0); // end
|
|
|
|
}
|
|
|
|
|
|
|
|
write_byte(self, 0x2C);
|
|
|
|
write_long(self, 0);
|
|
|
|
write_word(self, self->width);
|
|
|
|
write_word(self, self->height);
|
|
|
|
write_data(self, (uint8_t []) {0x00, 0x07}, 2); // 7-bits
|
|
|
|
|
|
|
|
int pixel_count = self->width * self->height;
|
|
|
|
int blocks = (pixel_count + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
|
|
|
|
|
|
|
uint8_t block_data[2 + BLOCK_SIZE];
|
|
|
|
block_data[1] = 0x80;
|
|
|
|
|
|
|
|
if (self->colorspace == DISPLAYIO_COLORSPACE_L8) {
|
|
|
|
mp_get_index(&mp_type_memoryview, bufinfo->len, MP_OBJ_NEW_SMALL_INT(pixel_count - 1), false);
|
|
|
|
|
|
|
|
uint8_t *pixels = bufinfo->buf;
|
|
|
|
for (int i = 0; i < blocks; i++) {
|
|
|
|
assert(pixel_count >= 0);
|
|
|
|
int block_size = MIN(BLOCK_SIZE, pixel_count);
|
|
|
|
pixel_count -= block_size;
|
|
|
|
block_data[0] = 1 + block_size;
|
|
|
|
for (int j = 0; j < block_size; j++) {
|
|
|
|
block_data[j + 2] = (*pixels++) >> 1;
|
|
|
|
}
|
|
|
|
write_data(self, block_data, 2 + block_size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mp_get_index(&mp_type_memoryview, bufinfo->len, MP_OBJ_NEW_SMALL_INT(2 * pixel_count - 1), false);
|
|
|
|
|
|
|
|
uint16_t *pixels = bufinfo->buf;
|
|
|
|
for (int i = 0; i < blocks; i++) {
|
|
|
|
int block_size = MIN(BLOCK_SIZE, pixel_count);
|
|
|
|
pixel_count -= block_size;
|
|
|
|
|
|
|
|
block_data[0] = 1 + block_size;
|
|
|
|
for (int j = 0; j < block_size; j++) {
|
2021-10-26 12:11:28 -04:00
|
|
|
int pixel = *pixels++;
|
|
|
|
if (self->byteswap) {
|
|
|
|
pixel = __builtin_bswap16(pixel);
|
|
|
|
}
|
|
|
|
int red = (pixel >> (11 + (5 - 2))) & 0x3;
|
|
|
|
int green = (pixel >> (5 + (6 - 3))) & 0x7;
|
|
|
|
int blue = (pixel >> (0 + (5 - 2))) & 0x3;
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
block_data[j + 2] = (red << 5) | (green << 2) | blue;
|
|
|
|
}
|
|
|
|
write_data(self, block_data, 2 + block_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_data(self, (uint8_t []) {0x01, 0x81, 0x00}, 3); // end code
|
2021-10-26 12:11:28 -04:00
|
|
|
flush_data(self);
|
|
|
|
handle_error(self);
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void shared_module_gifio_gifwriter_close(gifio_gifwriter_t *self) {
|
2021-10-26 12:11:28 -04:00
|
|
|
write_byte(self, ';');
|
|
|
|
flush_data(self);
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
int error = 0;
|
|
|
|
self->file_proto->ioctl(self->file, self->own_file ? MP_STREAM_CLOSE : MP_STREAM_FLUSH, 0, &error);
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
self->file = NULL;
|
|
|
|
|
2021-10-26 12:11:28 -04:00
|
|
|
if (error != 0) {
|
|
|
|
self->error = error;
|
|
|
|
}
|
|
|
|
handle_error(self);
|
Implement gifio.GifWriter
This involves:
* Adding a new "L8" colorspace for colorconverters
* factoring out displayio_colorconverter_convert_pixel
* Making a minimal "colorspace only" version of displayio for the
unix port (testing purposes)
* fixing an error message
I only tested writing B&W animated images, with the following script:
```python
import displayio
import gifio
with gifio.GifWriter("foo.gif", 64, 64, displayio.Colorspace.L8) as g:
for i in range(0, 256, 14):
data = bytes([i, 255-i] * 32 + [255-i, i] * 32) * 32
print("add_frame")
g.add_frame(data)
# expected to raise an error, buffer is not big enough
with gifio.GifWriter("/dev/null", 64, 64, displayio.Colorspace.L8) as g:
g.add_frame(bytes([3,3,3]))
```
2021-10-21 12:45:11 -04:00
|
|
|
}
|