roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
PNGdec.h
Go to the documentation of this file.
1//
2// PNG Decoder
3//
4// written by Larry Bank
5// bitbank@pobox.com
6// Arduino port started 5/3/2021
7// Original PNG code written 20+ years ago :)
8// The goal of this code is to decode PNG images on embedded systems
9//
10// Copyright 2021 BitBank Software, Inc. All Rights Reserved.
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14// http://www.apache.org/licenses/LICENSE-2.0
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//===========================================================================
21//
22#ifndef __PNGDEC__
23#define __PNGDEC__
24#if defined(__MACH__) || defined(__LINUX__) || defined(__MCUXPRESSO) || \
25 defined(ESP_PLATFORM)
26#include <stdlib.h>
27#include <string.h>
28#include <stdint.h>
29#include <stdio.h>
30#define memcpy_P memcpy
31#define PROGMEM
32#elif defined(ARDUINO)
33#include <Arduino.h>
34#endif
35#include "zutil.h"
36#include "inftrees.h"
37#include "inflate.h"
38
39//
40// PNG Decoder
41// Written by Larry Bank
42// Copyright (c) 2021 BitBank Software, Inc.
43//
44// Designed to decode most PNG images (1-32 bpp)
45// using less than 40K of RAM
46//
47#ifndef FALSE
48#define FALSE 0
49#define TRUE 1
50#endif
51/* Defines and variables */
52#define PNG_FILE_BUF_SIZE 2048
53// Number of bytes to reserve for current and previous lines
54// Defaults to 640 32-bit pixels max width
55#define PNG_MAX_BUFFERED_PIXELS (640*4 + 1)
56// PNG filter type
57enum {
64};
65
66// decode options
67enum {
70};
71
72// source pixel type
73enum {
79};
80// RGB565 endianness
81enum {
84};
85
86enum {
89};
90
91// Error codes returned by getLastError()
92enum {
102
103typedef struct png_draw_tag
104{
105 int y; // starting x,y of this line
106 int iWidth; // size of this line
107 int iPitch; // bytes per line
108 int iPixelType; // PNG pixel type (0,2,3,4,6)
109 int iBpp; // bits per color stimulus
110 int iHasAlpha; // flag indicating the presence of an alpha palette
111 void *pUser; // user supplied pointer
113 uint16_t *pFastPalette;
114 uint8_t *pPixels;
116
117typedef struct png_file_tag
118{
119 int32_t iPos; // current file position
120 int32_t iSize; // file size
121 uint8_t *pData; // memory file pointer
122 void * fHandle; // class pointer to File/SdFat or whatever you want
124
125// Callback function prototypes
126typedef int32_t (PNG_READ_CALLBACK)(PNGFILE *pFile, uint8_t *pBuf, int32_t iLen);
127typedef int32_t (PNG_SEEK_CALLBACK)(PNGFILE *pFile, int32_t iPosition);
128typedef void * (PNG_OPEN_CALLBACK)(const char *szFilename, int32_t *pFileSize);
129typedef void (PNG_DRAW_CALLBACK)(PNGDRAW *);
130typedef void (PNG_CLOSE_CALLBACK)(void *pHandle);
131
132//
133// our private structure to hold a JPEG image decode state
134//
135typedef struct png_image_tag
136{
137 int iWidth, iHeight; // image size
139 uint8_t ucMemType;
140 uint8_t *pImage;
141 int iPitch; // bytes per line
144 uint32_t iTransparent; // transparent color index/value
152 uint8_t ucZLIB[32768 + sizeof(inflate_state)]; // put this here to avoid needing malloc/free
155 uint8_t ucFileBuf[PNG_FILE_BUF_SIZE]; // holds temp file data
157
158#ifdef __cplusplus
159#define PNG_STATIC static
160//
161// The PNG class wraps portable C code which does the actual work
162//
163class PNG
164{
165 public:
166 int openRAM(uint8_t *pData, int iDataSize, PNG_DRAW_CALLBACK *pfnDraw);
167 int openFLASH(uint8_t *pData, int iDataSize, PNG_DRAW_CALLBACK *pfnDraw);
168 int open(const char *szFilename, PNG_OPEN_CALLBACK *pfnOpen, PNG_CLOSE_CALLBACK *pfnClose, PNG_READ_CALLBACK *pfnRead, PNG_SEEK_CALLBACK *pfnSeek, PNG_DRAW_CALLBACK *pfnDraw);
169 void close();
170 int decode(void *pUser, int iOptions);
171 int getWidth();
172 int getHeight();
173 int getBpp();
174 int hasAlpha();
175 uint32_t getTransparentColor();
176 int isInterlaced();
177 uint8_t * getPalette();
178 int getPixelType();
179 int getLastError();
180 int getBufferSize();
181 uint8_t *getBuffer();
182 void setBuffer(uint8_t *pBuffer);
183 uint8_t getAlphaMask(PNGDRAW *pDraw, uint8_t *pMask, uint8_t ucThreshold);
184 void getLineAsRGB565(PNGDRAW *pDraw, uint16_t *pPixels, int iEndianness, uint32_t u32Bkgd);
185
186 private:
187 PNGIMAGE _png;
188};
189#else
190#define PNG_STATIC
191int PNG_openRAM(PNGIMAGE *pPNG, uint8_t *pData, int iDataSize);
192int PNG_openFile(PNGIMAGE *pPNG, const char *szFilename);
195int PNG_decode(PNGIMAGE *pPNG, void *pUser, int iOptions);
196void PNG_close(PNGIMAGE *pPNG);
199int PNG_getLastError(PNGIMAGE *pPNG);
201uint8_t *PNG_getPalette(PNGIMAGE *pPNG);
205uint8_t *PNG_getBuffer(PNGIMAGE *pPNG);
206void PNG_setBuffer(PNGIMAGE *pPNG, uint8_t *pBuffer);
207#endif // __cplusplus
208
209// Due to unaligned memory causing an exception, we have to do these macros the slow way
210#define INTELSHORT(p) ((*p) + (*(p+1)<<8))
211#define INTELLONG(p) ((*p) + (*(p+1)<<8) + (*(p+2)<<16) + (*(p+3)<<24))
212#define MOTOSHORT(p) (((*(p))<<8) + (*(p+1)))
213#define MOTOLONG(p) (((*p)<<24) + ((*(p+1))<<16) + ((*(p+2))<<8) + (*(p+3)))
214
215// Must be a 32-bit target processor
216#define REGISTER_WIDTH 32
217
218#endif // __PNGDEC__
@ PNG_FILTER_AVG
Definition PNGdec.h:61
@ PNG_FILTER_PAETH
Definition PNGdec.h:62
@ PNG_FILTER_COUNT
Definition PNGdec.h:63
@ PNG_FILTER_NONE
Definition PNGdec.h:58
@ PNG_FILTER_UP
Definition PNGdec.h:60
@ PNG_FILTER_SUB
Definition PNGdec.h:59
struct png_draw_tag PNGDRAW
int PNG_getBpp(PNGIMAGE *pPNG)
int PNG_isInterlaced(PNGIMAGE *pPNG)
void PNG_close(PNGIMAGE *pPNG)
int PNG_openRAM(PNGIMAGE *pPNG, uint8_t *pData, int iDataSize)
struct png_file_tag PNGFILE
void() PNG_DRAW_CALLBACK(PNGDRAW *)
Definition PNGdec.h:129
uint8_t * PNG_getBuffer(PNGIMAGE *pPNG)
@ PNG_UNSUPPORTED_FEATURE
Definition PNGdec.h:98
@ PNG_TOO_BIG
Definition PNGdec.h:100
@ PNG_INVALID_FILE
Definition PNGdec.h:99
@ PNG_MEM_ERROR
Definition PNGdec.h:96
@ PNG_SUCCESS
Definition PNGdec.h:93
@ PNG_INVALID_PARAMETER
Definition PNGdec.h:94
@ PNG_NO_BUFFER
Definition PNGdec.h:97
@ PNG_DECODE_ERROR
Definition PNGdec.h:95
#define PNG_MAX_BUFFERED_PIXELS
Definition PNGdec.h:55
void *() PNG_OPEN_CALLBACK(const char *szFilename, int32_t *pFileSize)
Definition PNGdec.h:128
int PNG_getLastError(PNGIMAGE *pPNG)
@ PNG_PIXEL_TRUECOLOR
Definition PNGdec.h:75
@ PNG_PIXEL_GRAY_ALPHA
Definition PNGdec.h:77
@ PNG_PIXEL_GRAYSCALE
Definition PNGdec.h:74
@ PNG_PIXEL_INDEXED
Definition PNGdec.h:76
@ PNG_PIXEL_TRUECOLOR_ALPHA
Definition PNGdec.h:78
int PNG_decode(PNGIMAGE *pPNG, void *pUser, int iOptions)
void PNG_setBuffer(PNGIMAGE *pPNG, uint8_t *pBuffer)
struct png_image_tag PNGIMAGE
#define PNG_FILE_BUF_SIZE
Definition PNGdec.h:52
@ PNG_RGB565_LITTLE_ENDIAN
Definition PNGdec.h:82
@ PNG_RGB565_BIG_ENDIAN
Definition PNGdec.h:83
int PNG_getBufferSize(PNGIMAGE *pPNG)
int PNG_hasAlpha(PNGIMAGE *pPNG)
int PNG_getPixelType(PNGIMAGE *pPNG)
int32_t() PNG_READ_CALLBACK(PNGFILE *pFile, uint8_t *pBuf, int32_t iLen)
Definition PNGdec.h:126
int PNG_openFile(PNGIMAGE *pPNG, const char *szFilename)
int32_t() PNG_SEEK_CALLBACK(PNGFILE *pFile, int32_t iPosition)
Definition PNGdec.h:127
void() PNG_CLOSE_CALLBACK(void *pHandle)
Definition PNGdec.h:130
int PNG_getHeight(PNGIMAGE *pPNG)
@ PNG_MEM_RAM
Definition PNGdec.h:87
@ PNG_MEM_FLASH
Definition PNGdec.h:88
@ PNG_FAST_PALETTE
Definition PNGdec.h:69
@ PNG_CHECK_CRC
Definition PNGdec.h:68
uint8_t * PNG_getPalette(PNGIMAGE *pPNG)
int PNG_getWidth(PNGIMAGE *pPNG)
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
void * pUser
Definition PNGdec.h:111
uint16_t * pFastPalette
Definition PNGdec.h:113
int iHasAlpha
Definition PNGdec.h:110
int iPixelType
Definition PNGdec.h:108
uint8_t * pPixels
Definition PNGdec.h:114
const roo_display::Color * pPalette
Definition PNGdec.h:112
void * fHandle
Definition PNGdec.h:122
int32_t iPos
Definition PNGdec.h:119
int32_t iSize
Definition PNGdec.h:120
uint8_t * pData
Definition PNGdec.h:121
PNG_DRAW_CALLBACK * pfnDraw
Definition PNGdec.h:149
uint32_t iTransparent
Definition PNGdec.h:144
uint8_t ucBpp
Definition PNGdec.h:138
int iInterlaced
Definition PNGdec.h:143
int iHasAlpha
Definition PNGdec.h:142
PNGFILE PNGFile
Definition PNGdec.h:151
PNG_READ_CALLBACK * pfnRead
Definition PNGdec.h:146
PNG_CLOSE_CALLBACK * pfnClose
Definition PNGdec.h:150
uint8_t ucZLIB[32768+sizeof(inflate_state)]
Definition PNGdec.h:152
PNG_OPEN_CALLBACK * pfnOpen
Definition PNGdec.h:148
uint8_t * pImage
Definition PNGdec.h:140
uint8_t ucMemType
Definition PNGdec.h:139
uint8_t ucPixels[PNG_MAX_BUFFERED_PIXELS *2]
Definition PNGdec.h:154
uint8_t ucFileBuf[PNG_FILE_BUF_SIZE]
Definition PNGdec.h:155
PNG_SEEK_CALLBACK * pfnSeek
Definition PNGdec.h:147
roo_display::Color ucPalette[256]
Definition PNGdec.h:153
uint8_t ucPixelType
Definition PNGdec.h:138