/* **************************************************************************** * mcsfile/decode/mcs/picture.c -- decode an MCS picture. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libcasio. * libcasio 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. * * libcasio 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 libcasio; if not, see . * ************************************************************************* */ #include "../decode.h" /** * casio_decode_mcs_capture: * Decode a capture. * * @arg h 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 casio_decode_mcs_capture(casio_mcsfile_t **h, casio_stream_t *buffer, casio_mcshead_t *head) { int err; casio_mcs_captureheader_t hd; uint8_t *pic_raw = NULL; size_t pic_size; casio_mcsfile_t *handle; /* read header */ DREAD(hd) /* make final head and file */ head->casio_mcshead_width = be16toh(hd.casio_mcs_captureheader_width); head->casio_mcshead_height = be16toh(hd.casio_mcs_captureheader_height); err = casio_make_mcsfile(h, head); if (err) return (err); handle = *h; /* print info */ msg((ll_info, "capture is %dx%d sized", handle->casio_mcsfile_head.casio_mcshead_width, handle->casio_mcsfile_head.casio_mcshead_height)); /* read raw data */ pic_size = casio_get_picture_size(NULL, casio_pictureformat_1bit_packed, head->casio_mcshead_width, head->casio_mcshead_height); err = casio_error_alloc; if (!(pic_raw = malloc(pic_size))) goto fail; READ(pic_raw, pic_size) /* get the image and return */ if ((err = casio_decode_picture(handle->casio_mcsfile_pics[0], pic_raw, casio_pictureformat_1bit_packed, head->casio_mcshead_width, head->casio_mcshead_height))) goto fail; /* no error! */ free(pic_raw); return (0); fail: free(pic_raw); casio_free_mcsfile(*h); *h = NULL; return (err); } /** * casio_decode_mcs_picture: * Decode a picture. * * @arg h 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 casio_decode_mcs_picture(casio_mcsfile_t **h, casio_stream_t *buffer, casio_mcshead_t *head) { int err; casio_mcsfile_t *handle; unsigned char *pics_raw = NULL; size_t pic_size; /* make final head */ head->casio_mcshead_width = 128; head->casio_mcshead_height = 64; err = casio_make_mcsfile(h, head); if (err) return (err); handle = *h; /* get the images */ pic_size = casio_get_picture_size(NULL, casio_pictureformat_1bit_packed, head->casio_mcshead_width, head->casio_mcshead_height); err = casio_error_alloc; if (!(pics_raw = malloc(pic_size * 2))) goto fail; READ(pics_raw, pic_size * 2) /* decode the images */ if ((err = casio_decode_picture(handle->casio_mcsfile_pics[0], pics_raw, casio_pictureformat_1bit_packed, head->casio_mcshead_width, head->casio_mcshead_height)) || (err = casio_decode_picture(handle->casio_mcsfile_pics[1], &pics_raw[pic_size], casio_pictureformat_1bit_packed, head->casio_mcshead_width, head->casio_mcshead_height))) goto fail; /* no error */ free(pics_raw); return (0); fail: casio_free_mcsfile(*h); *h = NULL; free(pics_raw); return (0); }