98 lines
2.8 KiB
C
98 lines
2.8 KiB
C
/* *****************************************************************************
|
|
* image/packed4bits.c -- encode and decode from packed 4-bits encoded image.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/* colors correspondance */
|
|
static const uint32_t colors[16] = {
|
|
[g1m_color_black] = 0x000000,
|
|
[g1m_color_blue] = 0x0000ff,
|
|
[g1m_color_green] = 0x00ff00,
|
|
[g1m_color_cyan] = 0x00ffff,
|
|
[g1m_color_red] = 0xff0000,
|
|
[g1m_color_magenta] = 0xff00ff,
|
|
[g1m_color_yellow] = 0xffff00,
|
|
[g1m_color_white] = 0xffffff,
|
|
/* RESERVED */
|
|
};
|
|
|
|
/**
|
|
* g1m_pixels_from_packed4bits:
|
|
* Convert 4-bits formatted image to the final 32-bit matrix.
|
|
*
|
|
* @arg pixels the final pixels.
|
|
* @arg raw the raw data.
|
|
* @arg width the width.
|
|
* @arg height the height.
|
|
*/
|
|
|
|
void g1m_pixels_from_packed4bits(uint32_t **pixels, unsigned char *raw,
|
|
int width, int height)
|
|
{
|
|
uint32_t *cell = (uint32_t*)&pixels[height];
|
|
int cell_count = width * height;
|
|
|
|
/* fill cells */
|
|
int mask = 0xf0;
|
|
for (int i = 0; i < cell_count; i++) {
|
|
/* get nibble and iterate */
|
|
int b = (*raw & mask) >> (mask & 4);
|
|
raw += mask & 1;
|
|
mask = ~mask & 0xff;
|
|
|
|
/* put pixel */
|
|
*cell++ = colors[b];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* g1m_pixels_to_packed4bits:
|
|
* Convert the 32-bit matrix to a 4-bit formatted image.
|
|
*
|
|
* @arg dest the destination buffer.
|
|
* @arg pixels the decoded pixels.
|
|
* @arg width the width.
|
|
* @arg height the height.
|
|
*/
|
|
|
|
void g1m_pixels_to_packed4bits(unsigned char *dest, uint32_t **pixels,
|
|
int width, int height)
|
|
{
|
|
(void)width;
|
|
uint32_t *cell = (uint32_t*)&pixels[height];
|
|
int cell_count = width * height;
|
|
|
|
/* first of all, clean destination */
|
|
bzero(dest, cell_count / 2 + cell_count % 2);
|
|
|
|
/* fill cells */
|
|
int mask = 0xf0;
|
|
for (int i = 0; i < cell_count; i++) {
|
|
/* get color and iterate */
|
|
int color = 0;
|
|
color |= ((0xff0000 & *cell) != 0) << 2;
|
|
color |= ((0x00ff00 & *cell) != 0) << 1;
|
|
color |= ((0x0000ff & *cell++) != 0);
|
|
|
|
/* store nibble */
|
|
*dest |= color << mask & 4;
|
|
dest += mask & 1;
|
|
mask = ~mask & 0xff;
|
|
}
|
|
}
|