cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/image/16bits.c

82 lines
2.6 KiB
C

/* *****************************************************************************
* image/16bits.c -- encode and decode from 16-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>
/**
* g1m_pixels_from_16bits:
* Convert from 16-bit format to 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_16bits(uint32_t **pixels, unsigned char *raw,
int width, int height)
{
uint32_t *cell = (uint32_t*)&pixels[height];
int cell_count = width * height;
uint16_t *st = (uint16_t*)raw;
const unsigned int rmask = 0x1F << 11, gmask = 0x3F << 5, bmask = 0x1F;
for (int i = 0; i < cell_count; i++) {
/* get colors
* as colors don't occupy 8-bits each, we'll "split into equal
* halves out of 256";
* e.g. red can go from 0b00000 to 0b11111, then in 8-bits it
* will go from 0b00000000 to 0b11111000. */
unsigned int r = (*st & rmask) >> 8;
unsigned int g = (*st & gmask) >> 3;
unsigned int b = (*st++ & bmask) << 3;
/* put pixel */
*cell++ = (r << 16) | (g << 8) | b;
}
}
/**
* g1m_pixels_to_16bits:
* Convert from 32-bit matrix to 16-bit pixels (R5G6B5).
*
* @arg dest the destination buffer.
* @arg pixels the decoded pixels.
* @arg width the width.
* @arg height the height.
*/
void g1m_pixels_to_16bits(unsigned char *dest, uint32_t **pixels,
int width, int height)
{
uint32_t *cell = (uint32_t*)&pixels[height];
int cell_count = width * height;
uint16_t *d = (uint16_t*)dest;
const uint32_t rmask = 0x1F << 19, gmask = 0x3F << 10, bmask = 0x1F << 3;
for (int i = 0; i < cell_count; i++) {
/* get colors */
unsigned int r = (*cell & rmask) >> 19;
unsigned int g = (*cell & gmask) >> 10;
unsigned int b = (*cell++ & bmask) >> 3;
/* put pixel */
*d++ += (r << 11) | (g << 5) | b;
}
}