Initial commit
This commit is contained in:
commit
64a227437d
1920 files changed
+3428556
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,2 @@
|
||||
#define LGFX_MINIZ_HEADER_FILE_ONLY
|
||||
#include "lgfx_miniz.c"
|
||||
@@ -0,0 +1,615 @@
|
||||
/*-
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2019 kikuchan
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ original source is here : https://github.com/kikuchan/pngle/
|
||||
/
|
||||
/ Modified for LGFX by lovyan03, 2020-2022
|
||||
/ tweak for 32bit processor
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lgfx_miniz.h"
|
||||
#include "lgfx_pngle.h"
|
||||
|
||||
#include "pgmspace.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifdef PNGLE_DEBUG
|
||||
#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
|
||||
#else
|
||||
#define debug_printf(...) ((void)0)
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
PNGLE_STATE_ERROR = -2,
|
||||
PNGLE_STATE_OK = 0,
|
||||
} pngle_state_t;
|
||||
|
||||
#define PNGLE_ERROR(s) ( debug_printf(s), PNGLE_STATE_ERROR)
|
||||
#define PNGLE_MALLOC(a, b, name) (debug_printf("[pngle] Allocating %zu bytes for %s\n", (size_t)(a) * (size_t)(b), (name)), malloc((size_t)(a) * (size_t)(b)))
|
||||
|
||||
#define LGFX_PNGLE_OUTBUF_LEN 64 /// At least 32 required
|
||||
#define LGFX_PNGLE_READBUF_LEN 512 /// At least 256 required
|
||||
|
||||
#define LGFX_PNGLE_NON_TRANS_COLOR 0x01000000
|
||||
|
||||
typedef enum {
|
||||
// Supported chunks
|
||||
PNGLE_CHUNK_IDAT = 0x49444154UL, // IDAT
|
||||
PNGLE_CHUNK_IEND = 0x49454e44UL, // IEND
|
||||
PNGLE_CHUNK_PLTE = 0x504c5445UL, // PLTE
|
||||
PNGLE_CHUNK_tRNS = 0x74524e53UL, // tRNS
|
||||
} pngle_chunk_t;
|
||||
|
||||
// typedef struct _pngle_t pngle_t; // declared in pngle.h
|
||||
struct _pngle_t {
|
||||
// PLTE chunk
|
||||
uint8_t *scanline_buf;
|
||||
uint8_t *palette;
|
||||
|
||||
size_t n_palettes;
|
||||
uint32_t trans_color;
|
||||
|
||||
// scanline decoder (reset on every set_interlace_pass() call)
|
||||
size_t scanline_pixels;
|
||||
size_t scanline_stride;
|
||||
size_t scanline_remain_bytes_to_render;
|
||||
|
||||
lgfx_pngle_read_callback_t read_callback;
|
||||
lgfx_pngle_draw_callback_t draw_callback;
|
||||
void *user_data;
|
||||
|
||||
uint32_t drawing_y;
|
||||
|
||||
// for grayscale
|
||||
uint32_t magni;
|
||||
|
||||
pngle_ihdr_t hdr;
|
||||
|
||||
uint8_t filter_type;
|
||||
// interlace
|
||||
uint8_t interlace_pass;
|
||||
|
||||
// 0 indicates IHDR hasn't been processed yet
|
||||
uint8_t channels;
|
||||
|
||||
// decompression state (reset on IHDR)
|
||||
uint8_t *next_out; // NULL indicates IDAT hasn't been processed yet
|
||||
size_t avail_out;
|
||||
uint32_t out_buf[LGFX_PNGLE_OUTBUF_LEN >> 2]; // out_buf + read_buf (Do not change the order)
|
||||
uint8_t read_buf[LGFX_PNGLE_READBUF_LEN];
|
||||
lgfx_tinfl_decompressor inflator; // 11000 bytes
|
||||
uint8_t lz_buf[TINFL_LZ_DICT_SIZE]; // 32768 bytes
|
||||
};
|
||||
|
||||
// magic
|
||||
static const uint8_t png_header[] PROGMEM = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52 };
|
||||
static const uint8_t interlace_div_y[9] PROGMEM = { 8, 8, 8, 4, 4, 2, 2, 1, 1 };
|
||||
static const uint8_t interlace_off_y[9] PROGMEM = { 0, 0, 4, 0, 2, 0, 1, 0, 0 };
|
||||
static const uint8_t* interlace_off_x = &interlace_off_y[1];
|
||||
static const uint8_t* interlace_div_x = &interlace_div_y[1];
|
||||
|
||||
static inline uint32_t swap32(uint32_t c) { c = (c >> 16) + (c << 16); return ((c >> 8) & 0xFF00FF) + ((c & 0xFF00FF) << 8); }
|
||||
|
||||
static inline uint32_t read_uint32(const uint8_t *p)
|
||||
{
|
||||
return (p[0] << 24)
|
||||
+ (p[1] << 16)
|
||||
+ (p[2] << 8)
|
||||
+ (p[3] << 0)
|
||||
;
|
||||
}
|
||||
|
||||
pngle_t *lgfx_pngle_new()
|
||||
{
|
||||
pngle_t* res = (pngle_t *)PNGLE_MALLOC(1, sizeof(pngle_t), "pngle_t");
|
||||
if (res)
|
||||
{
|
||||
res->palette = NULL;
|
||||
res->scanline_buf = NULL;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void lgfx_pngle_destroy(pngle_t *pngle)
|
||||
{
|
||||
if (pngle) {
|
||||
if (pngle->scanline_buf ) { free(pngle->scanline_buf ); }
|
||||
if (pngle->palette ) { free(pngle->palette ); }
|
||||
free(pngle);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t lgfx_pngle_get_width(pngle_t *pngle)
|
||||
{
|
||||
if (!pngle) return 0;
|
||||
return pngle->hdr.width;
|
||||
}
|
||||
|
||||
uint32_t lgfx_pngle_get_height(pngle_t *pngle)
|
||||
{
|
||||
if (!pngle) return 0;
|
||||
return pngle->hdr.height;
|
||||
}
|
||||
|
||||
pngle_ihdr_t *lgfx_pngle_get_ihdr(pngle_t *pngle)
|
||||
{
|
||||
if (!pngle) return NULL;
|
||||
if (pngle->channels == 0) return NULL;
|
||||
return &pngle->hdr;
|
||||
}
|
||||
|
||||
static void make_pixels(pngle_t *pngle, const uint8_t* buf, uint32_t* rgbbuf, size_t len)
|
||||
{
|
||||
size_t depth = pngle->hdr.depth;
|
||||
uint32_t* argb32 = rgbbuf - 1;
|
||||
uint32_t* last = &argb32[len];
|
||||
if (depth >= 8)
|
||||
{
|
||||
size_t add = depth >> 3;
|
||||
switch (pngle->channels)
|
||||
{
|
||||
case 1: // grayscale or palette
|
||||
do
|
||||
{
|
||||
*++argb32 = buf[0];
|
||||
buf += add;
|
||||
} while (argb32 != last);
|
||||
break;
|
||||
|
||||
case 2: // grayscale with alpha
|
||||
do
|
||||
{
|
||||
/// grayscale to color (* 0x010101) + alpha channel
|
||||
*++argb32 = buf[0]*0x01010100 + buf[add];
|
||||
buf += add * 2;
|
||||
} while (argb32 != last);
|
||||
break;
|
||||
|
||||
case 3: // color
|
||||
do
|
||||
{
|
||||
*++argb32 = 0xFF
|
||||
+ (buf[ 0] << 8)
|
||||
+ (buf[add ] << 16)
|
||||
+ (buf[add*2] << 24);
|
||||
buf += add * 3;
|
||||
} while (argb32 != last);
|
||||
break;
|
||||
|
||||
default: //4 : color + alpha
|
||||
do
|
||||
{
|
||||
*++argb32 = (buf[ 0] << 8)
|
||||
+ (buf[add ] << 16)
|
||||
+ (buf[add*2] << 24)
|
||||
+ (buf[add*3] );
|
||||
buf += add * 4;
|
||||
} while (argb32 != last);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t mask = ((1 << depth) - 1);
|
||||
size_t shift = 8;
|
||||
size_t b = buf[0];
|
||||
do
|
||||
{
|
||||
shift -= depth;
|
||||
*++argb32 = (b >> shift) & mask;
|
||||
if (shift == 0) {
|
||||
shift = 8;
|
||||
b = *++buf;
|
||||
}
|
||||
} while (argb32 != last);
|
||||
}
|
||||
|
||||
argb32 = rgbbuf - 1;
|
||||
// color type: 0000 0111
|
||||
// ^-- indexed color (palette)
|
||||
// ^--- Color
|
||||
// ^---- Alpha channel
|
||||
switch (pngle->hdr.color_type)
|
||||
{
|
||||
case 0: // grayscale
|
||||
{
|
||||
uint32_t tp = pngle->trans_color;
|
||||
uint32_t magni = pngle->magni;
|
||||
do {
|
||||
uint32_t tmp = *++argb32;
|
||||
*argb32 = ((tmp == tp) ? 0 : 0xFF) + (tmp * magni);
|
||||
} while (argb32 != last);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: // true color
|
||||
if (pngle->trans_color != LGFX_PNGLE_NON_TRANS_COLOR)
|
||||
{
|
||||
uint32_t tp = pngle->trans_color;
|
||||
do
|
||||
{
|
||||
if (*++argb32 == tp) { ((uint8_t*)argb32)[0] = 0; }
|
||||
} while (argb32 != last);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3: // indexed color
|
||||
{
|
||||
size_t n_palettes = pngle->n_palettes;
|
||||
uint32_t* palette = ((uint32_t*)(pngle->palette));
|
||||
do
|
||||
{ // lookup palette info
|
||||
size_t pidx = *++argb32;
|
||||
*argb32 = pidx < n_palettes ? palette[pidx] : ~0u;
|
||||
} while (argb32 != last);
|
||||
}
|
||||
break;
|
||||
|
||||
//case 4: // grayscale with alpha ...nothing to do.
|
||||
//case 6: // truecolor with alpha ...nothing to do.
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int paeth(int a, int b, int c)
|
||||
{
|
||||
int pa = b - c;
|
||||
int pb = a - c;
|
||||
int pc = abs(pa + pb);
|
||||
pa = abs(pa);
|
||||
pb = abs(pb);
|
||||
if (pb < pa) { pa = pb; a = b; }
|
||||
return (pc < pa) ? c : a;
|
||||
}
|
||||
|
||||
static void set_interlace_pass(pngle_t *pngle, uint_fast8_t pass)
|
||||
{
|
||||
while (pngle->hdr.width <= pgm_read_byte(&interlace_off_x[pass]) || pngle->hdr.height <= pgm_read_byte(&interlace_off_y[pass]))
|
||||
{
|
||||
++pass;
|
||||
}
|
||||
pngle->interlace_pass = pass;
|
||||
pngle->drawing_y = pgm_read_byte(&interlace_off_y[pass]);
|
||||
size_t div_x = pgm_read_byte(&interlace_div_x[pass]);
|
||||
size_t scanline_pixels = (pngle->hdr.width - pgm_read_byte(&interlace_off_x[pass]) + div_x - 1) / div_x;
|
||||
pngle->scanline_pixels = scanline_pixels;
|
||||
size_t scanline_stride = (scanline_pixels * pngle->channels * pngle->hdr.depth + 7) >> 3;
|
||||
pngle->scanline_stride = scanline_stride;
|
||||
pngle->scanline_remain_bytes_to_render = scanline_stride;
|
||||
}
|
||||
|
||||
static int pngle_on_data(pngle_t *pngle, uint8_t *lzbuf, size_t len, size_t outbuf_len)
|
||||
{
|
||||
uint_fast8_t bytes_per_pixel = (pngle->channels * pngle->hdr.depth + 7) >> 3; // 1 if depth <= 8
|
||||
size_t filter_type = pngle->filter_type;
|
||||
size_t remain_bytes = pngle->scanline_remain_bytes_to_render;
|
||||
|
||||
|
||||
const uint8_t* p = lzbuf;
|
||||
uint8_t* scanline = &(pngle->scanline_buf[bytes_per_pixel]);
|
||||
while (len)
|
||||
{
|
||||
if (filter_type > 4)
|
||||
{
|
||||
filter_type = *p++; // 0 - 4
|
||||
if (filter_type >= 4)
|
||||
{
|
||||
if (filter_type > 4)
|
||||
{
|
||||
debug_printf("[pngle] Invalid filter type is found; 0x%02x\n", *p);
|
||||
return PNGLE_ERROR("Invalid filter type is found");
|
||||
}
|
||||
// Only when using the paeth filter, shift the previous result to the right by bytes_per_pixel. (To refer to the result of the upper left pixel).
|
||||
memmove(scanline, scanline - bytes_per_pixel, bytes_per_pixel + pngle->scanline_stride);
|
||||
}
|
||||
if (--len == 0) { break; }
|
||||
}
|
||||
size_t cidx = pngle->scanline_stride - remain_bytes;
|
||||
|
||||
size_t l = (len < remain_bytes) ? len : remain_bytes;
|
||||
remain_bytes -= l;
|
||||
len -= l;
|
||||
|
||||
const uint8_t* newdata = p - cidx;
|
||||
p += l;
|
||||
size_t last = cidx + l;
|
||||
|
||||
switch (filter_type) {
|
||||
default: memcpy(&scanline[cidx], &newdata[cidx], l); cidx = last; break;
|
||||
case 1: do { scanline[cidx] = newdata[cidx] + scanline[cidx - bytes_per_pixel]; } while (++cidx != last); break;
|
||||
case 2: do { scanline[cidx] += newdata[cidx]; } while (++cidx != last); break;
|
||||
case 3: do { scanline[cidx] = newdata[cidx] + ((scanline[cidx - bytes_per_pixel] + scanline[cidx]) >> 1); } while (++cidx != last); break;
|
||||
case 4: do { scanline[cidx] = newdata[cidx] + paeth(scanline[cidx - bytes_per_pixel], scanline[cidx + bytes_per_pixel], scanline[cidx]); } while (++cidx != last); break;
|
||||
}
|
||||
if (remain_bytes) { break; }
|
||||
|
||||
remain_bytes = pngle->scanline_stride; // reset
|
||||
filter_type = ~0;
|
||||
|
||||
uint32_t draw_x = pgm_read_byte(&interlace_off_x[pngle->interlace_pass]);
|
||||
uint32_t div_x = pgm_read_byte(&interlace_div_x[pngle->interlace_pass]);
|
||||
size_t scanline_pixels = pngle->scanline_pixels;
|
||||
size_t out_pos = 0;
|
||||
size_t out_len = ((((scanline_pixels + 7) & ~7) - 1) % outbuf_len) + 1;
|
||||
|
||||
do
|
||||
{
|
||||
if (out_len > scanline_pixels - out_pos) { out_len = scanline_pixels - out_pos; }
|
||||
make_pixels(pngle, &scanline[(out_pos * pngle->channels * pngle->hdr.depth) >> 3], pngle->out_buf, out_len);
|
||||
pngle->draw_callback(pngle->user_data, draw_x + out_pos * div_x, pngle->drawing_y, div_x, out_len, (const uint8_t*)pngle->out_buf);
|
||||
|
||||
out_pos += out_len;
|
||||
out_len = outbuf_len;
|
||||
} while (out_pos < scanline_pixels);
|
||||
|
||||
pngle->drawing_y += pgm_read_byte(&interlace_div_y[pngle->interlace_pass]);
|
||||
if (pngle->drawing_y >= pngle->hdr.height) {
|
||||
if (pngle->interlace_pass >= 6) { return 0; } // Do nothing further
|
||||
|
||||
// Interlace: Next pass
|
||||
set_interlace_pass(pngle, pngle->interlace_pass + 1);
|
||||
debug_printf("[pngle] interlace pass changed to: %d\n", pngle->interlace_pass);
|
||||
remain_bytes = pngle->scanline_stride; // reset
|
||||
memset(scanline, 0, remain_bytes);
|
||||
}
|
||||
}
|
||||
pngle->scanline_remain_bytes_to_render = remain_bytes;
|
||||
pngle->filter_type = filter_type;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lgfx_pngle_prepare(pngle_t *pngle, lgfx_pngle_read_callback_t read_cb, void* user_data)
|
||||
{
|
||||
if (pngle == NULL || read_cb == NULL) { return PNGLE_STATE_ERROR; }
|
||||
if (pngle->palette ) { free(pngle->palette ); pngle->palette = NULL; }
|
||||
if (pngle->scanline_buf ) { free(pngle->scanline_buf ); pngle->scanline_buf = NULL; }
|
||||
|
||||
pngle->read_callback = read_cb;
|
||||
pngle->user_data = user_data;
|
||||
pngle->n_palettes = 0;
|
||||
pngle->next_out = pngle->lz_buf;
|
||||
pngle->avail_out = TINFL_LZ_DICT_SIZE;
|
||||
pngle->filter_type = ~0;
|
||||
pngle->trans_color = LGFX_PNGLE_NON_TRANS_COLOR;
|
||||
lgfx_tinfl_init(&pngle->inflator);
|
||||
|
||||
if (pngle->read_callback(user_data, pngle->read_buf, 29) != 29
|
||||
|| memcmp_P(pngle->read_buf, png_header, sizeof(png_header))) return PNGLE_ERROR("Incorrect PNG signature");
|
||||
|
||||
memcpy(&(pngle->hdr), &pngle->read_buf[16], sizeof(pngle_ihdr_t));
|
||||
pngle->hdr.width = swap32(pngle->hdr.width );
|
||||
pngle->hdr.height = swap32(pngle->hdr.height);
|
||||
|
||||
debug_printf("[pngle] width : %d\n", pngle->hdr.width );
|
||||
debug_printf("[pngle] height : %d\n", pngle->hdr.height );
|
||||
debug_printf("[pngle] depth : %d\n", pngle->hdr.depth );
|
||||
debug_printf("[pngle] color_type : %d\n", pngle->hdr.color_type );
|
||||
debug_printf("[pngle] compression: %d\n", pngle->hdr.compression);
|
||||
debug_printf("[pngle] filter : %d\n", pngle->hdr.filter );
|
||||
debug_printf("[pngle] interlace : %d\n", pngle->hdr.interlace );
|
||||
|
||||
if (pngle->hdr.compression != 0) return PNGLE_ERROR("Unsupported compression type in IHDR");
|
||||
if (pngle->hdr.filter != 0) return PNGLE_ERROR("Unsupported filter type in IHDR");
|
||||
|
||||
/*
|
||||
Color Allowed Interpretation channels
|
||||
Type Bit Depths
|
||||
|
||||
0 1,2,4,8,16 Each pixel is a grayscale sample. 1 channels (Brightness)
|
||||
|
||||
2 8,16 Each pixel is an R,G,B triple. 3 channels (R, G, B)
|
||||
|
||||
3 1,2,4,8 Each pixel is a palette index; 1 channels (palette info)
|
||||
a PLTE chunk must appear.
|
||||
|
||||
4 8,16 Each pixel is a grayscale sample, 2 channels (Brightness, Alpha)
|
||||
followed by an alpha sample.
|
||||
|
||||
6 8,16 Each pixel is an R,G,B triple, 4 channels (R, G, B, Alpha)
|
||||
followed by an alpha sample.
|
||||
*/
|
||||
// 111
|
||||
// ^-- indexed color (palette)
|
||||
// ^--- Color
|
||||
// ^---- Alpha channel
|
||||
{
|
||||
static const uint8_t table[] PROGMEM =
|
||||
{ 1 << 5 | 0b00000 // type 0 : 1 channels (Brightness)
|
||||
, 0b11111
|
||||
, 3 << 5 | 0b00111 // type 2 : 3 channels (R, G, B)
|
||||
, 1 << 5 | 0b10000 // type 3 : 1 channels (palette info)
|
||||
, 2 << 5 | 0b00111 // type 4 : 2 channels (Brightness, Alpha)
|
||||
, 0b11111
|
||||
, 4 << 5 | 0b00111 // type 6 : 4 channels (R, G, B, Alpha)
|
||||
, 0b11111
|
||||
};
|
||||
uint_fast8_t ch_mask = pgm_read_byte(&table[pngle->hdr.color_type & 7]);
|
||||
pngle->channels = ch_mask >> 5;
|
||||
int depth = pngle->hdr.depth;
|
||||
if (pngle->hdr.color_type > 7 || pngle->channels == 0
|
||||
|| depth == 0 || (depth & ch_mask) || (depth != (depth & (-depth)))) return PNGLE_ERROR("Incorrect IHDR info");
|
||||
|
||||
pngle->magni = 0x01010100
|
||||
* ( (depth == 1) ? 0xFF
|
||||
: (depth == 2) ? 0x55
|
||||
: (depth == 4) ? 0x11
|
||||
: 0x01
|
||||
);
|
||||
|
||||
uint_fast8_t bytes_per_pixel = (pngle->channels * pngle->hdr.depth + 7) >> 3;
|
||||
size_t memlen = ((pngle->hdr.width * pngle->channels * pngle->hdr.depth + 7) >> 3) + (2 * bytes_per_pixel);
|
||||
if ((pngle->scanline_buf = (uint8_t*)PNGLE_MALLOC(memlen, 1, "scanline flipbuf")) == NULL) return PNGLE_ERROR("Insufficient memory");
|
||||
memset(pngle->scanline_buf, 0, memlen);
|
||||
}
|
||||
// interlace
|
||||
set_interlace_pass(pngle, pngle->hdr.interlace ? 0 : 7);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lgfx_pngle_decomp(pngle_t *pngle, lgfx_pngle_draw_callback_t draw_cb)
|
||||
{
|
||||
if (pngle == NULL || draw_cb == NULL) { return PNGLE_STATE_ERROR; }
|
||||
pngle->draw_callback = draw_cb;
|
||||
|
||||
uint8_t* read_buf = pngle->read_buf;
|
||||
for (;;)
|
||||
{
|
||||
if (pngle->read_callback(pngle->user_data, read_buf, 12) != 12) { return PNGLE_ERROR("Insufficient data"); }
|
||||
|
||||
uint32_t chunk_remain = read_uint32(&read_buf[4]);
|
||||
uint32_t chunk_type = read_uint32(&read_buf[8]);
|
||||
|
||||
debug_printf("[pngle] Chunk '%.4s' len %u\n", read_buf + 8, chunk_remain);
|
||||
|
||||
// initialize & sanity check
|
||||
switch (chunk_type) {
|
||||
default:
|
||||
if (pngle->read_callback(pngle->user_data, NULL, chunk_remain) != chunk_remain) { return PNGLE_ERROR("Insufficient data"); }
|
||||
break;
|
||||
|
||||
case PNGLE_CHUNK_IDAT:
|
||||
if (chunk_remain <= 0) return PNGLE_ERROR("Invalid IDAT chunk size");
|
||||
|
||||
do
|
||||
{
|
||||
size_t len = pngle->read_callback(pngle->user_data, read_buf, (chunk_remain < LGFX_PNGLE_READBUF_LEN) ? chunk_remain : LGFX_PNGLE_READBUF_LEN);
|
||||
if (len == 0) { return PNGLE_ERROR("Insufficient data"); }
|
||||
chunk_remain -= len;
|
||||
|
||||
debug_printf("[pngle] Reading IDAT (len %zd / chunk remain %u)\n", len, chunk_remain);
|
||||
|
||||
//debug_printf("[pngle] in_bytes %zd, out_bytes %zd, next_out %p\n", in_bytes, out_bytes, pngle->next_out);
|
||||
size_t in_pos = 0;
|
||||
do
|
||||
{
|
||||
size_t in_bytes = len;
|
||||
size_t out_bytes = pngle->avail_out;
|
||||
|
||||
// XXX: lgfx_tinfl_decompress always requires (next_out - lz_buf + avail_out) == TINFL_LZ_DICT_SIZE
|
||||
lgfx_tinfl_status status = lgfx_tinfl_decompress(&pngle->inflator, (const lgfx_mz_uint8*)&read_buf[in_pos], &in_bytes, pngle->lz_buf, (lgfx_mz_uint8*)pngle->next_out, &out_bytes, TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_PARSE_ZLIB_HEADER);
|
||||
if (status < TINFL_STATUS_DONE)
|
||||
{
|
||||
// Decompression failed.
|
||||
debug_printf("[pngle] lgfx_tinfl_decompress() failed with status %d!\n", status);
|
||||
return PNGLE_ERROR("Failed to decompress the IDAT stream");
|
||||
}
|
||||
|
||||
len -= in_bytes;
|
||||
in_pos += in_bytes;
|
||||
|
||||
//debug_printf("[pngle] lgfx_tinfl_decompress\n");
|
||||
//debug_printf("[pngle] => in_bytes %zd, out_bytes %zd, next_out %p, status %d\n", in_bytes, out_bytes, pngle->next_out, status);
|
||||
|
||||
if (out_bytes)
|
||||
{
|
||||
if (pngle_on_data(pngle, pngle->next_out, out_bytes, (LGFX_PNGLE_OUTBUF_LEN >> 2) + (len ? in_pos >> 2 : (LGFX_PNGLE_READBUF_LEN >> 2))) < 0) return -1;
|
||||
}
|
||||
pngle->next_out += out_bytes;
|
||||
pngle->avail_out -= out_bytes;
|
||||
// debug_printf("[pngle] => avail_out %zd, next_out %p\n", pngle->avail_out, pngle->next_out);
|
||||
if (pngle->avail_out == 0 || status == TINFL_STATUS_DONE)
|
||||
{ // Output buffer is full, or decompression is done, so write buffer to output file.
|
||||
pngle->avail_out = TINFL_LZ_DICT_SIZE;
|
||||
pngle->next_out = pngle->lz_buf;
|
||||
}
|
||||
} while (len);
|
||||
} while (chunk_remain);
|
||||
break;
|
||||
|
||||
case PNGLE_CHUNK_IEND:
|
||||
pngle->read_callback(pngle->user_data, NULL, 4); // skip crc
|
||||
return 0;
|
||||
|
||||
case PNGLE_CHUNK_PLTE:
|
||||
{
|
||||
// Allow only 2, 3, 6. (2=truecolor / 3=indexed color / 6=truecolor+alpha)
|
||||
if (((1 << pngle->hdr.color_type) & 0b1001100) == 0) { return PNGLE_ERROR("PLTE chunk is prohibited on the color type"); }
|
||||
|
||||
uint32_t chunk_remain_3 = chunk_remain / 3;
|
||||
if (chunk_remain_3 > MIN(256, (1UL << pngle->hdr.depth))) return PNGLE_ERROR("Too many palettes in PLTE");
|
||||
if (chunk_remain_3 <= 0 || chunk_remain != chunk_remain_3 * 3) return PNGLE_ERROR("Invalid PLTE chunk size");
|
||||
if (pngle->palette) return PNGLE_ERROR("Too many PLTE chunk");
|
||||
uint8_t* plt = (uint8_t*)PNGLE_MALLOC(chunk_remain_3, 4, "palette");
|
||||
if (plt == NULL) return PNGLE_ERROR("Insufficient memory");
|
||||
pngle->palette = plt;
|
||||
pngle->n_palettes = chunk_remain_3;
|
||||
|
||||
if (chunk_remain != pngle->read_callback(pngle->user_data, plt, chunk_remain)) { return PNGLE_ERROR("Insufficient data"); }
|
||||
while (chunk_remain_3--)
|
||||
{
|
||||
((uint32_t*)(plt))[chunk_remain_3] = ( plt[chunk_remain_3 * 3 + 0] << 8 )
|
||||
+ ( plt[chunk_remain_3 * 3 + 1] << 16 )
|
||||
+ ( plt[chunk_remain_3 * 3 + 2] << 24 )
|
||||
+ 0xFF;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case PNGLE_CHUNK_tRNS:
|
||||
if (chunk_remain <= 0 || chunk_remain > 256) return PNGLE_ERROR("Invalid tRNS chunk size");
|
||||
if (chunk_remain != pngle->read_callback(pngle->user_data, read_buf, chunk_remain)) { return PNGLE_ERROR("Insufficient data"); }
|
||||
switch (pngle->hdr.color_type) {
|
||||
case 3: // indexed color
|
||||
if (chunk_remain > pngle->n_palettes) return PNGLE_ERROR("Too many palettes in tRNS");
|
||||
for (size_t i = 0; i < chunk_remain; ++i)
|
||||
{
|
||||
pngle->palette[i * 4] = read_buf[i];
|
||||
}
|
||||
break;
|
||||
|
||||
case 0: // grayscale
|
||||
if (chunk_remain != 2) return PNGLE_ERROR("Invalid tRNS chunk size");
|
||||
pngle->trans_color = read_buf[1];
|
||||
break;
|
||||
|
||||
case 2: // truecolor
|
||||
if (chunk_remain != 6) return PNGLE_ERROR("Invalid tRNS chunk size");
|
||||
pngle->trans_color = (read_buf[1] << 8)
|
||||
+ (read_buf[3] << 16)
|
||||
+ (read_buf[5] << 24)
|
||||
+ 0xFF;
|
||||
break;
|
||||
|
||||
default:
|
||||
return PNGLE_ERROR("tRNS chunk is prohibited on the color type");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*-
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2019 kikuchan
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ original source is here : https://github.com/kikuchan/pngle/
|
||||
/
|
||||
/ Modified for LGFX by lovyan03, 2020-2022
|
||||
/ tweak for 32bit processor
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef __LGFX_PNGLE_H__
|
||||
#define __LGFX_PNGLE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Main Pngle object
|
||||
typedef struct _pngle_t pngle_t;
|
||||
|
||||
// Callback signatures
|
||||
typedef uint32_t (*lgfx_pngle_read_callback_t)(void *user_data, uint8_t *buf, uint32_t len);
|
||||
typedef void (*lgfx_pngle_draw_callback_t)(void *user_data, uint32_t x, uint32_t y, uint_fast8_t div_x, size_t len, const uint8_t* argb);
|
||||
|
||||
// ----------------
|
||||
// Basic interfaces
|
||||
// ----------------
|
||||
pngle_t *lgfx_pngle_new();
|
||||
|
||||
int lgfx_pngle_prepare(pngle_t *pngle, lgfx_pngle_read_callback_t read_cb, void* user_data);
|
||||
int lgfx_pngle_decomp(pngle_t *pngle, lgfx_pngle_draw_callback_t draw_cb);
|
||||
|
||||
void lgfx_pngle_destroy(pngle_t *pngle);
|
||||
|
||||
uint32_t lgfx_pngle_get_width(pngle_t *pngle);
|
||||
uint32_t lgfx_pngle_get_height(pngle_t *pngle);
|
||||
|
||||
// ----------------
|
||||
// Debug interfaces
|
||||
// ----------------
|
||||
|
||||
typedef struct __attribute__((packed)) _pngle_ihdr_t {
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t depth;
|
||||
uint8_t color_type;
|
||||
uint8_t compression;
|
||||
uint8_t filter;
|
||||
uint8_t interlace;
|
||||
} pngle_ihdr_t;
|
||||
|
||||
// Get IHDR information
|
||||
pngle_ihdr_t *lgfx_pngle_get_ihdr(pngle_t *pngle);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PNGLE_H__ */
|
||||
@@ -0,0 +1,461 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lgfx_qoi.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
// color spaces
|
||||
#define QOI_SRGB 0
|
||||
#define QOI_LINEAR 1
|
||||
|
||||
#define QOI_HEADER_SIZE 14
|
||||
#define QOI_PIXELS_MAX ((unsigned int)400000000)
|
||||
|
||||
#define LGFX_QOI_READBUF_LEN 256
|
||||
|
||||
// QOI header
|
||||
static const uint32_t qoi_sig = 0x716F6966; // {'q','o','i','f'};
|
||||
// QOI footer
|
||||
static const uint8_t qoi_padding[8] = {0,0,0,0,0,0,0,1};
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct { unsigned char a, r, g, b; } rgba;
|
||||
unsigned int v;
|
||||
} qoi_rgba_t;
|
||||
|
||||
|
||||
typedef struct __attribute__((packed)) _qoi_desc_t
|
||||
{
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
unsigned char channels;
|
||||
unsigned char colorspace;
|
||||
} qoi_desc_t;
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
QOI_OP_INDEX = 0x00, /* 00xxxxxx */
|
||||
QOI_OP_DIFF = 0x40, /* 01xxxxxx */
|
||||
QOI_OP_LUMA = 0x80, /* 10xxxxxx */
|
||||
QOI_OP_RUN = 0xc0, /* 11xxxxxx */
|
||||
QOI_OP_RGB = 0xfe, /* 11111110 */
|
||||
QOI_OP_RGBA = 0xff, /* 11111111 */
|
||||
QOI_MASK_2 = 0xc0 /* 11000000 */
|
||||
} qoi_flag_t;
|
||||
|
||||
|
||||
// typedef struct _qoi_t qoi_t; // declared in qoi.h
|
||||
struct _qoi_t
|
||||
{
|
||||
qoi_rgba_t* pixelBuffer;
|
||||
qoi_rgba_t px;
|
||||
|
||||
void *user_data;
|
||||
lgfx_qoi_read_callback_t read_cb;
|
||||
|
||||
uint8_t repeat;
|
||||
qoi_desc_t desc;
|
||||
qoi_rgba_t index[64];
|
||||
uint8_t read_buf[LGFX_QOI_READBUF_LEN];
|
||||
};
|
||||
|
||||
#define QOI_DEBUG
|
||||
|
||||
#ifdef QOI_DEBUG
|
||||
#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
|
||||
#define QOI_ERROR(...) (fprintf(stderr, __VA_ARGS__), -2)
|
||||
#else
|
||||
#define debug_printf(...) ((void)0)
|
||||
#define QOI_ERROR(...) ( -2 )
|
||||
#endif
|
||||
|
||||
|
||||
static inline uint8_t QOI_COLOR_HASH( const qoi_rgba_t *c )
|
||||
{
|
||||
return 0x3F & (c->rgba.r*3 + c->rgba.g*5 + c->rgba.b*7 + c->rgba.a*11);
|
||||
}
|
||||
|
||||
|
||||
static uint8_t read_uint8(const uint8_t *p)
|
||||
{
|
||||
return *p;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t read_uint32(const uint8_t *p)
|
||||
{
|
||||
return (p[0] << 24)
|
||||
| (p[1] << 16)
|
||||
| (p[2] << 8)
|
||||
| (p[3] << 0)
|
||||
;
|
||||
}
|
||||
|
||||
int lgfx_qoi_prepare(qoi_t *qoi, lgfx_qoi_read_callback_t read_cb, void* user_data)
|
||||
{
|
||||
if (qoi == NULL || read_cb == NULL) { return -2; }
|
||||
|
||||
qoi->read_cb = read_cb;
|
||||
qoi->user_data = user_data;
|
||||
|
||||
uint8_t* buf = qoi->read_buf;
|
||||
if (qoi->read_cb(user_data, buf, QOI_HEADER_SIZE) != QOI_HEADER_SIZE) return -2;
|
||||
if (qoi_sig != read_uint32(buf)) { return QOI_ERROR("Incorrect QOI signature"); }
|
||||
|
||||
qoi->desc.width = read_uint32(buf + 4);
|
||||
qoi->desc.height = read_uint32(buf + 8);
|
||||
qoi->desc.channels = read_uint8(buf + 12);
|
||||
qoi->desc.colorspace = read_uint8(buf + 13);
|
||||
qoi->px.rgba.a = 255;
|
||||
|
||||
if( qoi->desc.width == 0 || qoi->desc.height == 0 || qoi->desc.colorspace > 1 ) return QOI_ERROR("Incorrect QOI signature");
|
||||
if (qoi->desc.channels != 0 && qoi->desc.channels != 3 && qoi->desc.channels != 4) return QOI_ERROR("Bad channels count");
|
||||
// if( qoi->desc.height >= QOI_PIXELS_MAX / qoi->desc.width ) return QOI_ERROR("Image too big");
|
||||
|
||||
qoi->pixelBuffer = (qoi_rgba_t*)malloc(qoi->desc.width * sizeof(qoi_rgba_t));
|
||||
if (qoi->pixelBuffer == NULL) { return QOI_ERROR("Insufficient memory"); }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lgfx_qoi_decomp(qoi_t *qoi, lgfx_qoi_draw_callback_t draw_cb)
|
||||
{
|
||||
if (qoi == NULL || draw_cb == NULL) return -2;
|
||||
|
||||
uint8_t* buf = qoi->read_buf;
|
||||
size_t len = qoi->read_cb(qoi->user_data, buf, LGFX_QOI_READBUF_LEN);
|
||||
if (len == 0) { return QOI_ERROR("Insufficient data"); }
|
||||
size_t consume = 0;
|
||||
size_t flip = 0;
|
||||
size_t x = 0, y = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (qoi->repeat > 0)
|
||||
{
|
||||
uint32_t xe = x + qoi->repeat;
|
||||
if (xe > qoi->desc.width) { xe = qoi->desc.width; }
|
||||
qoi->repeat -= xe - x;
|
||||
do
|
||||
{
|
||||
qoi->pixelBuffer[x] = qoi->px;
|
||||
} while (++x != xe);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((consume & (LGFX_QOI_READBUF_LEN >> 1)) != flip)
|
||||
{
|
||||
len += qoi->read_cb(qoi->user_data, &buf[flip], LGFX_QOI_READBUF_LEN >> 1);
|
||||
flip ^= (LGFX_QOI_READBUF_LEN >> 1);
|
||||
}
|
||||
|
||||
uint8_t b1 = buf[consume & 0xFF];
|
||||
|
||||
if (b1 == QOI_OP_RGB)
|
||||
{
|
||||
qoi->px.rgba.r = buf[++consume & 0xFF];
|
||||
qoi->px.rgba.g = buf[++consume & 0xFF];
|
||||
qoi->px.rgba.b = buf[++consume & 0xFF];
|
||||
}
|
||||
else if (b1 == QOI_OP_RGBA)
|
||||
{
|
||||
qoi->px.rgba.r = buf[++consume & 0xFF];
|
||||
qoi->px.rgba.g = buf[++consume & 0xFF];
|
||||
qoi->px.rgba.b = buf[++consume & 0xFF];
|
||||
qoi->px.rgba.a = buf[++consume & 0xFF];
|
||||
}
|
||||
else if ((b1 & QOI_MASK_2) == QOI_OP_INDEX)
|
||||
{
|
||||
qoi->px = qoi->index[b1];
|
||||
}
|
||||
else if ((b1 & QOI_MASK_2) == QOI_OP_DIFF)
|
||||
{
|
||||
qoi->px.rgba.r += ((b1 >> 4) & 0x03) - 2;
|
||||
qoi->px.rgba.g += ((b1 >> 2) & 0x03) - 2;
|
||||
qoi->px.rgba.b += ( b1 & 0x03) - 2;
|
||||
}
|
||||
else if ((b1 & QOI_MASK_2) == QOI_OP_LUMA)
|
||||
{
|
||||
uint8_t b2 = buf[++consume & 0xFF];
|
||||
int vg = (b1 & 0x3f) - 32;
|
||||
qoi->px.rgba.r += vg - 8 + ((b2 >> 4));
|
||||
qoi->px.rgba.g += vg;
|
||||
qoi->px.rgba.b += vg - 8 + (b2 & 0x0f);
|
||||
}
|
||||
else // if ((b1 & QOI_MASK_2) == QOI_OP_RUN)
|
||||
{
|
||||
qoi->repeat = (b1 & 0x3f);
|
||||
}
|
||||
|
||||
++consume;
|
||||
qoi->index[QOI_COLOR_HASH(&qoi->px)] = qoi->px;
|
||||
|
||||
if (qoi->desc.channels != 4) {
|
||||
qoi->px.rgba.a = 255;
|
||||
}
|
||||
qoi->pixelBuffer[x++] = qoi->px;
|
||||
}
|
||||
|
||||
if (x < qoi->desc.width) { continue; }
|
||||
x = 0;
|
||||
|
||||
draw_cb(qoi->user_data, 0, y, 1, qoi->desc.width, (const uint8_t*)qoi->pixelBuffer);
|
||||
if (++y >= qoi->desc.height)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lgfx_qoi_reset(qoi_t *qoi)
|
||||
{
|
||||
if (!qoi) return;
|
||||
if (qoi->pixelBuffer != NULL) { free(qoi->pixelBuffer); qoi->pixelBuffer = NULL; }
|
||||
}
|
||||
|
||||
|
||||
qoi_t *lgfx_qoi_new()
|
||||
{
|
||||
qoi_t *qoi = (qoi_t *)calloc(1, sizeof(qoi_t) );
|
||||
if (!qoi) return NULL;
|
||||
return qoi;
|
||||
}
|
||||
|
||||
|
||||
void lgfx_qoi_destroy(qoi_t *qoi)
|
||||
{
|
||||
if (qoi) {
|
||||
lgfx_qoi_reset(qoi);
|
||||
free(qoi);
|
||||
}
|
||||
}
|
||||
|
||||
// Qoi Encoder
|
||||
|
||||
|
||||
static uint8_t* writeBuffer;
|
||||
static size_t writeBufferSize;
|
||||
static uint32_t writeBufferPos;
|
||||
lfgx_qoi_writer_func bytes_writer;
|
||||
|
||||
|
||||
static int8_t enc_write_uint8( uint8_t v )
|
||||
{
|
||||
writeBuffer[writeBufferPos++] = v;
|
||||
if( writeBufferPos == writeBufferSize ) { // buffer full, write!
|
||||
// TODO: handle write errors
|
||||
if( bytes_writer ) bytes_writer( writeBuffer, writeBufferSize );
|
||||
writeBufferPos = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int8_t enc_write_uint32( uint32_t v )
|
||||
{
|
||||
enc_write_uint8( (uint8_t)(v >> 24) );
|
||||
enc_write_uint8( (uint8_t)(v >> 16) );
|
||||
enc_write_uint8( (uint8_t)(v >> 8) );
|
||||
enc_write_uint8( (uint8_t)v );
|
||||
return 4;
|
||||
}
|
||||
|
||||
uint32_t lgfx_qoi_get_width(qoi_t *qoi)
|
||||
{
|
||||
if (!qoi) return 0;
|
||||
return qoi->desc.width;
|
||||
}
|
||||
|
||||
uint32_t lgfx_qoi_get_height(qoi_t *qoi)
|
||||
{
|
||||
if (!qoi) return 0;
|
||||
return qoi->desc.height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t lgfx_qoi_encoder_write_cb(const void *lineBuffer, uint32_t bufferLen, int w, int h, int num_chans, int flip, lgfx_qoi_encoder_get_row_func get_row, lfgx_qoi_writer_func write_bytes, void *qoienc)
|
||||
{
|
||||
qoi_desc_t desc;
|
||||
desc.width = w;
|
||||
desc.height = h;
|
||||
desc.channels = num_chans;
|
||||
desc.colorspace = QOI_SRGB; // QOI_SRGB=0, QOI_LINEAR=1
|
||||
writeBufferSize = bufferLen;
|
||||
size_t res = lgfx_qoi_encode(lineBuffer, &desc, flip, get_row, write_bytes, qoienc);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void *lgfx_qoi_encoder_write_fb(const void *lineBuffer, int w, int h, int num_chans, size_t *out_len, int flip, lgfx_qoi_encoder_get_row_func get_row, void *qoienc)
|
||||
{
|
||||
qoi_desc_t desc;
|
||||
desc.width = w;
|
||||
desc.height = h;
|
||||
desc.channels = num_chans;
|
||||
desc.colorspace = QOI_SRGB; // QOI_SRGB=0, QOI_LINEAR=1
|
||||
writeBufferSize = desc.width * desc.height * (desc.channels + 1) + QOI_HEADER_SIZE + sizeof(qoi_padding);
|
||||
size_t res = lgfx_qoi_encode(lineBuffer, &desc, flip, get_row, NULL, qoienc);
|
||||
*out_len = res;
|
||||
return (void*)writeBuffer;
|
||||
}
|
||||
|
||||
|
||||
size_t lgfx_qoi_encode(const void *lineBuffer, const qoi_desc_t *desc, int flip, lgfx_qoi_encoder_get_row_func get_row, lfgx_qoi_writer_func write_bytes, void *qoienc)
|
||||
{
|
||||
int i, p, repeat;
|
||||
int px_len, px_end, px_pos, channels;
|
||||
uint8_t *pixels = (uint8_t*)lineBuffer;
|
||||
|
||||
qoi_rgba_t *qoi_index = (qoi_rgba_t*)(calloc( 64, sizeof( qoi_rgba_t ) ) );
|
||||
qoi_rgba_t px, px_prev;
|
||||
|
||||
if (lineBuffer == NULL) { debug_printf( "Bad lineBuffer"); return 0; }
|
||||
if( qoi_index == NULL ) { debug_printf( "OOM"); return 0; }
|
||||
if (desc == NULL ) { debug_printf( "Bad desc"); return 0; }
|
||||
if (desc->width == 0 || desc->height == 0 ) { debug_printf( "Bad w/h"); return 0; }
|
||||
if (desc->channels < 3 || desc->channels > 4 ) { debug_printf( "Bad bpp"); return 0; }
|
||||
if (desc->colorspace > 1 ) { debug_printf( "Bad colorspace"); return 0; }
|
||||
if (desc->height >= QOI_PIXELS_MAX / desc->width ) { debug_printf( "Too big"); return 0; }
|
||||
|
||||
p = 0;
|
||||
writeBufferPos = 0;
|
||||
writeBuffer = (uint8_t*)malloc(writeBufferSize);
|
||||
if (!writeBuffer)
|
||||
{
|
||||
debug_printf( "Can't malloc %d bytes", (int)writeBufferSize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bytes_writer = write_bytes;
|
||||
|
||||
p += enc_write_uint32( qoi_sig);
|
||||
p += enc_write_uint32( desc->width);
|
||||
p += enc_write_uint32( desc->height);
|
||||
|
||||
p += enc_write_uint8( desc->channels );
|
||||
p += enc_write_uint8( desc->colorspace );
|
||||
|
||||
uint32_t lineBufferLen = desc->width * desc->channels;
|
||||
|
||||
repeat = 0;
|
||||
px_prev.rgba.r = 0;
|
||||
px_prev.rgba.g = 0;
|
||||
px_prev.rgba.b = 0;
|
||||
px_prev.rgba.a = 255;
|
||||
px = px_prev;
|
||||
|
||||
px_len = desc->width * desc->height * desc->channels;
|
||||
px_end = px_len - desc->channels;
|
||||
channels = desc->channels;
|
||||
|
||||
for (px_pos = 0; px_pos < px_len; px_pos += channels)
|
||||
{
|
||||
|
||||
uint32_t bufferPos = px_pos%lineBufferLen;
|
||||
uint32_t ypos = px_pos/lineBufferLen;
|
||||
if( get_row && bufferPos == 0 ) get_row( pixels, flip, desc->width, 1, ypos, qoienc );
|
||||
|
||||
if (channels == 4) {
|
||||
px = *(qoi_rgba_t *)(pixels + bufferPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
px.rgba.r = pixels[bufferPos + 0];
|
||||
px.rgba.g = pixels[bufferPos + 1];
|
||||
px.rgba.b = pixels[bufferPos + 2];
|
||||
}
|
||||
|
||||
if (px.v == px_prev.v)
|
||||
{
|
||||
repeat++;
|
||||
if (repeat == 62 || px_pos == px_end)
|
||||
{
|
||||
p += enc_write_uint8( (uint8_t)(QOI_OP_RUN | (repeat - 1)) );
|
||||
repeat = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int index_pos;
|
||||
|
||||
if (repeat > 0)
|
||||
{
|
||||
p += enc_write_uint8( (uint8_t)(QOI_OP_RUN | (repeat - 1)));
|
||||
repeat = 0;
|
||||
}
|
||||
|
||||
index_pos = QOI_COLOR_HASH(&px);
|
||||
|
||||
if (qoi_index[index_pos].v == px.v)
|
||||
{
|
||||
p += enc_write_uint8( (uint8_t)(QOI_OP_INDEX | index_pos) );
|
||||
}
|
||||
else
|
||||
{
|
||||
qoi_index[index_pos] = px;
|
||||
|
||||
if (px.rgba.a == px_prev.rgba.a)
|
||||
{
|
||||
signed char vr = px.rgba.r - px_prev.rgba.r;
|
||||
signed char vg = px.rgba.g - px_prev.rgba.g;
|
||||
signed char vb = px.rgba.b - px_prev.rgba.b;
|
||||
|
||||
signed char vg_r = vr - vg;
|
||||
signed char vg_b = vb - vg;
|
||||
|
||||
if ( vr > -3 && vr < 2 && vg > -3 && vg < 2 && vb > -3 && vb < 2 )
|
||||
{
|
||||
p += enc_write_uint8( (uint8_t)(QOI_OP_DIFF + ((vr + 2) << 4) + ((vg + 2) << 2) + (vb + 2)) );
|
||||
}
|
||||
else if ( vg_r > -9 && vg_r < 8 && vg > -33 && vg < 32 && vg_b > -9 && vg_b < 8 )
|
||||
{
|
||||
p += enc_write_uint8( (uint8_t)(QOI_OP_LUMA | (vg + 32)) );
|
||||
p += enc_write_uint8( (uint8_t)((vg_r + 8) << 4 | (vg_b + 8)) );
|
||||
}
|
||||
else
|
||||
{
|
||||
p += enc_write_uint8( QOI_OP_RGB );
|
||||
p += enc_write_uint8( px.rgba.r );
|
||||
p += enc_write_uint8( px.rgba.g );
|
||||
p += enc_write_uint8( px.rgba.b );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p += enc_write_uint8( QOI_OP_RGBA );
|
||||
p += enc_write_uint8( px.rgba.r );
|
||||
p += enc_write_uint8( px.rgba.g );
|
||||
p += enc_write_uint8( px.rgba.b );
|
||||
p += enc_write_uint8( px.rgba.a );
|
||||
}
|
||||
}
|
||||
}
|
||||
px_prev = px;
|
||||
}
|
||||
|
||||
for (i = 0; i < (int)sizeof(qoi_padding); i++)
|
||||
{
|
||||
p += enc_write_uint8( qoi_padding[i] );
|
||||
}
|
||||
|
||||
if( write_bytes )
|
||||
{
|
||||
if( writeBufferPos>0 ) write_bytes( writeBuffer, writeBufferPos );
|
||||
free( writeBuffer );
|
||||
}
|
||||
|
||||
free( qoi_index );
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef __LGFX_QOI_H__
|
||||
#define __LGFX_QOI_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Main Qoi object
|
||||
typedef struct _qoi_t qoi_t;
|
||||
// QOI image description header
|
||||
typedef struct _qoi_desc_t qoi_desc_t;
|
||||
|
||||
// Callback signatures
|
||||
typedef uint32_t (*lgfx_qoi_read_callback_t)(void *user_data, uint8_t *buf, uint32_t len);
|
||||
typedef void (*lgfx_qoi_draw_callback_t)(void *user_data, uint32_t x, uint32_t y, uint_fast8_t div_x, size_t len, const uint8_t* argb);
|
||||
|
||||
|
||||
typedef uint8_t *(*lgfx_qoi_encoder_get_row_func)(uint8_t *lineBuffer, int flip, int w, int h, int y, void *qoienc);
|
||||
// basic buffer/stream writer signature
|
||||
typedef int (*lfgx_qoi_writer_func)(uint8_t* buf, size_t buf_len);
|
||||
|
||||
// ---------------------
|
||||
// Basic read interfaces
|
||||
// ---------------------
|
||||
qoi_t *lgfx_qoi_new();
|
||||
|
||||
int lgfx_qoi_prepare(qoi_t *qoi, lgfx_qoi_read_callback_t read_cb, void* user_data);
|
||||
int lgfx_qoi_decomp(qoi_t *qoi, lgfx_qoi_draw_callback_t draw_cb);
|
||||
|
||||
void lgfx_qoi_destroy(qoi_t *qoi);
|
||||
void lgfx_qoi_reset(qoi_t *qoi);
|
||||
|
||||
uint32_t lgfx_qoi_get_width(qoi_t *qoi);
|
||||
uint32_t lgfx_qoi_get_height(qoi_t *qoi);
|
||||
|
||||
qoi_desc_t *lgfx_qoi_get_desc(qoi_t *qoi);
|
||||
|
||||
// ----------------------
|
||||
// Basic write interfaces
|
||||
// ----------------------
|
||||
|
||||
// write to buffer (will malloc)
|
||||
void *lgfx_qoi_encoder_write_fb(const void *lineBuffer, int w, int h, int num_chans, size_t *out_len, int flip, lgfx_qoi_encoder_get_row_func cb, void *qoienc);
|
||||
// write to callback (falls back to malloc if none provided)
|
||||
size_t lgfx_qoi_encoder_write_cb(const void *lineBuffer, uint32_t buflen, int w, int h, int num_chans, int flip, lgfx_qoi_encoder_get_row_func get_row, lfgx_qoi_writer_func write_bytes, void *qoienc);
|
||||
// encode
|
||||
size_t lgfx_qoi_encode(const void *lineBuffer, const qoi_desc_t *desc, int flip, lgfx_qoi_encoder_get_row_func get_row, lfgx_qoi_writer_func write_bytes, void *qoienc);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __LGFX_QOI_H__ */
|
||||
@@ -0,0 +1,892 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Richard Moore
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Special thanks to Nayuki (https://www.nayuki.io/) from which this library was
|
||||
* heavily inspired and compared against.
|
||||
*
|
||||
* See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp
|
||||
*/
|
||||
|
||||
/**
|
||||
* original source is here : https://github.com/ricmoo/QRCode/
|
||||
* Modified for LGFX by lovyan03, 2020
|
||||
*/
|
||||
|
||||
|
||||
#include "lgfx_qrcode.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../internal/alloca.h"
|
||||
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
//#pragma mark - Error Correction Lookup tables
|
||||
|
||||
#if LOCK_VERSION == 0
|
||||
|
||||
static const uint16_t NUM_ERROR_CORRECTION_CODEWORDS[4][40] = {
|
||||
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
||||
{ 10, 16, 26, 36, 48, 64, 72, 88, 110, 130, 150, 176, 198, 216, 240, 280, 308, 338, 364, 416, 442, 476, 504, 560, 588, 644, 700, 728, 784, 812, 868, 924, 980, 1036, 1064, 1120, 1204, 1260, 1316, 1372}, // Medium
|
||||
{ 7, 10, 15, 20, 26, 36, 40, 48, 60, 72, 80, 96, 104, 120, 132, 144, 168, 180, 196, 224, 224, 252, 270, 300, 312, 336, 360, 390, 420, 450, 480, 510, 540, 570, 570, 600, 630, 660, 720, 750}, // Low
|
||||
{ 17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308, 352, 384, 432, 480, 532, 588, 650, 700, 750, 816, 900, 960, 1050, 1110, 1200, 1260, 1350, 1440, 1530, 1620, 1710, 1800, 1890, 1980, 2100, 2220, 2310, 2430}, // High
|
||||
{ 13, 22, 36, 52, 72, 96, 108, 132, 160, 192, 224, 260, 288, 320, 360, 408, 448, 504, 546, 600, 644, 690, 750, 810, 870, 952, 1020, 1050, 1140, 1200, 1290, 1350, 1440, 1530, 1590, 1680, 1770, 1860, 1950, 2040}, // Quartile
|
||||
};
|
||||
|
||||
static const uint8_t NUM_ERROR_CORRECTION_BLOCKS[4][40] = {
|
||||
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
||||
// 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
||||
{ 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
|
||||
{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
|
||||
{ 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
|
||||
{ 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
|
||||
};
|
||||
|
||||
static const uint16_t NUM_RAW_DATA_MODULES[40] = {
|
||||
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
||||
208, 359, 567, 807, 1079, 1383, 1568, 1936, 2336, 2768, 3232, 3728, 4256, 4651, 5243, 5867, 6523,
|
||||
// 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
7211, 7931, 8683, 9252, 10068, 10916, 11796, 12708, 13652, 14628, 15371, 16411, 17483, 18587,
|
||||
// 32, 33, 34, 35, 36, 37, 38, 39, 40
|
||||
19723, 20891, 22091, 23008, 24272, 25568, 26896, 28256, 29648
|
||||
};
|
||||
|
||||
// @TODO: Put other LOCK_VERSIONS here
|
||||
#elif LOCK_VERSION == 3
|
||||
|
||||
static const int16_t NUM_ERROR_CORRECTION_CODEWORDS[4] = {
|
||||
26, 15, 44, 36
|
||||
};
|
||||
|
||||
static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4] = {
|
||||
1, 1, 2, 2
|
||||
};
|
||||
|
||||
static const uint16_t NUM_RAW_DATA_MODULES = 567;
|
||||
|
||||
#else
|
||||
|
||||
#error Unsupported LOCK_VERSION (add it...)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
static int max(int a, int b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
/*
|
||||
static int abs(int value) {
|
||||
if (value < 0) { return -value; }
|
||||
return value;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
//#pragma mark - Mode testing and conversion
|
||||
|
||||
static int8_t getAlphanumeric(char c) {
|
||||
|
||||
if (c >= '0' && c <= '9') { return (c - '0'); }
|
||||
if (c >= 'A' && c <= 'Z') { return (c - 'A' + 10); }
|
||||
|
||||
switch (c) {
|
||||
case ' ': return 36;
|
||||
case '$': return 37;
|
||||
case '%': return 38;
|
||||
case '*': return 39;
|
||||
case '+': return 40;
|
||||
case '-': return 41;
|
||||
case '.': return 42;
|
||||
case '/': return 43;
|
||||
case ':': return 44;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool isAlphanumeric(const char *text, uint16_t length) {
|
||||
while (length != 0) {
|
||||
if (getAlphanumeric(text[--length]) == -1) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static bool isNumeric(const char *text, uint16_t length) {
|
||||
while (length != 0) {
|
||||
char c = text[--length];
|
||||
if (c < '0' || c > '9') { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//#pragma mark - Counting
|
||||
|
||||
// We store the following tightly packed (less 8) in modeInfo
|
||||
// <=9 <=26 <= 40
|
||||
// NUMERIC ( 10, 12, 14);
|
||||
// ALPHANUMERIC ( 9, 11, 13);
|
||||
// BYTE ( 8, 16, 16);
|
||||
static char getModeBits(uint8_t version, uint8_t mode) {
|
||||
// Note: We use 15 instead of 16; since 15 doesn't exist and we cannot store 16 (8 + 8) in 3 bits
|
||||
// hex(int("".join(reversed([('00' + bin(x - 8)[2:])[-3:] for x in [10, 9, 8, 12, 11, 15, 14, 13, 15]])), 2))
|
||||
unsigned int modeInfo = 0x7bbb80a;
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 9
|
||||
if (version > 9) { modeInfo >>= 9; }
|
||||
#endif
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 26
|
||||
if (version > 26) { modeInfo >>= 9; }
|
||||
#endif
|
||||
|
||||
char result = 8 + ((modeInfo >> (3 * mode)) & 0x07);
|
||||
if (result == 15) { result = 16; }
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
//#pragma mark - BitBucket
|
||||
|
||||
typedef struct BitBucket {
|
||||
uint32_t bitOffsetOrWidth;
|
||||
uint16_t capacityBytes;
|
||||
uint8_t *data;
|
||||
} BitBucket;
|
||||
|
||||
/*
|
||||
void bb_dump(BitBucket *bitBuffer) {
|
||||
printf("Buffer: ");
|
||||
for (uint32_t i = 0; i < bitBuffer->capacityBytes; i++) {
|
||||
printf("%02x", bitBuffer->data[i]);
|
||||
if ((i % 4) == 3) { printf(" "); }
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
*/
|
||||
|
||||
static uint16_t bb_getGridSizeBytes(uint8_t size) {
|
||||
return (((size * size) + 7) >> 3);
|
||||
}
|
||||
|
||||
static uint16_t bb_getBufferSizeBytes(uint32_t bits) {
|
||||
return ((bits + 7) >> 3);
|
||||
}
|
||||
|
||||
static void bb_initBuffer(BitBucket *bitBuffer, uint8_t *data, int32_t capacityBytes) {
|
||||
bitBuffer->bitOffsetOrWidth = 0;
|
||||
bitBuffer->capacityBytes = capacityBytes;
|
||||
bitBuffer->data = data;
|
||||
|
||||
memset(data, 0, bitBuffer->capacityBytes);
|
||||
}
|
||||
|
||||
static void bb_initGrid(BitBucket *bitGrid, uint8_t *data, uint8_t size) {
|
||||
bitGrid->bitOffsetOrWidth = size;
|
||||
bitGrid->capacityBytes = bb_getGridSizeBytes(size);
|
||||
bitGrid->data = data;
|
||||
|
||||
memset(data, 0, bitGrid->capacityBytes);
|
||||
}
|
||||
|
||||
static bool bb_appendBits(BitBucket *bitBuffer, uint32_t val, uint8_t length) {
|
||||
uint32_t offset = bitBuffer->bitOffsetOrWidth;
|
||||
for (int_fast8_t i = length - 1; i >= 0; i--, offset++) {
|
||||
size_t index = offset >> 3;
|
||||
if (bitBuffer->capacityBytes <= index) return false;
|
||||
bitBuffer->data[index] |= ((val >> i) & 1) << (7 - (offset & 7));
|
||||
}
|
||||
bitBuffer->bitOffsetOrWidth = offset;
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
void bb_setBits(BitBucket *bitBuffer, uint32_t val, int offset, uint8_t length) {
|
||||
for (int8_t i = length - 1; i >= 0; i--, offset++) {
|
||||
bitBuffer->data[offset >> 3] |= ((val >> i) & 1) << (7 - (offset & 7));
|
||||
}
|
||||
}
|
||||
*/
|
||||
static void bb_setBit(BitBucket *bitGrid, uint_fast8_t x, uint_fast8_t y, bool on) {
|
||||
uint32_t offset = y * bitGrid->bitOffsetOrWidth + x;
|
||||
uint8_t mask = 1 << (7 - (offset & 0x07));
|
||||
if (on) {
|
||||
bitGrid->data[offset >> 3] |= mask;
|
||||
} else {
|
||||
bitGrid->data[offset >> 3] &= ~mask;
|
||||
}
|
||||
}
|
||||
|
||||
static void bb_invertBit(BitBucket *bitGrid, uint_fast8_t x, uint_fast8_t y, bool invert) {
|
||||
uint32_t offset = y * bitGrid->bitOffsetOrWidth + x;
|
||||
uint8_t mask = 1 << (7 - (offset & 0x07));
|
||||
bool on = ((bitGrid->data[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0);
|
||||
if (on ^ invert) {
|
||||
bitGrid->data[offset >> 3] |= mask;
|
||||
} else {
|
||||
bitGrid->data[offset >> 3] &= ~mask;
|
||||
}
|
||||
}
|
||||
|
||||
static bool bb_getBit(BitBucket *bitGrid, uint_fast8_t x, uint_fast8_t y) {
|
||||
uint32_t offset = y * bitGrid->bitOffsetOrWidth + x;
|
||||
return (bitGrid->data[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0;
|
||||
}
|
||||
|
||||
|
||||
//#pragma mark - Drawing Patterns
|
||||
|
||||
// XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical
|
||||
// properties, calling applyMask(m) twice with the same value is equivalent to no change at all.
|
||||
// This means it is possible to apply a mask, undo it, and try another mask. Note that a final
|
||||
// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
|
||||
static void applyMask(BitBucket *modules, BitBucket *isFunction, uint8_t mask) {
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
for (uint_fast8_t y = 0; y < size; y++) {
|
||||
for (uint_fast8_t x = 0; x < size; x++) {
|
||||
if (bb_getBit(isFunction, x, y)) { continue; }
|
||||
|
||||
bool invert = 0;
|
||||
switch (mask) {
|
||||
case 0: invert = ((x + y) & 1) == 0; break;
|
||||
case 1: invert = (y & 1) == 0; break;
|
||||
case 2: invert = (x % 3) == 0; break;
|
||||
case 3: invert = ((x + y) % 3) == 0; break;
|
||||
case 4: invert = ((x / 3 + (y >> 1)) & 1) == 0; break;
|
||||
case 5: invert = (((x * y) & 1) + x * y % 3) == 0; break;
|
||||
case 6: invert = ((((x * y) & 1) + x * y % 3) & 1) == 0;break;
|
||||
case 7: invert = ((((x + y) & 1) + x * y % 3) & 1) == 0;break;
|
||||
}
|
||||
bb_invertBit(modules, x, y, invert);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setFunctionModule(BitBucket *modules, BitBucket *isFunction, uint_fast8_t x, uint_fast8_t y, bool on) {
|
||||
bb_setBit(modules, x, y, on);
|
||||
bb_setBit(isFunction, x, y, true);
|
||||
}
|
||||
|
||||
// Draws a 9*9 finder pattern including the border separator, with the center module at (x, y).
|
||||
static void drawFinderPattern(BitBucket *modules, BitBucket *isFunction, uint_fast8_t x, uint_fast8_t y) {
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
for (int_fast8_t i = -4; i <= 4; i++) {
|
||||
for (int_fast8_t j = -4; j <= 4; j++) {
|
||||
uint_fast8_t dist = max(abs(i), abs(j)); // Chebyshev/infinity norm
|
||||
int_fast16_t xx = x + j, yy = y + i;
|
||||
if (0 <= xx && xx < size && 0 <= yy && yy < size) {
|
||||
setFunctionModule(modules, isFunction, xx, yy, dist != 2 && dist != 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draws a 5*5 alignment pattern, with the center module at (x, y).
|
||||
static void drawAlignmentPattern(BitBucket *modules, BitBucket *isFunction, uint_fast8_t x, uint_fast8_t y) {
|
||||
for (int_fast8_t i = -2; i <= 2; i++) {
|
||||
for (int_fast8_t j = -2; j <= 2; j++) {
|
||||
setFunctionModule(modules, isFunction, x + j, y + i, max(abs(i), abs(j)) != 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draws two copies of the format bits (with its own error correction code)
|
||||
// based on the given mask and this object's error correction level field.
|
||||
static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ecc, uint8_t mask) {
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Calculate error correction code and pack bits
|
||||
uint32_t data = ecc << 3 | mask; // errCorrLvl is uint2, mask is uint3
|
||||
uint32_t rem = data;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
||||
}
|
||||
|
||||
data = data << 10 | rem;
|
||||
data ^= 0x5412; // uint15
|
||||
|
||||
// Draw first copy
|
||||
for (uint_fast8_t i = 0; i <= 5; i++) {
|
||||
setFunctionModule(modules, isFunction, 8, i, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
setFunctionModule(modules, isFunction, 8, 7, ((data >> 6) & 1) != 0);
|
||||
setFunctionModule(modules, isFunction, 8, 8, ((data >> 7) & 1) != 0);
|
||||
setFunctionModule(modules, isFunction, 7, 8, ((data >> 8) & 1) != 0);
|
||||
|
||||
for (int_fast8_t i = 9; i < 15; i++) {
|
||||
setFunctionModule(modules, isFunction, 14 - i, 8, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
// Draw second copy
|
||||
for (int_fast8_t i = 0; i <= 7; i++) {
|
||||
setFunctionModule(modules, isFunction, size - 1 - i, 8, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
for (int_fast8_t i = 8; i < 15; i++) {
|
||||
setFunctionModule(modules, isFunction, 8, size - 15 + i, ((data >> i) & 1) != 0);
|
||||
}
|
||||
|
||||
setFunctionModule(modules, isFunction, 8, size - 8, true);
|
||||
}
|
||||
|
||||
|
||||
// Draws two copies of the version bits (with its own error correction code),
|
||||
// based on this object's version field (which only has an effect for 7 <= version <= 40).
|
||||
static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t version) {
|
||||
|
||||
int8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
#if LOCK_VERSION != 0 && LOCK_VERSION < 7
|
||||
return;
|
||||
|
||||
#else
|
||||
if (version < 7) { return; }
|
||||
|
||||
// Calculate error correction code and pack bits
|
||||
uint32_t rem = version; // version is uint6, in the range [7, 40]
|
||||
for (uint_fast8_t i = 0; i < 12; i++) {
|
||||
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
||||
}
|
||||
|
||||
uint32_t data = version << 12 | rem; // uint18
|
||||
|
||||
// Draw two copies
|
||||
for (uint_fast8_t i = 0; i < 18; i++) {
|
||||
bool bit = ((data >> i) & 1) != 0;
|
||||
uint8_t a = size - 11 + i % 3, b = i / 3;
|
||||
setFunctionModule(modules, isFunction, a, b, bit);
|
||||
setFunctionModule(modules, isFunction, b, a, bit);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint8_t version, uint8_t ecc) {
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Draw the horizontal and vertical timing patterns
|
||||
for (uint_fast8_t i = 0; i < size; i++) {
|
||||
setFunctionModule(modules, isFunction, 6, i, (i & 1) == 0);
|
||||
setFunctionModule(modules, isFunction, i, 6, (i & 1) == 0);
|
||||
}
|
||||
|
||||
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
||||
drawFinderPattern(modules, isFunction, 3, 3);
|
||||
drawFinderPattern(modules, isFunction, size - 4, 3);
|
||||
drawFinderPattern(modules, isFunction, 3, size - 4);
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION > 1
|
||||
|
||||
if (version > 1) {
|
||||
|
||||
// Draw the numerous alignment patterns
|
||||
|
||||
uint_fast8_t alignCount = version / 7 + 2;
|
||||
uint_fast8_t step;
|
||||
if (version != 32) {
|
||||
step = (version * 4 + alignCount * 2 + 1) / (2 * alignCount - 2) * 2; // ceil((size - 13) / (2*numAlign - 2)) * 2
|
||||
} else { // C-C-C-Combo breaker!
|
||||
step = 26;
|
||||
}
|
||||
|
||||
uint_fast8_t alignPositionIndex = alignCount - 1;
|
||||
uint8_t* alignPosition = (uint8_t*)alloca(alignCount);
|
||||
|
||||
alignPosition[0] = 6;
|
||||
|
||||
uint8_t size_ = version * 4 + 17;
|
||||
for (uint_fast8_t i = 0, pos = size_ - 7; i < alignCount - 1; i++, pos -= step) {
|
||||
alignPosition[alignPositionIndex--] = pos;
|
||||
}
|
||||
|
||||
for (uint_fast8_t i = 0; i < alignCount; i++) {
|
||||
for (uint_fast8_t j = 0; j < alignCount; j++) {
|
||||
if ((i == 0 && j == 0) || (i == 0 && j == alignCount - 1) || (i == alignCount - 1 && j == 0)) {
|
||||
continue; // Skip the three finder corners
|
||||
} else {
|
||||
drawAlignmentPattern(modules, isFunction, alignPosition[i], alignPosition[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Draw configuration data
|
||||
drawFormatBits(modules, isFunction, ecc, 0); // Dummy mask value; overwritten later in the constructor
|
||||
drawVersion(modules, isFunction, version);
|
||||
}
|
||||
|
||||
|
||||
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
||||
// data area of this QR Code symbol. Function modules need to be marked off before this is called.
|
||||
static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *codewords) {
|
||||
|
||||
uint32_t bitLength = codewords->bitOffsetOrWidth;
|
||||
uint8_t *data = codewords->data;
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Bit index into the data
|
||||
uint32_t i = 0;
|
||||
|
||||
// Do the funny zigzag scan
|
||||
for (int_fast16_t right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
||||
if (right == 6) { right = 5; }
|
||||
|
||||
for (uint_fast8_t vert = 0; vert < size; vert++) { // Vertical counter
|
||||
for (int j = 0; j < 2; j++) {
|
||||
uint8_t x = right - j; // Actual x coordinate
|
||||
bool upwards = ((right & 2) == 0) ^ (x < 6);
|
||||
uint8_t y = upwards ? size - 1 - vert : vert; // Actual y coordinate
|
||||
if (!bb_getBit(isFunction, x, y) && i < bitLength) {
|
||||
bb_setBit(modules, x, y, ((data[i >> 3] >> (7 - (i & 7))) & 1) != 0);
|
||||
i++;
|
||||
}
|
||||
// If there are any remainder bits (0 to 7), they are already
|
||||
// set to 0/false/white when the grid of modules was initialized
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//#pragma mark - Penalty Calculation
|
||||
|
||||
#define PENALTY_N1 3
|
||||
#define PENALTY_N2 3
|
||||
#define PENALTY_N3 40
|
||||
#define PENALTY_N4 10
|
||||
|
||||
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
||||
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
||||
// @TODO: This can be optimized by working with the bytes instead of bits.
|
||||
static uint32_t getPenaltyScore(BitBucket *modules) {
|
||||
uint32_t result = 0;
|
||||
|
||||
uint8_t size = modules->bitOffsetOrWidth;
|
||||
|
||||
// Adjacent modules in row having same color
|
||||
for (uint_fast8_t y = 0; y < size; y++) {
|
||||
|
||||
bool colorX = bb_getBit(modules, 0, y);
|
||||
for (uint_fast8_t x = 1, runX = 1; x < size; x++) {
|
||||
bool cx = bb_getBit(modules, x, y);
|
||||
if (cx != colorX) {
|
||||
colorX = cx;
|
||||
runX = 1;
|
||||
|
||||
} else {
|
||||
runX++;
|
||||
if (runX == 5) {
|
||||
result += PENALTY_N1;
|
||||
} else if (runX > 5) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adjacent modules in column having same color
|
||||
for (uint_fast8_t x = 0; x < size; x++) {
|
||||
bool colorY = bb_getBit(modules, x, 0);
|
||||
for (uint_fast8_t y = 1, runY = 1; y < size; y++) {
|
||||
bool cy = bb_getBit(modules, x, y);
|
||||
if (cy != colorY) {
|
||||
colorY = cy;
|
||||
runY = 1;
|
||||
} else {
|
||||
runY++;
|
||||
if (runY == 5) {
|
||||
result += PENALTY_N1;
|
||||
} else if (runY > 5) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t black = 0;
|
||||
for (uint_fast8_t y = 0; y < size; y++) {
|
||||
uint_fast16_t bitsRow = 0, bitsCol = 0;
|
||||
for (uint_fast8_t x = 0; x < size; x++) {
|
||||
bool color = bb_getBit(modules, x, y);
|
||||
|
||||
// 2*2 blocks of modules having same color
|
||||
if (x > 0 && y > 0) {
|
||||
bool colorUL = bb_getBit(modules, x - 1, y - 1);
|
||||
bool colorUR = bb_getBit(modules, x, y - 1);
|
||||
bool colorL = bb_getBit(modules, x - 1, y);
|
||||
if (color == colorUL && color == colorUR && color == colorL) {
|
||||
result += PENALTY_N2;
|
||||
}
|
||||
}
|
||||
|
||||
// Finder-like pattern in rows and columns
|
||||
bitsRow = ((bitsRow << 1) & 0x7FF) | color;
|
||||
bitsCol = ((bitsCol << 1) & 0x7FF) | bb_getBit(modules, y, x);
|
||||
|
||||
// Needs 11 bits accumulated
|
||||
if (x >= 10) {
|
||||
if (bitsRow == 0x05D || bitsRow == 0x5D0) {
|
||||
result += PENALTY_N3;
|
||||
}
|
||||
if (bitsCol == 0x05D || bitsCol == 0x5D0) {
|
||||
result += PENALTY_N3;
|
||||
}
|
||||
}
|
||||
|
||||
// Balance of black and white modules
|
||||
if (color) { black++; }
|
||||
}
|
||||
}
|
||||
|
||||
// Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
|
||||
uint_fast16_t total = size * size;
|
||||
for (uint_fast16_t k = 0; black * 20 < (9 - k) * total || black * 20 > (11 + k) * total; k++) {
|
||||
result += PENALTY_N4;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
//#pragma mark - Reed-Solomon Generator
|
||||
|
||||
static uint8_t rs_multiply(uint_fast8_t x, uint_fast8_t y) {
|
||||
// Russian peasant multiplication
|
||||
// See: https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
|
||||
uint16_t z = 0;
|
||||
for (int_fast8_t i = 7; i >= 0; i--) {
|
||||
z = (z << 1) ^ ((z >> 7) * 0x11D);
|
||||
z ^= ((y >> i) & 1) * x;
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
static void rs_init(uint8_t degree, uint8_t *coeff) {
|
||||
memset(coeff, 0, degree);
|
||||
coeff[degree - 1] = 1;
|
||||
|
||||
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
||||
// drop the highest term, and store the rest of the coefficients in order of descending powers.
|
||||
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
||||
uint16_t root = 1;
|
||||
for (uint_fast8_t i = 0; i < degree; i++) {
|
||||
// Multiply the current product by (x - r^i)
|
||||
for (uint_fast8_t j = 0; j < degree; j++) {
|
||||
coeff[j] = rs_multiply(coeff[j], root);
|
||||
if (j + 1 < degree) {
|
||||
coeff[j] ^= coeff[j + 1];
|
||||
}
|
||||
}
|
||||
root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D)
|
||||
}
|
||||
}
|
||||
|
||||
static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8_t length, uint8_t *result, uint8_t stride) {
|
||||
// Compute the remainder by performing polynomial division
|
||||
|
||||
//for (uint8_t i = 0; i < degree; i++) { result[] = 0; }
|
||||
//memset(result, 0, degree);
|
||||
|
||||
for (uint_fast8_t i = 0; i < length; i++) {
|
||||
uint8_t factor = data[i] ^ result[0];
|
||||
for (uint_fast8_t j = 1; j < degree; j++) {
|
||||
result[(j - 1) * stride] = result[j * stride];
|
||||
}
|
||||
result[(degree - 1) * stride] = 0;
|
||||
|
||||
for (uint_fast8_t j = 0; j < degree; j++) {
|
||||
result[j * stride] ^= rs_multiply(coeff[j], factor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//#pragma mark - QrCode
|
||||
|
||||
static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) {
|
||||
int8_t mode = MODE_BYTE;
|
||||
|
||||
if (isNumeric((char*)text, length)) {
|
||||
mode = MODE_NUMERIC;
|
||||
bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_NUMERIC));
|
||||
|
||||
uint16_t accumData = 0;
|
||||
uint8_t accumCount = 0;
|
||||
for (uint16_t i = 0; i < length; i++) {
|
||||
accumData = accumData * 10 + ((char)(text[i]) - '0');
|
||||
accumCount++;
|
||||
if (accumCount == 3) {
|
||||
if (!bb_appendBits(dataCodewords, accumData, 10)) return -1;
|
||||
accumData = 0;
|
||||
accumCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 1 or 2 digits remaining
|
||||
if (accumCount > 0) {
|
||||
bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1);
|
||||
}
|
||||
|
||||
} else if (isAlphanumeric((char*)text, length)) {
|
||||
mode = MODE_ALPHANUMERIC;
|
||||
bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_ALPHANUMERIC));
|
||||
|
||||
uint16_t accumData = 0;
|
||||
uint8_t accumCount = 0;
|
||||
for (uint16_t i = 0; i < length; i++) {
|
||||
accumData = accumData * 45 + getAlphanumeric((char)(text[i]));
|
||||
accumCount++;
|
||||
if (accumCount == 2) {
|
||||
if (!bb_appendBits(dataCodewords, accumData, 11)) return -1;
|
||||
accumData = 0;
|
||||
accumCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 1 character remaining
|
||||
if (accumCount > 0) {
|
||||
bb_appendBits(dataCodewords, accumData, 6);
|
||||
}
|
||||
|
||||
} else {
|
||||
bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4);
|
||||
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE));
|
||||
for (uint16_t i = 0; i < length; i++) {
|
||||
if (!bb_appendBits(dataCodewords, (char)(text[i]), 8)) return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//bb_setBits(dataCodewords, length, 4, getModeBits(version, mode));
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
static bool performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data) {
|
||||
|
||||
// See: http://www.thonky.com/qr-code-tutorial/structure-final-message
|
||||
|
||||
#if LOCK_VERSION == 0
|
||||
uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc][version - 1];
|
||||
uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc][version - 1];
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1];
|
||||
#else
|
||||
uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc];
|
||||
uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc];
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES;
|
||||
#endif
|
||||
|
||||
uint8_t blockEccLen = totalEcc / numBlocks;
|
||||
uint8_t numShortBlocks = numBlocks - moduleCount / 8 % numBlocks;
|
||||
uint8_t shortBlockLen = moduleCount / 8 / numBlocks;
|
||||
|
||||
uint8_t shortDataBlockLen = shortBlockLen - blockEccLen;
|
||||
|
||||
uint8_t* result = (uint8_t*)alloca(data->capacityBytes);
|
||||
memset(result, 0, data->capacityBytes);
|
||||
|
||||
uint8_t* coeff = (uint8_t*)alloca(blockEccLen);
|
||||
rs_init(blockEccLen, coeff);
|
||||
|
||||
uint16_t offset = 0;
|
||||
uint8_t *dataBytes = data->data;
|
||||
|
||||
|
||||
// Interleave all short blocks
|
||||
for (uint8_t i = 0; i < shortDataBlockLen; i++) {
|
||||
uint16_t index = i;
|
||||
uint8_t stride = shortDataBlockLen;
|
||||
for (uint_fast8_t blockNum = 0; blockNum < numBlocks; blockNum++) {
|
||||
if (offset == data->capacityBytes) return false;
|
||||
result[offset++] = dataBytes[index];
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
if (blockNum == numShortBlocks) { stride++; }
|
||||
#endif
|
||||
index += stride;
|
||||
}
|
||||
}
|
||||
|
||||
// Version less than 5 only have short blocks
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
{
|
||||
// Interleave long blocks
|
||||
uint16_t index = shortDataBlockLen * (numShortBlocks + 1);
|
||||
uint8_t stride = shortDataBlockLen;
|
||||
for (uint8_t blockNum = 0; blockNum < numBlocks - numShortBlocks; blockNum++) {
|
||||
if (offset == data->capacityBytes) return false;
|
||||
result[offset++] = dataBytes[index];
|
||||
|
||||
if (blockNum == 0) { stride++; }
|
||||
index += stride;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Add all ecc blocks, interleaved
|
||||
uint8_t blockSize = shortDataBlockLen;
|
||||
for (uint_fast8_t blockNum = 0; blockNum < numBlocks; blockNum++) {
|
||||
|
||||
#if LOCK_VERSION == 0 || LOCK_VERSION >= 5
|
||||
if (blockNum == numShortBlocks) { blockSize++; }
|
||||
#endif
|
||||
if (offset + blockNum >= data->capacityBytes) return false;
|
||||
rs_getRemainder(blockEccLen, coeff, dataBytes, blockSize, &result[offset + blockNum], numBlocks);
|
||||
dataBytes += blockSize;
|
||||
}
|
||||
|
||||
memcpy(data->data, result, data->capacityBytes);
|
||||
data->bitOffsetOrWidth = moduleCount;
|
||||
return true;
|
||||
}
|
||||
|
||||
// We store the Format bits tightly packed into a single byte (each of the 4 modes is 2 bits)
|
||||
// The format bits can be determined by ECC_FORMAT_BITS >> (2 * ecc)
|
||||
static const uint8_t ECC_FORMAT_BITS = (0x02 << 6) | (0x03 << 4) | (0x00 << 2) | (0x01 << 0);
|
||||
|
||||
|
||||
//#pragma mark - Public QRCode functions
|
||||
|
||||
uint16_t lgfx_qrcode_getBufferSize(uint8_t version) {
|
||||
return bb_getGridSizeBytes(4 * version + 17);
|
||||
}
|
||||
|
||||
// @TODO: Return error if data is too big.
|
||||
int8_t lgfx_qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length) {
|
||||
uint8_t size = version * 4 + 17;
|
||||
qrcode->version = version;
|
||||
qrcode->size = size;
|
||||
qrcode->ecc = ecc;
|
||||
qrcode->modules = modules;
|
||||
|
||||
uint8_t eccFormatBits = (ECC_FORMAT_BITS >> (2 * ecc)) & 0x03;
|
||||
|
||||
#if LOCK_VERSION == 0
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1];
|
||||
uint16_t dataCapacity = (moduleCount >> 3) - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits][version - 1];
|
||||
#else
|
||||
version = LOCK_VERSION;
|
||||
uint16_t moduleCount = NUM_RAW_DATA_MODULES;
|
||||
uint16_t dataCapacity = (moduleCount >> 3) - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits];
|
||||
#endif
|
||||
|
||||
struct BitBucket codewords;
|
||||
int32_t codewordLen = bb_getBufferSizeBytes(moduleCount);
|
||||
uint8_t* codewordBytes = (uint8_t*)alloca(codewordLen);
|
||||
bb_initBuffer(&codewords, codewordBytes, codewordLen);
|
||||
|
||||
// Place the data code words into the buffer
|
||||
int8_t mode = encodeDataCodewords(&codewords, data, length, version);
|
||||
|
||||
if (mode < 0) { return -1; }
|
||||
qrcode->mode = mode;
|
||||
|
||||
// Add terminator and pad up to a byte if applicable
|
||||
int32_t padding = (dataCapacity * 8) - codewords.bitOffsetOrWidth;
|
||||
if (padding < 0) return -1;
|
||||
if (padding > 4) { padding = 4; }
|
||||
bb_appendBits(&codewords, 0, padding);
|
||||
bb_appendBits(&codewords, 0, (8 - (codewords.bitOffsetOrWidth & 7)) & 7);
|
||||
|
||||
// Pad with alternate bytes until data capacity is reached
|
||||
for (uint8_t padByte = 0xEC; codewords.bitOffsetOrWidth < (dataCapacity * 8); padByte ^= 0xEC ^ 0x11) {
|
||||
bb_appendBits(&codewords, padByte, 8);
|
||||
}
|
||||
|
||||
BitBucket modulesGrid;
|
||||
bb_initGrid(&modulesGrid, modules, size);
|
||||
|
||||
BitBucket isFunctionGrid;
|
||||
uint8_t* isFunctionGridBytes = (uint8_t*)alloca(bb_getGridSizeBytes(size));
|
||||
bb_initGrid(&isFunctionGrid, isFunctionGridBytes, size);
|
||||
|
||||
// Draw function patterns, draw all codewords, do masking
|
||||
drawFunctionPatterns(&modulesGrid, &isFunctionGrid, version, eccFormatBits);
|
||||
if (!performErrorCorrection(version, eccFormatBits, &codewords)) return -1;
|
||||
drawCodewords(&modulesGrid, &isFunctionGrid, &codewords);
|
||||
|
||||
// Find the best (lowest penalty) mask
|
||||
uint8_t mask = 0;
|
||||
uint32_t minPenalty = ~0u;
|
||||
for (uint_fast8_t i = 0; i < 8; i++) {
|
||||
drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, i);
|
||||
applyMask(&modulesGrid, &isFunctionGrid, i);
|
||||
uint32_t penalty = getPenaltyScore(&modulesGrid);
|
||||
if (penalty < minPenalty) {
|
||||
mask = i;
|
||||
minPenalty = penalty;
|
||||
}
|
||||
applyMask(&modulesGrid, &isFunctionGrid, i); // Undoes the mask due to XOR
|
||||
}
|
||||
|
||||
qrcode->mask = mask;
|
||||
|
||||
// Overwrite old format bits
|
||||
drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, mask);
|
||||
|
||||
// Apply the final choice of mask
|
||||
applyMask(&modulesGrid, &isFunctionGrid, mask);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int8_t lgfx_qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data) {
|
||||
return lgfx_qrcode_initBytes(qrcode, modules, version, ecc, (uint8_t*)data, strlen(data));
|
||||
}
|
||||
|
||||
bool lgfx_qrcode_getModule(QRCode *qrcode, uint_fast8_t x, uint_fast8_t y) {
|
||||
if (x >= qrcode->size || y >= qrcode->size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t offset = y * qrcode->size + x;
|
||||
return qrcode->modules[offset >> 3] & (1 << (7 - (offset & 0x07)));
|
||||
}
|
||||
|
||||
/*
|
||||
uint8_t lgfx_qrcode_getHexLength(QRCode *qrcode) {
|
||||
return ((qrcode->size * qrcode->size) + 7) / 4;
|
||||
}
|
||||
|
||||
void lgfx_qrcode_getHex(QRCode *qrcode, char *result) {
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Richard Moore
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Special thanks to Nayuki (https://www.nayuki.io/) from which this library was
|
||||
* heavily inspired and compared against.
|
||||
*
|
||||
* See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp
|
||||
*/
|
||||
|
||||
/**
|
||||
* original source is here : https://github.com/ricmoo/QRCode/
|
||||
* Modified for LGFX by lovyan03, 2020
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _LGFX_QRCODE_H_
|
||||
#define _LGFX_QRCODE_H_
|
||||
|
||||
#ifndef __cplusplus
|
||||
typedef unsigned char bool;
|
||||
static const bool false = 0;
|
||||
static const bool true = 1;
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
// QR Code Format Encoding
|
||||
#define MODE_NUMERIC 0
|
||||
#define MODE_ALPHANUMERIC 1
|
||||
#define MODE_BYTE 2
|
||||
|
||||
|
||||
// Error Correction Code Levels
|
||||
#define ECC_LOW 0
|
||||
#define ECC_MEDIUM 1
|
||||
#define ECC_QUARTILE 2
|
||||
#define ECC_HIGH 3
|
||||
|
||||
|
||||
// If set to non-zero, this library can ONLY produce QR codes at that version
|
||||
// This saves a lot of dynamic memory, as the codeword tables are skipped
|
||||
#ifndef LOCK_VERSION
|
||||
#define LOCK_VERSION 0
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct QRCode {
|
||||
uint8_t version;
|
||||
uint8_t size;
|
||||
uint8_t ecc;
|
||||
uint8_t mode;
|
||||
uint8_t mask;
|
||||
uint8_t *modules;
|
||||
} QRCode;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
|
||||
uint16_t lgfx_qrcode_getBufferSize(uint8_t version);
|
||||
|
||||
int8_t lgfx_qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data);
|
||||
int8_t lgfx_qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length);
|
||||
|
||||
bool lgfx_qrcode_getModule(QRCode *qrcode, uint_fast8_t x, uint_fast8_t y);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
#endif /* __QRCODE_H_ */
|
||||
@@ -0,0 +1,989 @@
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ TJpgDec - Tiny JPEG Decompressor R0.01c (C)ChaN, 2019
|
||||
/-----------------------------------------------------------------------------/
|
||||
/ The TJpgDec is a generic JPEG decompressor module for tiny embedded systems.
|
||||
/ This is a free software that opened for education, research and commercial
|
||||
/ developments under license policy of following terms.
|
||||
/
|
||||
/ Copyright (C) 2019, ChaN, all right reserved.
|
||||
/
|
||||
/ * The TJpgDec module is a free software and there is NO WARRANTY.
|
||||
/ * No restriction on use. You can use, modify and redistribute it for
|
||||
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
|
||||
/ * Redistributions of source code must retain the above copyright notice.
|
||||
/
|
||||
/-----------------------------------------------------------------------------/
|
||||
/ Oct 04, 2011 R0.01 First release.
|
||||
/ Feb 19, 2012 R0.01a Fixed decompression fails when scan starts with an escape seq.
|
||||
/ Sep 03, 2012 R0.01b Added JD_TBLCLIP option.
|
||||
/ Mar 16, 2019 R0.01c Supprted stdint.h.
|
||||
/-----------------------------------------------------------------------------/
|
||||
/ original source is here : http://elm-chan.org/fsw/tjpgd/00index.html
|
||||
/
|
||||
/ Modified for LGFX by lovyan03, 2020
|
||||
/ add support grayscale jpeg
|
||||
/ add bayer pattern
|
||||
/ tweak for 32bit processor
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
#include "lgfx_tjpgd.h"
|
||||
|
||||
#include <string.h> // for memcpy memset
|
||||
|
||||
|
||||
/*-----------------------------------------------*/
|
||||
/* Zigzag-order to raster-order conversion table */
|
||||
/*-----------------------------------------------*/
|
||||
|
||||
//#define ZIG(n) Zig[n]
|
||||
|
||||
static const uint8_t Zig[64] = { /* Zigzag-order to raster-order conversion table */
|
||||
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
|
||||
12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
|
||||
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
|
||||
58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*-------------------------------------------------*/
|
||||
/* Input scale factor of Arai algorithm */
|
||||
/* (scaled up 16 bits for fixed point operations) */
|
||||
/*-------------------------------------------------*/
|
||||
|
||||
//#define IPSF(n) Ipsf[n]
|
||||
|
||||
static const uint16_t Ipsf[64] = { /* See also aa_idct.png */
|
||||
(uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
|
||||
(uint16_t)(1.38704*8192), (uint16_t)(1.92388*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.08979*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.38268*8192),
|
||||
(uint16_t)(1.30656*8192), (uint16_t)(1.81226*8192), (uint16_t)(1.70711*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.36048*8192),
|
||||
(uint16_t)(1.17588*8192), (uint16_t)(1.63099*8192), (uint16_t)(1.53636*8192), (uint16_t)(1.38268*8192), (uint16_t)(1.17588*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.32442*8192),
|
||||
(uint16_t)(1.00000*8192), (uint16_t)(1.38704*8192), (uint16_t)(1.30656*8192), (uint16_t)(1.17588*8192), (uint16_t)(1.00000*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.27590*8192),
|
||||
(uint16_t)(0.78570*8192), (uint16_t)(1.08979*8192), (uint16_t)(1.02656*8192), (uint16_t)(0.92388*8192), (uint16_t)(0.78570*8192), (uint16_t)(0.61732*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.21677*8192),
|
||||
(uint16_t)(0.54120*8192), (uint16_t)(0.75066*8192), (uint16_t)(0.70711*8192), (uint16_t)(0.63638*8192), (uint16_t)(0.54120*8192), (uint16_t)(0.42522*8192), (uint16_t)(0.29290*8192), (uint16_t)(0.14932*8192),
|
||||
(uint16_t)(0.27590*8192), (uint16_t)(0.38268*8192), (uint16_t)(0.36048*8192), (uint16_t)(0.32442*8192), (uint16_t)(0.27590*8192), (uint16_t)(0.21678*8192), (uint16_t)(0.14932*8192), (uint16_t)(0.07612*8192)
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*---------------------------------------------*/
|
||||
/* Conversion table for fast clipping process */
|
||||
/*---------------------------------------------*/
|
||||
|
||||
#if JD_TBLCLIP
|
||||
|
||||
//#define BYTECLIP(v) Clip8[(uint16_t)(v) & 0x3FF]
|
||||
|
||||
static const uint8_t Clip8[1024] = {
|
||||
/* 0..255 */
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
|
||||
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
|
||||
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
|
||||
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
|
||||
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
|
||||
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
|
||||
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
|
||||
/* 256..511 */
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* -512..-257 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* -256..-1 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
#else /* JD_TBLCLIP */
|
||||
|
||||
static inline int32_t BYTECLIP (
|
||||
int32_t val
|
||||
)
|
||||
{
|
||||
return (val < 0) ? 0 : (val > 255) ? 255 : val;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------*/
|
||||
/* Output 4x4 bayer pattern table */
|
||||
/*---------------------------------------------*/
|
||||
|
||||
#if JD_BAYER
|
||||
static const int8_t Bayer[16] = { 0, 4, 1, 5,-2, 2,-1, 3, 1, 5, 0, 4,-1, 3,-2, 2};
|
||||
#endif
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Allocate a memory block from memory pool */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static uint8_t* alloc_pool ( /* Pointer to allocated memory block (NULL:no memory available) */
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
uint_fast16_t nd /* Number of bytes to allocate */
|
||||
)
|
||||
{
|
||||
uint8_t *rp = 0;
|
||||
|
||||
|
||||
nd = (nd + 3) & ~3; /* Align block size to the word boundary */
|
||||
|
||||
if (jd->sz_pool >= nd) {
|
||||
jd->sz_pool -= nd;
|
||||
rp = jd->pool; /* Get start of available memory pool */
|
||||
jd->pool = (rp + nd); /* Allocate requierd bytes */
|
||||
}
|
||||
|
||||
return rp; /* Return allocated memory block (NULL:no memory to allocate) */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Create de-quantization and prescaling tables with a DQT segment */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static int32_t create_qt_tbl ( /* 0:OK, !0:Failed */
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
const uint8_t* data, /* Pointer to the quantizer tables */
|
||||
uint_fast16_t ndata /* Size of input data */
|
||||
)
|
||||
{
|
||||
const uint8_t* dataend = data + ndata;
|
||||
do { /* Process all tables in the segment */
|
||||
size_t d = *data++; /* Get table property */
|
||||
if (d & 0xF0) return JDR_FMT1; /* Err: not 8-bit resolution */
|
||||
int32_t *pb = (int32_t*)alloc_pool(jd, 64 * sizeof (int32_t));/* Allocate a memory block for the table */
|
||||
if (!pb) return JDR_MEM1; /* Err: not enough memory */
|
||||
jd->qttbl[d & 3] = pb; /* Register the table */
|
||||
for (size_t i = 0; i < 64; ++i) { /* Load the table */
|
||||
uint_fast8_t z = Zig[i]; /* Zigzag-order to raster-order conversion */
|
||||
pb[z] = (int32_t)((uint32_t)data[i] * Ipsf[z]); /* Apply scale factor of Arai algorithm to the de-quantizers */
|
||||
}
|
||||
} while (dataend != (data += 64));
|
||||
|
||||
return JDR_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Create huffman code tables with a DHT segment */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static int32_t create_huffman_tbl ( /* 0:OK, !0:Failed */
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
const uint8_t* data, /* Pointer to the packed huffman tables */
|
||||
int_fast16_t ndata /* Size of input data */
|
||||
)
|
||||
{
|
||||
uint_fast16_t np;
|
||||
uint8_t *pb, *pd;
|
||||
uint16_t *ph;
|
||||
|
||||
do { /* Process all tables in the segment */
|
||||
uint_fast8_t d = *data++; /* Get table number and class */
|
||||
if (d & 0xEE) return JDR_FMT1; /* Err: invalid class/number */
|
||||
uint_fast8_t cls = d >> 4; /* class = dc(0)/ac(1), table number = 0/1 */
|
||||
uint_fast8_t num = d & 0x0F;
|
||||
pb = alloc_pool(jd, 16); /* Allocate a memory block for the bit distribution table */
|
||||
if (!pb) return JDR_MEM1; /* Err: not enough memory */
|
||||
jd->huffbits[num][cls] = pb - 1;
|
||||
np = 0;
|
||||
size_t i = 0;
|
||||
do { /* Load number of patterns for 1 to 16-bit code */
|
||||
np += (pb[i] = data[i]); /* Get sum of code words for each code */
|
||||
} while (++i < 16);
|
||||
|
||||
ph = (uint16_t*)alloc_pool(jd, np * sizeof (uint16_t));/* Allocate a memory block for the code word table */
|
||||
if (!ph) return JDR_MEM1; /* Err: not enough memory */
|
||||
jd->huffcode[num][cls] = ph - 1;
|
||||
uint_fast16_t hc = 0;
|
||||
i = 0;
|
||||
do { /* Re-build huffman code word table */
|
||||
size_t b = pb[i];
|
||||
while (b--) *ph++ = hc++;
|
||||
hc <<= 1;
|
||||
} while (++i < 16);
|
||||
|
||||
pd = alloc_pool(jd, np); /* Allocate a memory block for the decoded data */
|
||||
if (!pd) return JDR_MEM1; /* Err: not enough memory */
|
||||
jd->huffdata[num][cls] = pd - 1;
|
||||
|
||||
memcpy(pd, data += 16, np); /* Load decoded data corresponds to each code ward */
|
||||
data += np;
|
||||
} while (ndata -= 17 + np);
|
||||
|
||||
return JDR_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Extract N bits from input stream */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static int32_t bitext ( /* >=0: extracted data, <0: error code */
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
uint_fast8_t nbit /* Number of bits to extract (1 to 11) */
|
||||
)
|
||||
{
|
||||
uint_fast8_t msk = jd->dbit;
|
||||
uint8_t *dp = jd->dptr;
|
||||
uint32_t w = *dp;
|
||||
|
||||
if (msk < nbit) {
|
||||
do { /* Next byte? */
|
||||
uint8_t *dpend = jd->dpend;
|
||||
if (++dp == dpend) { /* No input data is available, re-fill input buffer */
|
||||
dp = jd->inbuf; /* Top of input buffer */
|
||||
dpend = dp + jd->infunc(jd->device, dp, JD_SZBUF);
|
||||
if (dp == dpend) return 0 - (int32_t)JDR_INP; /* Err: read error or wrong stream termination */
|
||||
jd->dpend = dpend;
|
||||
}
|
||||
uint_fast8_t s = *dp;
|
||||
w = (w << 8) + s;
|
||||
if (s == 0xff) { /* Is start of flag sequence? */
|
||||
if (++dp == dpend) { /* No input data is available, re-fill input buffer */
|
||||
dp = jd->inbuf; /* Top of input buffer */
|
||||
dpend = dp + jd->infunc(jd->device, dp, JD_SZBUF);
|
||||
if (dp == dpend) return 0 - (int32_t)JDR_INP; /* Err: read error or wrong stream termination */
|
||||
jd->dpend = dpend;
|
||||
}
|
||||
if (*dp != 0) return 0 - (int32_t)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
|
||||
*dp = 0xff; /* The flag is a data 0xFF */
|
||||
}
|
||||
jd->dptr = dp;
|
||||
msk += 8; /* Read from MSB */
|
||||
} while (msk < nbit);
|
||||
}
|
||||
msk -= nbit;
|
||||
jd->dbit = msk;
|
||||
return (w >> msk) & ((1 << nbit) - 1); /* Get bits */
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Extract a huffman decoded data from input stream */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static int32_t huffext ( /* >=0: decoded data, <0: error code */
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
const uint8_t* hb, /* Pointer to the bit distribution table */
|
||||
const uint16_t* hc, /* Pointer to the code word table */
|
||||
const uint8_t* hd /* Pointer to the data table */
|
||||
)
|
||||
{
|
||||
const uint8_t* hb_end = hb + 16 + 1;
|
||||
uint_fast8_t msk = jd->dbit;
|
||||
uint32_t w = *jd->dptr & ((1ul << msk) - 1);
|
||||
for (;;) {
|
||||
if (!msk) { /* Next byte? */
|
||||
uint8_t *dp = jd->dptr;
|
||||
uint8_t *dpend = jd->dpend;
|
||||
msk = 8;
|
||||
if (++dp == dpend) { /* No input data is available, re-fill input buffer */
|
||||
dp = jd->inbuf; /* Top of input buffer */
|
||||
jd->dpend = dpend = dp + jd->infunc(jd->device, dp, JD_SZBUF);
|
||||
if (dp == dpend) return 0 - (int32_t)JDR_INP; /* Err: read error or wrong stream termination */
|
||||
}
|
||||
uint_fast8_t s = *dp;
|
||||
w = (w << 8) + s;
|
||||
if (*dp == 0xff) { /* Is start of flag sequence? */
|
||||
if (++dp == dpend) { /* No input data is available, re-fill input buffer */
|
||||
dp = jd->inbuf; /* Top of input buffer */
|
||||
jd->dpend = dpend = dp + jd->infunc(jd->device, dp, JD_SZBUF);
|
||||
if (dp == dpend) return 0 - (int32_t)JDR_INP; /* Err: read error or wrong stream termination */
|
||||
}
|
||||
if (*dp != 0) return 0 - (int32_t)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
|
||||
*dp = 0xff; /* The flag is a data 0xFF */
|
||||
}
|
||||
jd->dptr = dp;
|
||||
}
|
||||
do {
|
||||
uint_fast16_t v = w >> --msk;
|
||||
uint_fast8_t nc = *++hb;
|
||||
if (hb == hb_end) return 0 - (int32_t)JDR_FMT1; /* Err: code not found (may be collapted data) */
|
||||
if (nc) {
|
||||
const uint8_t* hd_end = hd + nc;
|
||||
do { /* Search the code word in this bit length */
|
||||
if (v == *++hc) goto huffext_match; /* Matched? */
|
||||
} while (++hd != hd_end);
|
||||
}
|
||||
} while (msk);
|
||||
}
|
||||
huffext_match:
|
||||
jd->dbit = msk;
|
||||
return *++hd; /* Return the decoded data */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Apply Inverse-DCT in Arai Algorithm (see also aa_idct.png) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static void block_idct (
|
||||
int32_t* src, /* Input block data (de-quantized and pre-scaled for Arai Algorithm) */
|
||||
int16_t* dst /* Pointer to the destination to store the block as byte array */
|
||||
)
|
||||
{
|
||||
const int32_t M13 = (int32_t)(1.41421*256), M2 = (int32_t)(1.08239*256), M4 = (int32_t)(2.61313*256), M5 = (int32_t)(1.84776*256);
|
||||
int32_t v0, v1, v2, v3, v4, v5, v6, v7;
|
||||
int32_t t10, t11, t12, t13;
|
||||
|
||||
/* Process columns */
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
/* Get and Process the odd elements */
|
||||
v4 = src[8 * 7];
|
||||
v5 = src[8 * 1];
|
||||
v6 = src[8 * 5];
|
||||
v7 = src[8 * 3];
|
||||
|
||||
t10 = v5 - v4;
|
||||
t11 = v5 + v4;
|
||||
t12 = v6 - v7;
|
||||
v7 += v6;
|
||||
v5 = (t11 - v7) * M13 >> 8;
|
||||
t13 = (t10 + t12) * M5 >> 8;
|
||||
v6 = t13 - ((t12 * M4 >> 8) + (v7 += t11));
|
||||
v4 = t13 - ((t10 * M2 >> 8) + (v5 -= v6));
|
||||
|
||||
/* Get and Process the even elements */
|
||||
v0 = src[8 * 0];
|
||||
v2 = src[8 * 4];
|
||||
t10 = v0 + v2;
|
||||
t12 = v0 - v2;
|
||||
|
||||
v1 = src[8 * 2];
|
||||
v3 = src[8 * 6];
|
||||
t11 = (v1 - v3) * M13 >> 8;
|
||||
v3 += v1;
|
||||
t11 -= v3;
|
||||
|
||||
v0 = t10 + v3;
|
||||
v3 = t10 - v3;
|
||||
v1 = t12 + t11;
|
||||
v2 = t12 - t11;
|
||||
|
||||
/* Write-back transformed values */
|
||||
src[8 * 0] = v0 + v7;
|
||||
src[8 * 7] = v0 - v7;
|
||||
src[8 * 1] = v1 + v6;
|
||||
src[8 * 6] = v1 - v6;
|
||||
src[8 * 2] = v2 + v5;
|
||||
src[8 * 5] = v2 - v5;
|
||||
src[8 * 3] = v3 + v4;
|
||||
src[8 * 4] = v3 - v4;
|
||||
|
||||
++src; /* Next column */
|
||||
}
|
||||
|
||||
/* Process rows */
|
||||
src -= 8;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
/* Get and Process the odd elements */
|
||||
v4 = src[1];
|
||||
v5 = src[7] + v4;
|
||||
v4 = (v4 << 1) - v5;
|
||||
|
||||
v6 = src[5];
|
||||
v7 = src[3] + v6;
|
||||
v6 = (v6 << 1) - v7;
|
||||
|
||||
v7 += v5;
|
||||
v5 = (v5 << 1) - v7;
|
||||
|
||||
t13 = v4 + v6;
|
||||
t13 = t13 * M5 >> 8;
|
||||
v6 = v6 * M4 >> 8;
|
||||
v6 += v7;
|
||||
v6 = t13 - v6;
|
||||
v5 = v5 * M13 >> 8;
|
||||
v5 -= v6;
|
||||
v4 = v4 * M2 >> 8;
|
||||
v4 += v5;
|
||||
v4 = t13 - v4;
|
||||
|
||||
/* Get and Process the even elements */
|
||||
v0 = src[0] + (128L << 8); /* remove DC offset (-128) here */
|
||||
v2 = src[4];
|
||||
t10 = v0 + v2;
|
||||
t12 = v0 - v2;
|
||||
|
||||
v1 = src[2];
|
||||
v3 = src[6] + v1;
|
||||
t11 = (v1 << 1) - v3;
|
||||
t11 = t11 * M13 >> 8;
|
||||
t11 -= v3;
|
||||
|
||||
v0 = t10 + v3;
|
||||
v3 = t10 - v3;
|
||||
v1 = t12 + t11;
|
||||
v2 = t12 - t11;
|
||||
|
||||
dst[0] = (v0 + v7) >> 8;
|
||||
dst[7] = (v0 - v7) >> 8;
|
||||
dst[1] = (v1 + v6) >> 8;
|
||||
dst[6] = (v1 - v6) >> 8;
|
||||
dst[2] = (v2 + v5) >> 8;
|
||||
dst[5] = (v2 - v5) >> 8;
|
||||
dst[3] = (v3 + v4) >> 8;
|
||||
dst[4] = (v3 - v4) >> 8;
|
||||
|
||||
dst += 8;
|
||||
src += 8; /* Next row */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Load all blocks in the MCU into working buffer */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static JRESULT mcu_load (
|
||||
lgfxJdec* jd /* Pointer to the decompressor object */
|
||||
)
|
||||
{
|
||||
int32_t *tmp = (int32_t*)jd->workbuf; /* Block working buffer for de-quantize and IDCT */
|
||||
int32_t b, d, e;
|
||||
uint32_t blk, nby, nbc;
|
||||
int16_t *bp;
|
||||
const uint8_t *hb, *hd;
|
||||
const uint16_t *hc;
|
||||
|
||||
|
||||
nby = jd->msx * jd->msy; /* Number of Y blocks (1, 2 or 4) */
|
||||
nbc = jd->comps_in_frame - 1; /* Number of C blocks (2 or 0(grayscale)) */
|
||||
bp = jd->mcubuf; /* Pointer to the first block */
|
||||
|
||||
for (blk = 0; blk < nby + nbc; ++blk) {
|
||||
size_t cmp = (blk < nby) ? 0 : blk - nby + 1; /* Component number 0:Y, 1:Cb, 2:Cr */
|
||||
size_t id = cmp ? 1 : 0; /* Huffman table ID of the component */
|
||||
|
||||
/* Extract a DC element from input stream */
|
||||
hb = jd->huffbits[id][0]; /* Huffman table for the DC element */
|
||||
hc = jd->huffcode[id][0];
|
||||
hd = jd->huffdata[id][0];
|
||||
b = huffext(jd, hb, hc, hd); /* Extract a huffman coded data (bit length) */
|
||||
if (b < 0) return (JRESULT)(-b); /* Err: invalid code or input */
|
||||
d = jd->dcv[cmp]; /* DC value of previous block */
|
||||
if (b) { /* If there is any difference from previous block */
|
||||
e = bitext(jd, b); /* Extract data bits */
|
||||
if (e < 0) return (JRESULT)(-e); /* Err: input */
|
||||
b = 1 << (b - 1); /* MSB position */
|
||||
if (!(e & b)) e -= (b << 1) - 1; /* Restore sign if needed */
|
||||
d += e; /* Get current value */
|
||||
jd->dcv[cmp] = d; /* Save current DC value for next block */
|
||||
}
|
||||
const int32_t *dqf = jd->qttbl[jd->qtid[cmp]]; /* De-quantizer table ID for this component */
|
||||
tmp[0] = d * dqf[0] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
|
||||
|
||||
/* Extract following 63 AC elements from input stream */
|
||||
memset(&tmp[1], 0, 63*sizeof(int32_t)); /* Clear rest of elements */
|
||||
hb = jd->huffbits[id][1]; /* Huffman table for the AC elements */
|
||||
hc = jd->huffcode[id][1];
|
||||
hd = jd->huffdata[id][1];
|
||||
uint_fast8_t i = 1; /* Top of the AC elements */
|
||||
do {
|
||||
b = huffext(jd, hb, hc, hd); /* Extract a huffman coded value (zero runs and bit length) */
|
||||
if (b == 0) break; /* EOB? */
|
||||
if (b < 0) return (JRESULT)(-b); /* Err: invalid code or input error */
|
||||
i += b >> 4; /* Number of leading zero elements Skip zero elements */
|
||||
if (b &= 0x0F) { /* Bit length */
|
||||
d = bitext(jd, b); /* Extract data bits */
|
||||
if (d < 0) return (JRESULT)(-d);/* Err: input device */
|
||||
b = 1 << (b - 1); /* MSB position */
|
||||
if (!(d & b)) d -= (b << 1) - 1;/* Restore negative value if needed */
|
||||
uint_fast8_t z = Zig[i]; /* Zigzag-order to raster-order converted index */
|
||||
tmp[z] = d * dqf[z] >> 8; /* De-quantize, apply scale factor of Arai algorithm and descale 8 bits */
|
||||
}
|
||||
} while (++i < 64); /* Next AC element */
|
||||
|
||||
if (i == 1 || (JD_USE_SCALE && jd->scale == 3)) {
|
||||
d = (int16_t)((*tmp >> 8) + 128); /* If scale ratio is 1/8, IDCT can be ommited and only DC element is used */
|
||||
for (i = 0; i < 64; bp[i++] = d) ;
|
||||
} else {
|
||||
block_idct(tmp, bp); /* Apply IDCT and store the block to the MCU buffer */
|
||||
}
|
||||
|
||||
bp += 64; /* Next block */
|
||||
}
|
||||
|
||||
return JDR_OK; /* All blocks have been loaded successfully */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Output an MCU: Convert YCrCb to RGB and output it in RGB form */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static JRESULT mcu_output (
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
uint32_t (*outfunc)(void*, void*, JRECT*), /* RGB output function */
|
||||
uint32_t x, /* MCU position in the image (left of the MCU) */
|
||||
uint32_t y /* MCU position in the image (top of the MCU) */
|
||||
)
|
||||
{
|
||||
const int_fast16_t FP_SHIFT = 8;
|
||||
uint32_t ix, iy, mx, my, rx, ry;
|
||||
int32_t yy, cb, cr;
|
||||
int16_t *py, *pc;
|
||||
uint8_t *rgb24;
|
||||
JRECT rect;
|
||||
|
||||
mx = jd->msx << 3; my = jd->msy << 3; /* MCU size (pixel) */
|
||||
rx = (mx < jd->width - x) ? mx : jd->width - x; /* Output rectangular size (it may be clipped at right/bottom end) */
|
||||
ry = (my < jd->height - y) ? my : jd->height - y;
|
||||
|
||||
if (JD_USE_SCALE) {
|
||||
rx >>= jd->scale; ry >>= jd->scale;
|
||||
if (!rx || !ry) return JDR_OK; /* Skip this MCU if all pixel is to be rounded off */
|
||||
x >>= jd->scale; y >>= jd->scale;
|
||||
}
|
||||
rect.left = x; rect.right = x + rx - 1; /* Rectangular area in the frame buffer */
|
||||
rect.top = y; rect.bottom = y + ry - 1;
|
||||
|
||||
uint8_t* workbuf = (uint8_t*)jd->workbuf;
|
||||
|
||||
if (!JD_USE_SCALE || jd->scale != 3) { /* Not for 1/8 scaling */
|
||||
|
||||
uint_fast8_t ixshift = (mx == 16);
|
||||
uint_fast8_t iyshift = (my == 16);
|
||||
|
||||
/* Build an RGB MCU from discrete comopnents */
|
||||
rgb24 = workbuf;
|
||||
iy = 0;
|
||||
do {
|
||||
#if JD_BAYER
|
||||
const int8_t* btbl = &Bayer[(iy & 3) << 2];
|
||||
#endif
|
||||
py = &jd->mcubuf[((iy & 8) + iy) << 3];
|
||||
pc = &jd->mcubuf[((mx << iyshift) + (iy >> iyshift)) << 3];
|
||||
ix = 0;
|
||||
do {
|
||||
do {
|
||||
cb = (pc[ 0] - 128); /* Get Cb/Cr component and restore right level */
|
||||
cr = (pc[64] - 128);
|
||||
++pc;
|
||||
|
||||
/* Convert CbCr to RGB */
|
||||
int32_t rr = ((int32_t)(1.402 * (1<<FP_SHIFT)) * cr) >> FP_SHIFT;
|
||||
int32_t gg = ((int32_t)(0.34414 * (1<<FP_SHIFT)) * cb
|
||||
+ (int32_t)(0.71414 * (1<<FP_SHIFT)) * cr) >> FP_SHIFT;
|
||||
int32_t bb = ((int32_t)(1.772 * (1<<FP_SHIFT)) * cb) >> FP_SHIFT;
|
||||
do {
|
||||
#if JD_BAYER
|
||||
yy = *py + btbl[ix & 3]; /* Get Y component */
|
||||
#else
|
||||
yy = *py; /* Get Y component */
|
||||
#endif
|
||||
++py;
|
||||
/* Convert YCbCr to RGB */
|
||||
rgb24[0] = BYTECLIP(yy + rr);
|
||||
rgb24[1] = BYTECLIP(yy - gg);
|
||||
rgb24[2] = BYTECLIP(yy + bb);
|
||||
rgb24 += 3;
|
||||
} while (++ix & ixshift);
|
||||
} while (ix & 7);
|
||||
py += 64 - 8; /* Jump to next block if double block heigt */
|
||||
} while (ix != mx);
|
||||
} while (++iy < my);
|
||||
|
||||
/* Descale the MCU rectangular if needed */
|
||||
if (JD_USE_SCALE && jd->scale) {
|
||||
uint32_t x_, y_, r_, g_, b_, s_, w_;
|
||||
uint8_t *op;
|
||||
|
||||
/* Get averaged RGB value of each square correcponds to a pixel */
|
||||
s_ = jd->scale * 2; /* Bumber of shifts for averaging */
|
||||
w_ = 1 << jd->scale; /* Width of square */
|
||||
op = workbuf;
|
||||
iy = 0;
|
||||
do {
|
||||
ix = 0;
|
||||
do {
|
||||
rgb24 = &workbuf[(iy * mx + ix) * 3];
|
||||
r_ = g_ = b_ = 0;
|
||||
y_ = 0;
|
||||
do { /* Accumulate RGB value in the square */
|
||||
x_ = 0;
|
||||
do {
|
||||
r_ += rgb24[x_*3 ];
|
||||
g_ += rgb24[x_*3+1];
|
||||
b_ += rgb24[x_*3+2];
|
||||
} while (++x_ < w_);
|
||||
rgb24 += mx * 3;
|
||||
} while (++y_ < w_);
|
||||
/* Put the averaged RGB value as a pixel */
|
||||
op[0] = r_ >> s_;
|
||||
op[1] = g_ >> s_;
|
||||
op[2] = b_ >> s_;
|
||||
op += 3;
|
||||
} while ((ix += w_) < mx);
|
||||
} while ((iy += w_) < my);
|
||||
}
|
||||
|
||||
} else { /* For only 1/8 scaling (left-top pixel in each block are the DC value of the block) */
|
||||
|
||||
/* Build a 1/8 descaled RGB MCU from discrete comopnents */
|
||||
rgb24 = workbuf;
|
||||
pc = jd->mcubuf + mx * my;
|
||||
cb = pc[0] - 128; /* Get Cb/Cr component and restore right level */
|
||||
cr = pc[64] - 128;
|
||||
iy = 0;
|
||||
do {
|
||||
py = jd->mcubuf;
|
||||
if (iy == 8) py += 64 * 2;
|
||||
ix = 0;
|
||||
do {
|
||||
yy = *py; /* Get Y component */
|
||||
py += 64;
|
||||
|
||||
/* Convert YCbCr to RGB */
|
||||
rgb24[0] = BYTECLIP(yy + (((int32_t)(1.402 * (1<<FP_SHIFT)) * cr) >> FP_SHIFT));
|
||||
rgb24[1] = BYTECLIP(yy - (((int32_t)(0.34414 * (1<<FP_SHIFT)) * cb
|
||||
+ (int32_t)(0.71414 * (1<<FP_SHIFT)) * cr) >> FP_SHIFT));
|
||||
rgb24[2] = BYTECLIP(yy + (((int32_t)(1.772 * (1<<FP_SHIFT)) * cb) >> FP_SHIFT));
|
||||
rgb24 += 3;
|
||||
} while ((ix += 8) < mx);
|
||||
} while ((iy += 8) < my);
|
||||
}
|
||||
|
||||
/* Squeeze up pixel table if a part of MCU is to be truncated */
|
||||
mx >>= jd->scale;
|
||||
if (rx < mx) {
|
||||
uint8_t *s_, *d;
|
||||
s_ = d = workbuf;
|
||||
for (size_t y_ = 1; y_ < ry; ++y_) {
|
||||
memcpy(d += rx * 3, s_ += mx * 3, rx * 3); /* Copy effective pixels */
|
||||
}
|
||||
}
|
||||
|
||||
/* Convert RGB888 to RGB565 if needed */
|
||||
if (JD_FORMAT == 1) {
|
||||
uint8_t *s = workbuf;
|
||||
uint16_t *d = (uint16_t*)s;
|
||||
uint_fast16_t w;
|
||||
uint_fast16_t n = rx * ry;
|
||||
|
||||
do {
|
||||
w = (*s++ & 0xF8) << 8; /* RRRRR----------- */
|
||||
w |= (*s++ & 0xFC) << 3; /* -----GGGGGG----- */
|
||||
w |= *s++ >> 3; /* -----------BBBBB */
|
||||
*d++ = w;
|
||||
} while (--n);
|
||||
}
|
||||
|
||||
/* Output the RGB rectangular */
|
||||
return outfunc(jd->device, workbuf, &rect) ? JDR_OK : JDR_INTR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Process restart interval */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static JRESULT restart (
|
||||
lgfxJdec* jd, /* Pointer to the decompressor object */
|
||||
uint16_t rstn /* Expected restert sequense number */
|
||||
)
|
||||
{
|
||||
uint16_t d;
|
||||
uint8_t *dp = jd->dptr, *dpend = jd->dpend;
|
||||
|
||||
/* Discard padding bits and get two bytes from the input stream */
|
||||
d = 0;
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (++dp == dpend) { /* No input data is available, re-fill input buffer */
|
||||
dp = jd->inbuf;
|
||||
jd->dpend = dpend = dp + jd->infunc(jd->device, dp, JD_SZBUF);
|
||||
if (dp == dpend) return JDR_INP;
|
||||
}
|
||||
d = (d << 8) | *dp; /* Get a byte */
|
||||
}
|
||||
jd->dptr = dp; jd->dbit = 0;
|
||||
|
||||
/* Check the marker */
|
||||
if ((d & 0xFFD8) != 0xFFD0 || (d & 7) != (rstn & 7)) {
|
||||
return JDR_FMT1; /* Err: expected RSTn marker is not detected (may be collapted data) */
|
||||
}
|
||||
|
||||
/* Reset DC offset */
|
||||
jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0;
|
||||
|
||||
return JDR_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Analyze the JPEG image and Initialize decompressor object */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
//#define LDB_WORD(ptr) (uint16_t)(((uint16_t)*((uint8_t*)(ptr))<<8)|(uint16_t)*(uint8_t*)((ptr)+1))
|
||||
|
||||
static inline uint16_t LDB_WORD(uint8_t* ptr) {
|
||||
return ptr[0]<<8 | ptr[1];
|
||||
}
|
||||
|
||||
|
||||
JRESULT lgfx_jd_prepare (
|
||||
lgfxJdec* jd, /* Blank decompressor object */
|
||||
uint32_t (*infunc)(void*, uint8_t*, uint32_t), /* JPEG strem input function */
|
||||
void* pool, /* Working buffer for the decompression session */
|
||||
uint_fast16_t sz_pool, /* Size of working buffer */
|
||||
void* dev /* I/O device identifier for the session */
|
||||
)
|
||||
{
|
||||
uint8_t *seg;
|
||||
uint32_t ofs;
|
||||
size_t n;
|
||||
int32_t rc;
|
||||
|
||||
|
||||
if (!pool) return JDR_PAR;
|
||||
|
||||
jd->pool = (uint8_t*)pool; /* Work memroy */
|
||||
jd->sz_pool = sz_pool; /* Size of given work memory */
|
||||
jd->infunc = infunc; /* Stream input function */
|
||||
jd->device = dev; /* I/O device identifier */
|
||||
jd->nrst = 0; /* No restart interval (default) */
|
||||
|
||||
// memset(jd->huffbits, 0, sizeof(uint8_t*) * 4); /* Nulls pointers */
|
||||
// memset(jd->huffcode, 0, sizeof(uint16_t*) * 4);
|
||||
// memset(jd->huffdata, 0, sizeof(uint8_t*) * 4);
|
||||
// memset(jd->qttbl, 0, sizeof(uint32_t*) * 4);
|
||||
|
||||
jd->inbuf = seg = alloc_pool(jd, JD_SZBUF); /* Allocate stream input buffer */
|
||||
if (!seg) return JDR_MEM1;
|
||||
|
||||
if (infunc(dev, seg, 2) != 2) return JDR_INP;/* Check SOI marker */
|
||||
if (LDB_WORD(seg) != 0xFFD8) return JDR_FMT1; /* Err: SOI is not detected */
|
||||
ofs = 2;
|
||||
|
||||
for (;;) {
|
||||
if (infunc(dev, seg, 1) != 1) return JDR_INP;
|
||||
if (seg[0] != 0xFF) return JDR_FMT1; /* Check a JPEG marker */
|
||||
do
|
||||
{
|
||||
if (infunc(dev, &seg[1], 1) != 1) return JDR_INP;
|
||||
} while (seg[1] == 0xFF);
|
||||
if (infunc(dev, &seg[2], 2) != 2) return JDR_INP;
|
||||
uint_fast16_t len = LDB_WORD(seg + 2) - 2; /* Length field */
|
||||
ofs += 4 + len; /* Number of bytes loaded */
|
||||
|
||||
switch (seg[1]) { /* Marker */
|
||||
case 0xC0: /* SOF0 (baseline JPEG) */
|
||||
{/* Load segment data */
|
||||
if (len > JD_SZBUF) return JDR_MEM2;
|
||||
if (infunc(dev, seg, len) != len) return JDR_INP;
|
||||
|
||||
jd->width = LDB_WORD(seg+3); /* Image width in unit of pixel */
|
||||
jd->height = LDB_WORD(seg+1); /* Image height in unit of pixel */
|
||||
jd->comps_in_frame = seg[5];
|
||||
if (seg[5] != 1 && seg[5] != 3)
|
||||
return JDR_FMT3; /* Err: Supports only Y/Cb/Cr or Y(Grayscale) format */
|
||||
|
||||
/* Check three image components */
|
||||
for (size_t i = 0; i < seg[5]; ++i) {
|
||||
uint_fast8_t b = seg[7 + 3 * i]; /* Get sampling factor */
|
||||
if (!i) { /* Y component */
|
||||
if (b != 0x11 && b != 0x22 && b != 0x21) { /* Check sampling factor */
|
||||
return JDR_FMT3; /* Err: Supports only 4:4:4, 4:2:0 or 4:2:2 */
|
||||
}
|
||||
jd->msx = b >> 4; jd->msy = b & 15; /* Size of MCU [blocks] */
|
||||
} else { /* Cb/Cr component */
|
||||
if (b != 0x11) return JDR_FMT3; /* Err: Sampling factor of Cr/Cb must be 1 */
|
||||
}
|
||||
b = seg[8 + 3 * i]; /* Get dequantizer table ID for this component */
|
||||
if (b > 3) return JDR_FMT3; /* Err: Invalid ID */
|
||||
jd->qtid[i] = b;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xDD: /* DRI */
|
||||
{/* Load segment data */
|
||||
if (len > JD_SZBUF) return JDR_MEM2;
|
||||
if (infunc(dev, seg, len) != len) return JDR_INP;
|
||||
|
||||
/* Get restart interval (MCUs) */
|
||||
jd->nrst = LDB_WORD(seg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xC4: /* DHT */
|
||||
{/* Load segment data */
|
||||
if (len > JD_SZBUF) return JDR_MEM2;
|
||||
if (infunc(dev, seg, len) != len) return JDR_INP;
|
||||
|
||||
/* Create huffman tables */
|
||||
rc = create_huffman_tbl(jd, seg, len);
|
||||
if (rc) return (JRESULT)rc;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xDB: /* DQT */
|
||||
{/* Load segment data */
|
||||
if (len > JD_SZBUF) return JDR_MEM2;
|
||||
if (infunc(dev, seg, len) != len) return JDR_INP;
|
||||
|
||||
/* Create de-quantizer tables */
|
||||
rc = create_qt_tbl(jd, seg, len);
|
||||
if (rc) return (JRESULT)rc;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0xDA: /* SOS */
|
||||
{/* Load segment data */
|
||||
if (len > JD_SZBUF) return JDR_MEM2;
|
||||
if (infunc(dev, seg, len) != len) return JDR_INP;
|
||||
|
||||
if (!jd->width || !jd->height) return JDR_FMT1; /* Err: Invalid image size */
|
||||
|
||||
if (seg[0] != jd->comps_in_frame) return JDR_FMT3; /* Err: Supports only three color or grayscale components format */
|
||||
|
||||
/* Check if all tables corresponding to each components have been loaded */
|
||||
for (size_t i = 0; i < jd->comps_in_frame; ++i) {
|
||||
uint_fast8_t b = seg[2 + 2 * i]; /* Get huffman table ID */
|
||||
if (b != 0x00 && b != 0x11) return JDR_FMT3; /* Err: Different table number for DC/AC element */
|
||||
b = i ? 1 : 0;
|
||||
if (!jd->huffbits[b][0] || !jd->huffbits[b][1]) { /* Check dc/ac huffman table for this component */
|
||||
return JDR_FMT1; /* Err: Nnot loaded */
|
||||
}
|
||||
if (!jd->qttbl[jd->qtid[i]]) { /* Check dequantizer table for this component */
|
||||
return JDR_FMT1; /* Err: Not loaded */
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate working buffer for MCU and RGB */
|
||||
n = jd->msy * jd->msx; /* Number of Y blocks in the MCU */
|
||||
if (!n) return JDR_FMT1; /* Err: SOF0 has not been loaded */
|
||||
len = n * 64 * 2 + 64; /* Allocate buffer for IDCT and RGB output */
|
||||
if (len < 256) len = 256; /* but at least 256 byte is required for IDCT */
|
||||
jd->workbuf = alloc_pool(jd, len); /* and it may occupy a part of following MCU working buffer for RGB output */
|
||||
if (!jd->workbuf) return JDR_MEM1; /* Err: not enough memory */
|
||||
size_t mcubuf_len = (n + 2) * 64;
|
||||
jd->mcubuf = (int16_t*)alloc_pool(jd, mcubuf_len * sizeof(int16_t)); /* Allocate MCU working buffer */
|
||||
if (!jd->mcubuf) return JDR_MEM1; /* Err: not enough memory */
|
||||
if (jd->comps_in_frame == 1) {
|
||||
for (size_t i = n * 16; i < mcubuf_len; ++i) {
|
||||
jd->mcubuf[i] = 128; /* Cb/Cr clear ( for grayscale )*/
|
||||
}
|
||||
}
|
||||
|
||||
/* Pre-load the JPEG data to extract it from the bit stream */
|
||||
ofs %= JD_SZBUF; /* Align read offset to JD_SZBUF */
|
||||
int32_t dc = infunc(dev, seg + ofs, JD_SZBUF - ofs);
|
||||
jd->dptr = seg + ofs - 1;
|
||||
jd->dpend = seg + ofs + dc;
|
||||
jd->dbit = 0; /* Prepare to read bit stream */
|
||||
|
||||
}
|
||||
return JDR_OK; /* Initialization succeeded. Ready to decompress the JPEG image. */
|
||||
|
||||
case 0xC1: /* SOF1 */
|
||||
case 0xC2: /* SOF2 */
|
||||
case 0xC3: /* SOF3 */
|
||||
case 0xC5: /* SOF5 */
|
||||
case 0xC6: /* SOF6 */
|
||||
case 0xC7: /* SOF7 */
|
||||
case 0xC9: /* SOF9 */
|
||||
case 0xCA: /* SOF10 */
|
||||
case 0xCB: /* SOF11 */
|
||||
case 0xCD: /* SOF13 */
|
||||
case 0xCE: /* SOF14 */
|
||||
case 0xCF: /* SOF15 */
|
||||
case 0xD9: /* EOI */
|
||||
return JDR_FMT3; /* Unsuppoted JPEG standard (may be progressive JPEG) */
|
||||
|
||||
default: /* Unknown segment (comment, exif or etc..) */
|
||||
/* Skip segment data */
|
||||
if (infunc(dev, 0, len) != len) { /* Null pointer specifies to skip bytes of stream */
|
||||
return JDR_INP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Start to decompress the JPEG picture */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
JRESULT lgfx_jd_decomp (
|
||||
lgfxJdec* jd, /* Initialized decompression object */
|
||||
uint32_t (*outfunc)(void*, void*, JRECT*), /* RGB output function */
|
||||
uint_fast8_t scale /* Output de-scaling factor (0 to 3) */
|
||||
)
|
||||
{
|
||||
uint32_t x, y, mx, my;
|
||||
uint32_t nrst, rst, rsc;
|
||||
JRESULT rc;
|
||||
|
||||
|
||||
if (scale > (JD_USE_SCALE ? 3 : 0)) return JDR_PAR;
|
||||
jd->scale = scale;
|
||||
|
||||
nrst = jd->nrst;
|
||||
mx = jd->msx << 3; my = jd->msy << 3; /* Size of the MCU (pixel) */
|
||||
|
||||
jd->dcv[2] = jd->dcv[1] = jd->dcv[0] = 0; /* Initialize DC values */
|
||||
rst = rsc = 0;
|
||||
|
||||
rc = JDR_OK;
|
||||
|
||||
for (y = 0; y < jd->height; y += my) { /* Vertical loop of MCUs */
|
||||
x = 0;
|
||||
do { /* Horizontal loop of MCUs */
|
||||
if (nrst && rst++ == nrst) { /* Process restart interval if enabled */
|
||||
rc = restart(jd, rsc++);
|
||||
if (rc != JDR_OK) return rc;
|
||||
rst = 1;
|
||||
}
|
||||
rc = mcu_load(jd); /* Load an MCU (decompress huffman coded stream and apply IDCT) */
|
||||
if (rc != JDR_OK) return rc;
|
||||
rc = mcu_output(jd, outfunc, x, y); /* Output the MCU (color space conversion, scaling and output) */
|
||||
if (rc != JDR_OK) return rc;
|
||||
} while ( (x += mx) < jd->width);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ TJpgDec - Tiny JPEG Decompressor include file (C)ChaN, 2019
|
||||
/-----------------------------------------------------------------------------/
|
||||
/ original source is here : http://elm-chan.org/fsw/tjpgd/00index.html
|
||||
/
|
||||
/ Modified for LGFX by lovyan03, 2020
|
||||
/ add support grayscale jpeg
|
||||
/ add bayer pattern
|
||||
/ tweak for 32bit processor
|
||||
/----------------------------------------------------------------------------*/
|
||||
#ifndef __LGFX_TJPGDEC_H__
|
||||
#define __LGFX_TJPGDEC_H__
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* System Configurations */
|
||||
|
||||
#define JD_SZBUF 512 /* Size of stream input buffer */
|
||||
#define JD_FORMAT 0 /* Output pixel format 0:RGB888 (3 BYTE/pix), 1:RGB565 (1 WORD/pix) */
|
||||
#define JD_USE_SCALE 1 /* Use descaling feature for output */
|
||||
#define JD_TBLCLIP 0 /* Use table for saturation (might be a bit faster but increases 1K bytes of code size) */
|
||||
#define JD_BAYER 1 /* Use bayer pattern table */
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if __has_include (<stdint.h>)
|
||||
#include <stdint.h>
|
||||
#elif defined(_WIN32) /* Main development platform */
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef short int16_t;
|
||||
typedef unsigned long uint32_t;
|
||||
typedef long int32_t;
|
||||
#endif
|
||||
|
||||
/* Error code */
|
||||
typedef enum {
|
||||
JDR_OK = 0, /* 0: Succeeded */
|
||||
JDR_INTR, /* 1: Interrupted by output function */
|
||||
JDR_INP, /* 2: Device error or wrong termination of input stream */
|
||||
JDR_MEM1, /* 3: Insufficient memory pool for the image */
|
||||
JDR_MEM2, /* 4: Insufficient stream input buffer */
|
||||
JDR_PAR, /* 5: Parameter error */
|
||||
JDR_FMT1, /* 6: Data format error (may be damaged data) */
|
||||
JDR_FMT2, /* 7: Right format but not supported */
|
||||
JDR_FMT3 /* 8: Not supported JPEG standard */
|
||||
} JRESULT;
|
||||
|
||||
|
||||
|
||||
/* Rectangular structure */
|
||||
typedef struct {
|
||||
uint32_t left, right, top, bottom;
|
||||
} JRECT;
|
||||
|
||||
|
||||
|
||||
/* Decompressor object structure */
|
||||
typedef struct lgfxJdec lgfxJdec;
|
||||
struct lgfxJdec {
|
||||
uint8_t* dptr; /* Current data read ptr */
|
||||
uint8_t* dpend; /* data end ptr */
|
||||
uint8_t* inbuf; /* Bit stream input buffer */
|
||||
uint8_t dbit; /* Current bit in the current read byte */
|
||||
uint8_t scale; /* Output scaling ratio */
|
||||
uint8_t msx, msy; /* MCU size in unit of block (width, height) */
|
||||
uint8_t qtid[3]; /* Quantization table ID of each component */
|
||||
int32_t dcv[3]; /* Previous DC element of each component */
|
||||
uint16_t nrst; /* Restart inverval */
|
||||
uint16_t width, height;/* Size of the input image (pixel) */
|
||||
uint8_t* huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */
|
||||
uint16_t* huffcode[2][2]; /* Huffman code word tables [id][dcac] */
|
||||
uint8_t* huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */
|
||||
int32_t* qttbl[4]; /* Dequantizer tables [id] */
|
||||
void* workbuf; /* Working buffer for IDCT and RGB output */
|
||||
int16_t* mcubuf; /* Working buffer for the MCU */
|
||||
uint8_t* pool; /* Pointer to available memory pool */
|
||||
uint16_t sz_pool; /* Size of momory pool (bytes available) */
|
||||
uint32_t (*infunc)(void*, uint8_t*, uint32_t);/* Pointer to jpeg stream input function */
|
||||
void* device; /* Pointer to I/O device identifiler for the session */
|
||||
uint8_t comps_in_frame; /* 1=Y(grayscale) 3=YCrCb */
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* TJpgDec API functions */
|
||||
JRESULT lgfx_jd_prepare (lgfxJdec*, uint32_t(*)(void*,uint8_t*,uint32_t), void*, uint_fast16_t, void*);
|
||||
JRESULT lgfx_jd_decomp (lgfxJdec*, uint32_t(*)(void*,void*,JRECT*), uint_fast8_t);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TJPGDEC */
|
||||
@@ -0,0 +1,91 @@
|
||||
/*----------------------------------------------------------------------------/
|
||||
Lovyan GFX - Graphics library for embedded devices.
|
||||
|
||||
Original Source:
|
||||
https://github.com/lovyan03/LovyanGFX/
|
||||
|
||||
Licence:
|
||||
[BSD](https://github.com/lovyan03/LovyanGFX/blob/master/license.txt)
|
||||
|
||||
Author:
|
||||
[lovyan03](https://twitter.com/lovyan03)
|
||||
|
||||
Contributors:
|
||||
[ciniml](https://github.com/ciniml)
|
||||
[mongonta0716](https://github.com/mongonta0716)
|
||||
[tobozo](https://github.com/tobozo)
|
||||
/----------------------------------------------------------------------------*/
|
||||
#ifndef __LGFX_PGMSPACE_H__
|
||||
#define __LGFX_PGMSPACE_H__
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined ( ARDUINO ) && !defined ( pgm_read_byte )
|
||||
#if __has_include(<pgmspace.h>)
|
||||
#include <pgmspace.h>
|
||||
#elif __has_include(<avr/pgmspace.h>) || defined(__AVR__)
|
||||
#include <avr/pgmspace.h>
|
||||
#else
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
#endif
|
||||
#if !defined ( pgm_read_byte )
|
||||
#define pgm_read_byte(addr) (*(const uint8_t *)((uintptr_t)addr))
|
||||
#define pgm_read_word(addr) (*(const uint16_t *)((uintptr_t)addr))
|
||||
#define pgm_read_dword(addr) (*(const uint32_t *)((uintptr_t)addr))
|
||||
#define pgm_read_ptr(addr) (*(void * const *)(addr))
|
||||
#elif defined (ARDUINO_ARCH_SPRESENSE)
|
||||
#undef pgm_read_ptr
|
||||
#define pgm_read_ptr(addr) (*(void * const *)(addr))
|
||||
#endif
|
||||
|
||||
/// for not ESP8266
|
||||
#if !defined ( pgm_read_dword_with_offset )
|
||||
#if defined (__SAMD21__) || defined(__SAMD21G18A__) || defined(__SAMD21J18A__) || defined(__SAMD21E17A__) || defined(__SAMD21E18A__) || defined(ARDUINO_ARCH_MBED_RP2040) || defined(ARDUINO_ARCH_RP2040)
|
||||
#define pgm_read_word_unaligned(addr) (uint16_t) \
|
||||
( *(const uint8_t *)((uintptr_t)addr) \
|
||||
| *(const uint8_t *)((uintptr_t)addr+1) << 8 )
|
||||
#define pgm_read_3byte_unaligned(addr) (uint32_t) \
|
||||
( *(const uint8_t *)((uintptr_t)addr) \
|
||||
| *(const uint8_t *)((uintptr_t)addr+1) << 8 \
|
||||
| *(const uint8_t *)((uintptr_t)addr+2) << 16 )
|
||||
#define pgm_read_dword_unaligned(addr) (uint32_t) \
|
||||
( *(const uint8_t *)((uintptr_t)addr) \
|
||||
| *(const uint8_t *)((uintptr_t)addr+1) << 8 \
|
||||
| *(const uint8_t *)((uintptr_t)addr+2) << 16 \
|
||||
| *(const uint8_t *)((uintptr_t)addr+3) << 24 )
|
||||
#else
|
||||
#define pgm_read_word_unaligned(addr) (*(const uint16_t *)((uintptr_t)addr))
|
||||
#define pgm_read_dword_unaligned(addr) (*(const uint32_t *)((uintptr_t)addr))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined ( pgm_read_3byte_unaligned )
|
||||
#define pgm_read_3byte_unaligned(addr) (pgm_read_dword_unaligned(addr) & 0xFFFFFFu)
|
||||
#endif
|
||||
|
||||
#if defined ( ESP8266 ) || defined (__SAMD21__) || defined(__SAMD21G18A__) || defined(__SAMD21J18A__) || defined(__SAMD21E17A__) || defined(__SAMD21E18A__) || defined(ARDUINO_ARCH_MBED_RP2040) || defined(ARDUINO_ARCH_RP2040)
|
||||
static inline void write_3byte_unaligned(void* addr, uint32_t value)
|
||||
{
|
||||
((uint8_t*)(addr))[0] = value;
|
||||
((uint8_t*)(addr))[1] = value >> 8;
|
||||
((uint8_t*)(addr))[2] = value >> 16;
|
||||
}
|
||||
#else
|
||||
static inline void write_3byte_unaligned(void* addr, uint32_t value)
|
||||
{
|
||||
((uint16_t*)(addr))[0] = value;
|
||||
((uint8_t* )(addr))[2] = value >> 16;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ARDUINO
|
||||
static inline void* memcpy_P(void* __restrict dst, const void* __restrict src, size_t len) { return memcpy(dst, src, len); }
|
||||
static inline int memcmp_P(const void* __restrict dst, const void* __restrict src, size_t len) { return memcmp(dst, src, len); }
|
||||
#endif
|
||||
|
||||
#ifndef PROGMEM
|
||||
#define PROGMEM
|
||||
#endif
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user