/* ***************************************************************************** * encode/mcs/cells.c -- encode an MCS cells tab. * 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_announce_mcs_cells: * Announce a matrix's size. * * @arg handle the handle to make the size out of. * @arg sz the size to contribute to. * @return the error code (0 if ok). */ int g1m_announce_mcs_cells(const g1m_mcsfile_t *handle, size_t *sz) { unsigned int y, x; size_t content_size = handle->g1m_mcsfile_head.g1m_mcshead_height * handle->g1m_mcsfile_head.g1m_mcshead_width * sizeof(g1m_mcsbcd_t); *sz += sizeof(struct mcs_cellsheader) + content_size; /* check if there's an imaginary part */ for (y = 0; y < handle->g1m_mcsfile_head.g1m_mcshead_height; y++) for (x = 0; x < handle->g1m_mcsfile_head.g1m_mcshead_width; x++) { if (g1m_bcd_has_special(&handle->g1m_mcsfile_cells[y][x] .g1m_mcscell_real)) { *sz += content_size; return (0); } } /* no error! */ return (0); } /** * g1m_encode_mcs_cells: * Encode a matrix. * * @arg handle the handle to encode. * @arg buffer the buffer to write to. * @return the error code (0 if ok). */ int g1m_encode_mcs_cells(const g1m_mcsfile_t *handle, g1m_buffer_t *buffer) { unsigned int y, x; struct mcs_cellsheader hd = { .height = htobe16(handle->g1m_mcsfile_head.g1m_mcshead_height), .width = (handle->g1m_mcsfile_head.g1m_mcshead_type & g1m_mcstype_list) ? 1 : htobe16(handle->g1m_mcsfile_head.g1m_mcshead_width), ._undocumented = {0}}; /* write the header */ DWRITE(hd) /* write the real parts */ int one_imgn = 0; for (y = 0; y < handle->g1m_mcsfile_head.g1m_mcshead_height; y++) for (x = 0; x < handle->g1m_mcsfile_head.g1m_mcshead_width; x++) { g1m_mcsbcd_t rawbcd; g1m_bcd_tomcs(&handle->g1m_mcsfile_cells[y][x] .g1m_mcscell_real, &rawbcd); DWRITE(rawbcd) one_imgn |= g1m_bcd_has_special(&handle->g1m_mcsfile_cells[y][x] .g1m_mcscell_real); } /* write the imaginary parts */ if (one_imgn) for (y = 0; y < handle->g1m_mcsfile_head.g1m_mcshead_height; y++) for (x = 0; x < handle->g1m_mcsfile_head.g1m_mcshead_width; x++) { g1m_mcsbcd_t rawbcd; g1m_bcd_tomcs(&handle->g1m_mcsfile_cells[y][x] .g1m_mcscell_imgn, &rawbcd); DWRITE(rawbcd) } /* no error */ return (0); }