143 lines
4.0 KiB
C
143 lines
4.0 KiB
C
/* *****************************************************************************
|
|
* decode/mcs/picture.c -- decode an MCS picture.
|
|
* 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>
|
|
|
|
/* ************************************************************************** */
|
|
/* Internal functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* get_image:
|
|
* Allocate space, and convert monochromic image to 0x0RGB.
|
|
*
|
|
* @arg buffer the buffer to read from.
|
|
* @arg img the image to make.
|
|
* @arg width the image width.
|
|
* @arg height the image height.
|
|
*/
|
|
|
|
static int get_image(g1m_buffer_t *buffer, uint32_t ***img,
|
|
unsigned int width, unsigned int height)
|
|
{
|
|
/* get raw pixels */
|
|
size_t linesize = width / 8 + !!(width % 8);
|
|
size_t bufsize = height * linesize;
|
|
uint8_t buf[bufsize];
|
|
READ(buf, bufsize)
|
|
|
|
/* alloc */
|
|
*img = malloc(height * (sizeof(uint32_t*))
|
|
+ width * height * sizeof(uint32_t));
|
|
if (!*img)
|
|
return (g1m_error_alloc);
|
|
|
|
/* fill */
|
|
uint32_t **image = *img;
|
|
for (uint_fast16_t y = 0; y < height; y++) {
|
|
/* setup index */
|
|
image[y] = (uint32_t*)&image[height] + y * width;
|
|
|
|
/* init vars for monochrome with fill bits image browsing */
|
|
uint8_t *b = &buf[y * linesize];
|
|
int bit = 1 << 7;
|
|
|
|
/* browse and save pixels! (monochrome to 0x0RGB) */
|
|
for (uint_fast16_t x = 0; x < width; x++) {
|
|
/* set pixel */
|
|
image[y][x] = *b & bit ? 0xffffff : 0x000000;
|
|
|
|
/* go further */
|
|
b += bit & 1;
|
|
bit = (bit >> 1) | ((bit & 1) << 7);
|
|
}
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Real decode functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_mcs_capture:
|
|
* Decode a capture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcs_capture(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_captureheader)
|
|
|
|
/* correct endianess */
|
|
hd.width = be16toh(hd.width);
|
|
hd.height = be16toh(hd.height);
|
|
|
|
/* store */
|
|
handle->head.width = hd.width;
|
|
handle->head.height = hd.height;
|
|
|
|
/* print info */
|
|
log_info("capture is %dx%d sized", handle->head.width, handle->head.height);
|
|
|
|
/* get the image and return */
|
|
return (get_image(buffer, &handle->image,
|
|
handle->head.width, handle->head.height));
|
|
}
|
|
|
|
/**
|
|
* g1m_decode_mcs_picture:
|
|
* Decode a picture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcs_picture(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
int err;
|
|
|
|
/* store some dimensions, in case. */
|
|
handle->head.width = 128;
|
|
handle->head.height = 64;
|
|
|
|
/* get the first image */
|
|
if ((err = get_image(buffer, &handle->image, 128, 64))) {
|
|
log_fatal("Failed to get the first image.");
|
|
return (err);
|
|
}
|
|
|
|
/* and the second */
|
|
if ((err = get_image(buffer, &handle->second_image, 128, 64))) {
|
|
log_fatal("Failed to get the second image.");
|
|
return (err);
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|