circuitpython/shared-module/gifio/OnDiskGif.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

268 lines
8.4 KiB
C
Raw Permalink Normal View History

2023-02-04 17:44:29 -05:00
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Mark Komus
2023-02-04 17:44:29 -05:00
*
* 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.
*/
2023-02-12 12:17:34 -05:00
#include "shared-bindings/gifio/OnDiskGif.h"
2023-02-04 17:44:29 -05:00
#include "shared-bindings/displayio/Bitmap.h"
2023-04-18 20:44:03 -04:00
#include "shared-bindings/displayio/Palette.h"
2023-02-04 17:44:29 -05:00
#include <string.h>
#include "py/mperrno.h"
#include "py/runtime.h"
2023-03-22 21:19:09 -04:00
2023-02-04 17:44:29 -05:00
static int32_t GIFReadFile(GIFFILE *pFile, uint8_t *pBuf, int32_t iLen) {
uint32_t iBytesRead;
iBytesRead = iLen;
pyb_file_obj_t *f = pFile->fHandle;
// Note: If you read a file all the way to the last byte, seek() stops working
if ((pFile->iSize - pFile->iPos) < iLen) {
iBytesRead = pFile->iSize - pFile->iPos - 1; // <-- ugly work-around
}
if (iBytesRead <= 0) {
return 0;
}
UINT bytes_read;
if (f_read(&f->fp, pBuf, iBytesRead, &bytes_read) != FR_OK) {
mp_raise_OSError(MP_EIO);
}
pFile->iPos = f->fp.fptr;
return bytes_read;
} /* GIFReadFile() */
static int32_t GIFSeekFile(GIFFILE *pFile, int32_t iPosition) {
pyb_file_obj_t *f = pFile->fHandle;
f_lseek(&f->fp, iPosition);
pFile->iPos = f->fp.fptr;
return pFile->iPos;
} /* GIFSeekFile() */
static void GIFDraw(GIFDRAW *pDraw) {
// Called for every scan line of the image as it decodes
// The pixels delivered are the 8-bit native GIF output
// The palette is either RGB565 or the original 24-bit RGB values
// depending on the pixel type selected with gif.begin()
2023-04-20 19:26:04 -04:00
gifio_ondiskgif_t *ondiskgif = (gifio_ondiskgif_t *)pDraw->pUser;
displayio_bitmap_t *bitmap = ondiskgif->bitmap;
displayio_palette_t *palette = ondiskgif->palette;
2023-04-18 20:44:03 -04:00
// Update the palette if we have one in RGB888
if (palette != NULL) {
uint8_t *pPal = pDraw->pPalette24;
for (int p = 0; p < 256; p++) {
uint8_t r = *pPal++;
uint8_t g = *pPal++;
uint8_t b = *pPal++;
uint32_t color = (r << 16) + (g << 8) + b;
common_hal_displayio_palette_set_color(palette, p, color);
common_hal_displayio_palette_make_opaque(palette, p); // Transparency can change frame to frame
}
if (pDraw->ucHasTransparency) {
common_hal_displayio_palette_make_transparent(palette, pDraw->ucTransparent);
}
}
2023-02-04 17:44:29 -05:00
int iWidth = pDraw->iWidth;
if (iWidth + pDraw->iX > bitmap->width) {
iWidth = bitmap->width - pDraw->iX;
}
if (pDraw->iY + pDraw->y >= bitmap->height || pDraw->iX >= bitmap->width || iWidth < 1) {
return;
}
int32_t row_start = (pDraw->y + pDraw->iY) * bitmap->stride;
2023-02-05 11:54:04 -05:00
uint32_t *row = bitmap->data + row_start;
2023-02-04 17:44:29 -05:00
2023-02-05 11:54:04 -05:00
if (pDraw->ucDisposalMethod == 2) { // restore to background color
// Not supported currently. Need to reset the area the previous frame occupied
// to the background color before the previous frame was drawn
// See: https://github.com/bitbank2/AnimatedGIF/issues/3
// To workaround clear the gif.bitmap object yourself as required.
2023-02-05 11:54:04 -05:00
}
2023-02-04 17:44:29 -05:00
2023-04-18 20:44:03 -04:00
if (palette != NULL) {
uint8_t *s = pDraw->pPixels;
uint8_t *d = (uint8_t *)row;
d += pDraw->iX;
for (int x = 0; x < iWidth; x++)
{
2023-04-18 20:44:03 -04:00
*d++ = *s++;
}
} else {
2023-04-18 20:44:03 -04:00
// No palette writing RGB565_SWAPPED right to bitmap buffer
uint8_t *s = pDraw->pPixels;
;
uint16_t *d = (uint16_t *)row;
uint16_t *pPal;
pPal = (uint16_t *)pDraw->pPalette;
uint8_t c, ucTransparent = pDraw->ucTransparent;
d += pDraw->iX;
if (pDraw->ucHasTransparency == 1) {
for (int x = 0; x < iWidth; x++)
{
c = *s++;
if (c != ucTransparent) {
*d = pPal[c];
}
d++;
}
} else {
for (int x = 0; x < iWidth; x++)
{
c = *s++;
*d++ = pPal[c];
}
2023-02-05 11:54:04 -05:00
}
2023-02-04 17:44:29 -05:00
}
}
2023-04-18 20:44:03 -04:00
void common_hal_gifio_ondiskgif_construct(gifio_ondiskgif_t *self, pyb_file_obj_t *file, bool use_palette) {
2023-02-04 17:44:29 -05:00
self->file = file;
2023-04-18 20:44:03 -04:00
if (use_palette == true) {
GIF_begin(&self->gif, GIF_PALETTE_RGB888);
} else {
GIF_begin(&self->gif, GIF_PALETTE_RGB565_BE);
}
2023-02-04 17:44:29 -05:00
self->gif.iError = GIF_SUCCESS;
self->gif.pfnRead = GIFReadFile;
self->gif.pfnSeek = GIFSeekFile;
self->gif.pfnDraw = GIFDraw;
self->gif.pfnClose = NULL;
self->gif.pfnOpen = NULL;
self->gif.GIFFile.fHandle = self->file;
f_rewind(&self->file->fp);
self->gif.GIFFile.iSize = (int32_t)f_size(&self->file->fp);
int result = GIF_init(&self->gif);
if (result != 1) {
switch (self->gif.iError) {
case GIF_TOO_WIDE:
mp_raise_ValueError_varg(MP_ERROR_TEXT("%q must be <= %d"), MP_QSTR_width, MAX_WIDTH);
break;
default:
mp_arg_error_invalid(MP_QSTR_file);
break;
}
2023-02-04 17:44:29 -05:00
}
2023-04-18 20:44:03 -04:00
int bpp = 16;
if (use_palette == true) {
displayio_palette_t *palette = mp_obj_malloc(displayio_palette_t, &displayio_palette_type);
2023-04-18 20:44:03 -04:00
common_hal_displayio_palette_construct(palette, 256, false);
2023-04-20 19:26:04 -04:00
self->palette = palette;
2023-04-18 20:44:03 -04:00
bpp = 8;
} else {
2023-04-20 19:26:04 -04:00
self->palette = NULL;
2023-04-18 20:44:03 -04:00
}
displayio_bitmap_t *bitmap = mp_obj_malloc(displayio_bitmap_t, &displayio_bitmap_type);
2023-04-18 20:44:03 -04:00
common_hal_displayio_bitmap_construct(bitmap, self->gif.iCanvasWidth, self->gif.iCanvasHeight, bpp);
2023-04-20 19:26:04 -04:00
self->bitmap = bitmap;
2023-02-04 17:44:29 -05:00
2023-02-05 11:54:04 -05:00
GIFINFO info;
GIF_getInfo(&self->gif, &info);
self->duration = info.iDuration;
self->frame_count = info.iFrameCount;
self->min_delay = info.iMinDelay;
self->max_delay = info.iMaxDelay;
2023-03-22 21:19:09 -04:00
}
2023-02-04 17:44:29 -05:00
2023-03-11 10:44:35 -05:00
void common_hal_gifio_ondiskgif_deinit(gifio_ondiskgif_t *self) {
self->file = NULL;
2023-04-20 19:26:04 -04:00
common_hal_displayio_bitmap_deinit(self->bitmap);
self->bitmap = NULL;
self->palette = NULL;
2023-03-11 10:44:35 -05:00
}
bool common_hal_gifio_ondiskgif_deinited(gifio_ondiskgif_t *self) {
2023-04-20 19:26:04 -04:00
return self->bitmap == NULL;
2023-02-04 17:44:29 -05:00
}
2023-02-12 12:17:34 -05:00
uint16_t common_hal_gifio_ondiskgif_get_height(gifio_ondiskgif_t *self) {
2023-02-04 17:44:29 -05:00
return (uint16_t)self->gif.iCanvasHeight;
}
2023-02-12 12:17:34 -05:00
uint16_t common_hal_gifio_ondiskgif_get_width(gifio_ondiskgif_t *self) {
2023-02-04 17:44:29 -05:00
return (uint16_t)self->gif.iCanvasWidth;
}
2023-02-12 12:17:34 -05:00
mp_obj_t common_hal_gifio_ondiskgif_get_bitmap(gifio_ondiskgif_t *self) {
2023-04-20 19:26:04 -04:00
return MP_OBJ_FROM_PTR(self->bitmap);
2023-04-18 20:44:03 -04:00
}
mp_obj_t common_hal_gifio_ondiskgif_get_palette(gifio_ondiskgif_t *self) {
2023-04-20 19:26:04 -04:00
if (self->palette == NULL) {
return mp_const_none;
}
return MP_OBJ_FROM_PTR(self->palette);
2023-02-04 17:44:29 -05:00
}
2023-02-12 12:17:34 -05:00
int32_t common_hal_gifio_ondiskgif_get_duration(gifio_ondiskgif_t *self) {
2023-02-05 11:54:04 -05:00
return self->duration;
}
2023-02-12 12:17:34 -05:00
int32_t common_hal_gifio_ondiskgif_get_frame_count(gifio_ondiskgif_t *self) {
2023-02-05 11:54:04 -05:00
return self->frame_count;
}
2023-02-12 12:17:34 -05:00
int32_t common_hal_gifio_ondiskgif_get_min_delay(gifio_ondiskgif_t *self) {
2023-02-05 11:54:04 -05:00
return self->min_delay;
}
2023-02-12 12:17:34 -05:00
int32_t common_hal_gifio_ondiskgif_get_max_delay(gifio_ondiskgif_t *self) {
2023-02-05 11:54:04 -05:00
return self->max_delay;
}
uint32_t common_hal_gifio_ondiskgif_next_frame(gifio_ondiskgif_t *self, bool setDirty) {
2023-02-08 23:58:44 -05:00
int nextDelay = 0;
2023-04-18 20:44:03 -04:00
int result = 0;
2023-04-20 19:26:04 -04:00
result = GIF_playFrame(&self->gif, &nextDelay, self);
2023-02-04 17:44:29 -05:00
2023-02-08 23:58:44 -05:00
if ((result >= 0) && (setDirty)) {
2023-02-04 17:44:29 -05:00
displayio_area_t dirty_area = {
.x1 = 0,
.y1 = 0,
2023-04-20 19:26:04 -04:00
.x2 = self->bitmap->width,
.y2 = self->bitmap->height,
2023-02-04 17:44:29 -05:00
};
2023-04-20 19:26:04 -04:00
displayio_bitmap_set_dirty_area(self->bitmap, &dirty_area);
2023-02-04 17:44:29 -05:00
}
2023-02-08 23:58:44 -05:00
return nextDelay;
2023-02-04 17:44:29 -05:00
}