/* ***************************************************************************** * image/packed1bit.c -- encode and decode from packed monochrome encoded image. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************** */ #include /** * g1m_pixels_from_packed1bit: * Convert packed monochrome formatted pixels 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_packed1bit(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 byte = 0x80; for (int i = 0; i < cell_count; i++) { /* get pixel and iterate */ int pixel = *raw & byte; raw += byte & 1; byte = (byte >> 1) | ((byte & 1) << 7); /* put pixel */ *cell++ = (pixel != 0) * 0xffffff; } } /** * g1m_pixels_to_packed1bit: * Convert 32-bit matrix to packed monochrome pixels. * * @arg dest the destination buffer. * @arg pixels the decoded pixels. * @arg width the width. * @arg height the height. */ void g1m_pixels_to_packed1bit(unsigned char *dest, uint32_t **pixels, int width, int height) { uint32_t *cell = (uint32_t*)&pixels[height]; int cell_count = width * height; /* fill */ int byte = 0x80; for (int i = 0; i < cell_count; i++) { /* get pixel */ int pixel = ((*cell++ & 0xffffff) != 0) * byte; *dest |= pixel; /* iterate */ dest += byte & 1; byte = (byte >> 1) | ((byte & 1) << 7); } }