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/decode/mcs/matrix.c

107 lines
3.1 KiB
C

/* *****************************************************************************
* decode/mcs/matrix.c -- decode an MCS matrix.
* 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_decode_mcs_matrix:
* Decode a matrix.
*
* @arg handle the handle to make.
* @arg buffer the buffer to read from.
* @arg head the pre-filled head to complete and use.
* @return the error code (0 if ok).
*/
int g1m_decode_mcs_matrix(g1m_mcsfile_t **handle, g1m_buffer_t *buffer,
g1m_mcshead_t *head)
{
int err = g1m_error_alloc;
/* read header */
DREAD(hd, mcs_matheader)
hd.width = be16toh(hd.width);
hd.height = be16toh(hd.height);
/* log info */
uint_fast32_t w = hd.width, h = hd.height;
log_info("Matrix size is %" PRIuFAST32 "*%" PRIuFAST32, w, h);
/* make final head */
head->width = w;
head->height = h;
if ((err = g1m_make_mcsfile(handle, head)))
return (err);
/* main copying loop */
g1m_mcs_cell_t **tab = (*handle)->cells; int one_imgn = 0;
for (uint_fast32_t y = 0; y < h; y++)
for (uint_fast32_t x = 0; x < w; x++) {
/* read the cell */
g1m_bcd_t bcd; GDREAD(rawbcd, bcd)
one_imgn |= g1m_bcd_frommcs(&rawbcd, &bcd);
/* store it */
tab[y][x] = (g1m_mcs_cell_t){
.real = bcd,
.imgn = {},
.used = 1
};
}
/* check imaginary parts */
if (one_imgn) for (uint_fast32_t y = 0; y < h; y++)
for (uint_fast32_t x = 0; x < w; x++) {
g1m_bcd_t bcd; GDREAD(rawbcd, bcd)
if (g1m_bcd_has_special(&tab[y][x].real)) {
g1m_bcd_frommcs(&rawbcd, &bcd);
tab[y][x].imgn = bcd;
}
}
#if LOGLEVEL <= ll_info
/* logging loop */
char rbuf[G1M_BCD_GOODBUFSIZE], ibuf[G1M_BCD_GOODBUFSIZE];
for (uint_fast32_t y = 0; y < h; y++)
for (uint_fast32_t x = 0; x < w; x++) {
g1m_bcdtoa(&tab[y][x].real, rbuf, G1M_BCD_GOODBUFSIZE);
if (g1m_bcd_has_special(&tab[y][x].real)) {
g1m_bcd_t ibcd = tab[y][x].imgn;
int ch = g1m_bcd_is_negative(&ibcd) ? '-' : '+';
ibcd.flags &= ~g1m_bcdflag_neg;
g1m_bcdtoa(&ibcd, ibuf, G1M_BCD_GOODBUFSIZE);
log_info("[%" PRIuFAST32 "][%" PRIuFAST32 "] %s %c %si",
y, x, rbuf, ch, ibuf);
} else
log_info("[%" PRIuFAST32 "][%" PRIuFAST32 "] %s",
y, x, rbuf);
}
#endif
/* no error */
return (0);
/* in case of unexpected EOF */
fail:
g1m_free_mcsfile(*handle);
*handle = NULL;
return (err);
}