From e6e8cd423f0c18e4556d29ac937f5f18510d9485 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Mon, 12 Jun 2017 01:54:57 +0200 Subject: [PATCH] Corrected picture utilities and header, added editorconfig --- .editorconfig | 10 +++ include/libcasio/cdefs.h | 10 +-- include/libcasio/picture.h | 94 +++++++++++------------------ include/libcasio/protocol/seven.h | 4 +- src/file/decode/casemul.c | 3 +- src/file/decode/std/picture.c | 4 +- src/mcsfile/decode/cas/screenshot.c | 3 +- src/mcsfile/decode/mcs/picture.c | 8 +-- src/picture/encode.c | 8 +-- src/picture/size.c | 66 ++++++++++++++++++++ 10 files changed, 131 insertions(+), 79 deletions(-) create mode 100644 .editorconfig create mode 100644 src/picture/size.c diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..20bc71a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +# Editor Configuration for libcasio - see http://editorconfig.org +root = true + +# Unix-style newlines, ending newline; +# 4 column tabulations. +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab +indent_size = 4 diff --git a/include/libcasio/cdefs.h b/include/libcasio/cdefs.h index fbc564d..147644b 100644 --- a/include/libcasio/cdefs.h +++ b/include/libcasio/cdefs.h @@ -84,15 +84,15 @@ # endif # ifdef __cplusplus +# define CASIO_BEGIN_NAMESPACE # define CASIO_BEGIN_DECLS extern "C" { -# define CASIO_BEGIN_NAMESPACE -# define CASIO_END_NAMESPACE # define CASIO_END_DECLS } -# else -# define CASIO_BEGIN_DECLS -# define CASIO_BEGIN_NAMESPACE # define CASIO_END_NAMESPACE +# else +# define CASIO_BEGIN_NAMESPACE +# define CASIO_BEGIN_DECLS # define CASIO_END_DECLS +# define CASIO_END_NAMESPACE # endif /* ************************************************************************* */ /* `casio_attr_wur`: warn if the result is unused. */ diff --git a/include/libcasio/picture.h b/include/libcasio/picture.h index af319dc..a8c280e 100644 --- a/include/libcasio/picture.h +++ b/include/libcasio/picture.h @@ -19,24 +19,26 @@ #ifndef LIBCASIO_PICTURE_H # define LIBCASIO_PICTURE_H # include -# include /* XXX: TEMP FIX */ CASIO_BEGIN_NAMESPACE /* This file describes the picture formats CASIO has used for various purposes * (screenshots, pictures, screenstreaming), and that libcasio is able to * decode. * - * The formats pictures should fit in a `casio_pictureformat_t`. - * They are more or less organized as 0xBBVS, where BB is the number of bits - * each pixel occupy (in total), V is the variation for this number of bits, - * and S is the special hex digit (e.g. reverse). */ - -typedef unsigned int casio_pictureformat_t; - -/* And here is a cross-compiler version of the `pixel_t` type, which is - * basically four bytes per pixel: 0x00RRGGBB. */ + * libcasio decodes from, encodes to, and converts using 32-bit pixels, which + * are basically 0x00RRGGBB. */ typedef casio_uint32_t casio_pixel_t; + +/* The format pictures this file describes fit in a `casio_pictureformat_t`. + * The (arbitrary) codes for them is more or less 0xBBVS, where 'BB' is the + * number of bits each pixel occupy (in total), 'V' is the variation for + * this number of bits, and 'S' is the special hex digit (e.g. reverse). + * + * Do not use the format described above other than to define your own + * formats, as it might change! */ + +typedef enum casio_pictureformat_s { /* ************************************************************************* */ /* Monochrome pictures with fill bits */ /* ************************************************************************* */ @@ -49,16 +51,11 @@ typedef casio_uint32_t casio_pixel_t; * An off bit (0b0) represents a white pixel, and an on bit (0b1) represents * a black pixel. Reverse white and black in the `_r` special type. */ -# define casio_pictureformat_1bit 0x0100 -# define casio_pictureformat_1bit_r 0x0101 + casio_pictureformat_1bit = 0x0100, + casio_pictureformat_1bit_r = 0x0101, /* To calculate the size, it's simple: just calculate the number of bytes * a line occupies, then multiply it by the number of lines. */ - -# define casio_picturesize_1bit(CASIO__W, CASIO__H) \ - ((((CASIO__W) / 8) + !!((CASIO__W) % 8)) * (CASIO__H)) -# define casio_picturesize_1bit_r(CASIO__W, CASIO__H) \ - casio_picturesize_1bit((CASIO__W), (CASIO__H)) /* ************************************************************************* */ /* Packed monochrome pictures */ /* ************************************************************************* */ @@ -69,15 +66,12 @@ typedef casio_uint32_t casio_pixel_t; * * The navigation to a line is less easy as it takes at least one division. */ -# define casio_pictureformat_1bit_packed 0x0110 -# define casio_pictureformat_1bit_packed_r 0x0111 + casio_pictureformat_1bit_packed = 0x0110, + casio_pictureformat_1bit_packed_r = 0x0111, /* To calculate the size, find out the number of occupied bits, divide by * eight to get the bytes, and make sure to keep an extra byte if there are * left bits. */ - -# define casio_picturesize_1bit_packed(CASIO__W, CASIO__H) \ - (((CASIO__W) * (CASIO__H) / 8) + !!((CASIO__W) * (CASIO__H) % 8)) /* ************************************************************************* */ /* Old monochrome format */ /* ************************************************************************* */ @@ -86,13 +80,10 @@ typedef casio_uint32_t casio_pixel_t; * that it starts with the last byte (where the bits are in left to right * order), but then it goes from right to left, and from bottom to top. */ -# define casio_pictureformat_1bit_old 0x0120 + casio_pictureformat_1bit_old = 0x0120, /* The size is the same as for normal 1-bit monochrome pictures, only the * byte order changes. */ - -# define casio_picturesize_1bit_old(CASIO__W, CASIO__H) \ - casio_picturesize_1bit(CASIO__W, CASIO__H) /* ************************************************************************* */ /* Dual monochrome format */ /* ************************************************************************* */ @@ -101,31 +92,23 @@ typedef casio_uint32_t casio_pixel_t; * It is basically gray pictures, with white, light gray, dark gray and * black. */ -# define casio_pictureformat_2bit_dual 0x0200 + casio_pictureformat_2bit_dual = 0x0200, /* To calculate the size, well, we just have two monochrome screens. */ - -# define casio_picturesize_2bit_dual(CASIO__W, CASIO__H) \ - (casio_picturesize_1bit(CASIO__W, CASIO__H) * 2) /* ************************************************************************* */ /* 4-bit RGB_ format */ /* ************************************************************************* */ -/* This is a 4 bit per pixel format. There is no need for fill bits (?). +/* This is a 4 bit per pixel format. There is no need for fill nibbles. * Each nibble (group of 4 bits) is made of the following: * - one bit for red (OR by 0xFF0000); * - one bit for green (OR by 0x00FF00); * - one bit for blue (OR by 0x0000FF); * - one alignment bit. */ -# define casio_pictureformat_4bit 0x0400 -# define casio_pictureformat_4bit_rgb 0x0400 + casio_pictureformat_4bit = 0x0400, + casio_pictureformat_4bit_rgb = 0x0400, -/* Calculating the size is trivial. */ - -# define casio_picturesize_4bit(CASIO__W, CASIO__H) \ - ((CASIO__W) * (CASIO__H) / 2) -# define casio_picturesize_4bit_rgb(CASIO__W, CASIO__H) \ - casio_picturesize_4bit(CASIO__W, CASIO__H) +/* Calculating the size is trivial: just divide the number of pixels by two. */ /* ************************************************************************* */ /* 4-bit code format */ /* ************************************************************************* */ @@ -142,12 +125,9 @@ typedef casio_uint32_t casio_pixel_t; /* Here is the encoding code: */ -# define casio_pictureformat_4bit_code 0x0410 + casio_pictureformat_4bit_code = 0x0410, /* The size is calculated the same way as previously. */ - -# define casio_picturesize_4bit_code(CASIO__W, CASIO__H) \ - casio_picturesize_4bit(CASIO__W, CASIO__H) /* ************************************************************************* */ /* Quad-monochrome VRAM formats */ /* ************************************************************************* */ @@ -156,15 +136,10 @@ typedef casio_uint32_t casio_pixel_t; * - for the `color` variant: [orange, green, blue, white (bg)] * - for the `mono` variant: [(unused), (unused), black, white (bg)] */ -# define casio_pictureformat_4bit_color 0x0420 -# define casio_pictureformat_4bit_mono 0x0421 + casio_pictureformat_4bit_color = 0x0420, + casio_pictureformat_4bit_mono = 0x0421, -/* Just multiply the size of a VRAM by four. */ - -# define casio_picturesize_4bit_color(CASIO__W, CASIO__H) \ - (4 * casio_picturesize_1bit_old(CASIO__W, CASIO__H)) -# define casio_picturesize_4bit_mono(CASIO__W, CASIO__H) \ - casio_picturesize_4bit_color(CASIO__W, CASIO__H) +/* To get the size, just multiply the size of a VRAM by four. */ /* ************************************************************************* */ /* Casemul format */ /* ************************************************************************* */ @@ -172,12 +147,9 @@ typedef casio_uint32_t casio_pixel_t; * It is basically arbitrary color codes, where, for example, 1 is orange. * You can check the full color codes in `src/picture.c`. */ -# define casio_pictureformat_casemul 0x0800 + casio_pictureformat_casemul = 0x0800, /* Each pixel takes one byte. */ - -# define casio_picturesize_casemul(CASIO__W, CASIO__H) \ - ((CASIO__W) * (CASIO__H)) /* ************************************************************************* */ /* 16-bit R5G6B5 format */ /* ************************************************************************* */ @@ -187,18 +159,21 @@ typedef casio_uint32_t casio_pixel_t; * the last five bits represent the high five (re-clap!) bits of the blue * part. */ -# define casio_pictureformat_16bit 0x1000 + casio_pictureformat_16bit = 0x1000 +} casio_pictureformat_t; /* Two bytes per pixel. */ - -# define casio_picturesize_16bit(CASIO__W, CASIO__H) \ - ((CASIO__W) * (CASIO__H) * 2) /* ************************************************************************* */ /* Utilities */ /* ************************************************************************* */ /* Here are the functions to decode/encode pictures using this format: */ CASIO_BEGIN_DECLS + +extern size_t casio_get_picture_size OF((casio_pixel_t **casio__pixels, + casio_pictureformat_t casio__format, + unsigned int casio__width, unsigned int casio__height)); + extern int casio_decode_picture OF((casio_pixel_t **casio__pixels, const void *casio__raw, casio_pictureformat_t casio__format, unsigned int casio__width, unsigned casio__height)); @@ -206,6 +181,7 @@ extern int casio_decode_picture OF((casio_pixel_t **casio__pixels, extern int casio_encode_picture OF((void *casio__raw, const casio_pixel_t **casio__pixels, casio_pictureformat_t casio__format, unsigned int casio__width, unsigned casio__height)); + CASIO_END_DECLS CASIO_END_NAMESPACE #endif /* LIBCASIO_PICTURE_H */ diff --git a/include/libcasio/protocol/seven.h b/include/libcasio/protocol/seven.h index df1e84d..8c267f8 100644 --- a/include/libcasio/protocol/seven.h +++ b/include/libcasio/protocol/seven.h @@ -148,8 +148,8 @@ typedef enum casio_seven_ow_e { # define CASIO_SEVEN_MAX_VRAM_WIDTH 384 /* max. encountered VRAM width */ # define CASIO_SEVEN_MAX_VRAM_HEIGHT 500 /* max. encountered VRAM height */ -# define CASIO_SEVEN_MAX_VRAM_SIZE casio_picturesize_16bit( \ - CASIO_SEVEN_MAX_VRAM_WIDTH, CASIO_SEVEN_MAX_VRAM_HEIGHT) +# define CASIO_SEVEN_MAX_VRAM_SIZE \ + (CASIO_SEVEN_MAX_VRAM_WIDTH * CASIO_SEVEN_MAX_VRAM_HEIGHT * 2) # define CASIO_SEVEN_MAX_RAWDATA_SIZE 256 /* maximum raw data size */ # define CASIO_SEVEN_MAX_ENCDATA_SIZE ((CASIO_SEVEN_MAX_RAWDATA_SIZE) * 2) diff --git a/src/file/decode/casemul.c b/src/file/decode/casemul.c index 1ce3654..0ba7851 100644 --- a/src/file/decode/casemul.c +++ b/src/file/decode/casemul.c @@ -184,7 +184,8 @@ static int read_picture(casio_mcsfile_t **pfile, casio_stream_t *buffer, msg((ll_info, "picture dimension is %d*%dpx", width, height)); /* read all of the pixels */ - rawpxsize = casio_picturesize_casemul(width, height); + rawpxsize = casio_get_picture_size(NULL, + casio_pictureformat_casemul, width, height); rawpx = malloc(rawpxsize); if (!rawpx) return (casio_error_alloc); GREAD(rawpx, rawpxsize) diff --git a/src/file/decode/std/picture.c b/src/file/decode/std/picture.c index 76ab0d5..ff1803d 100644 --- a/src/file/decode/std/picture.c +++ b/src/file/decode/std/picture.c @@ -101,9 +101,7 @@ int casio_decode_std_g3p(casio_file_t **h, casio_stream_t *buffer, } /* make the destination buffer */ - rawsize = picfmt == casio_pictureformat_4bit_code - ? casio_picturesize_4bit_code(width, height) - : casio_picturesize_16bit(width, height); + rawsize = casio_get_picture_size(NULL, picfmt, width, height); err = casio_error_alloc; if (!(infbuf = malloc(rawsize))) goto fail; diff --git a/src/mcsfile/decode/cas/screenshot.c b/src/mcsfile/decode/cas/screenshot.c index 87144da..d58f45a 100644 --- a/src/mcsfile/decode/cas/screenshot.c +++ b/src/mcsfile/decode/cas/screenshot.c @@ -37,7 +37,8 @@ int casio_decode_caspart_capture(casio_mcsfile_t *handle, /* read the picture data */ width = handle->casio_mcsfile_head.casio_mcshead_width; height = handle->casio_mcsfile_head.casio_mcshead_height; - pic_size = casio_picturesize_4bit_color(width, height); + pic_size = casio_get_picture_size(NULL, + casio_pictureformat_4bit_color, width, height); /* read the picture size */ err = casio_error_alloc; diff --git a/src/mcsfile/decode/mcs/picture.c b/src/mcsfile/decode/mcs/picture.c index a43a797..ca28b67 100644 --- a/src/mcsfile/decode/mcs/picture.c +++ b/src/mcsfile/decode/mcs/picture.c @@ -52,8 +52,8 @@ int casio_decode_mcs_capture(casio_mcsfile_t **h, casio_stream_t *buffer, handle->casio_mcsfile_head.casio_mcshead_height)); /* read raw data */ - pic_size = casio_picturesize_1bit_packed(head->casio_mcshead_width, - head->casio_mcshead_height); + 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) @@ -99,8 +99,8 @@ int casio_decode_mcs_picture(casio_mcsfile_t **h, casio_stream_t *buffer, handle = *h; /* get the images */ - pic_size = casio_picturesize_1bit_packed(head->casio_mcshead_width, - head->casio_mcshead_height); + 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) diff --git a/src/picture/encode.c b/src/picture/encode.c index ff5e2d1..aecde34 100644 --- a/src/picture/encode.c +++ b/src/picture/encode.c @@ -39,9 +39,8 @@ int casio_encode_picture(void *vraw, switch (format) { case casio_pictureformat_1bit: - memset(raw, 0, casio_picturesize_1bit(width, height)); for (y = 0; y < height; y++) { - msk = 0x80; + msk = 0x80; *raw = 0; for (x = 0; x < width; x++) { /* get pixel */ if (!pixels[y][x]) *raw |= msk; @@ -49,15 +48,15 @@ int casio_encode_picture(void *vraw, /* go to next */ raw += msk & 1; msk = (msk >> 1) | ((msk & 1) << 7); + *raw &= ~(msk - 1); } if (width & 0x7) raw++; } break; case casio_pictureformat_1bit_r: - memset(raw, 0, casio_picturesize_1bit(width, height)); for (y = 0; y < height; y++) { - msk = 0x80; + msk = 0x80; *raw = 0; for (x = 0; x < width; x++) { /* get pixel */ if (pixels[y][x]) *raw |= msk; @@ -65,6 +64,7 @@ int casio_encode_picture(void *vraw, /* go to next */ raw += msk & 1; msk = (msk >> 1) | ((msk & 1) << 7); + *raw &= ~(msk - 1); } if (width & 0x7) raw++; } diff --git a/src/picture/size.c b/src/picture/size.c new file mode 100644 index 0000000..b08797d --- /dev/null +++ b/src/picture/size.c @@ -0,0 +1,66 @@ +/* **************************************************************************** + * picture/size.c -- get picture size. + * 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 "picture.h" + +/** + * casio_get_picture_size: + * Get a 'raw' picture size. + * + * @arg pixels the picture. + * @arg fmt the picture format. + * @arg width the picture width. + * @arg height the picture height. + * @return the raw size. + */ + +size_t casio_get_picture_size(casio_pixel_t **pixels, + casio_pictureformat_t fmt, unsigned int width, unsigned int height) +{ + (void)pixels; + + switch (fmt) { + case casio_pictureformat_1bit: /* FALLTHRU */ + case casio_pictureformat_1bit_r: /* FALLTHRU */ + case casio_pictureformat_1bit_old: + return (((width / 8) + !!(width % 8)) * height); + + case casio_pictureformat_1bit_packed: /* FALLTHRU */ + case casio_pictureformat_1bit_packed_r: + return ((width * height / 8) + !!(width * height % 8)); + + case casio_pictureformat_2bit_dual: + return (((width / 8) + !!(width % 8)) * height * 2); + + case casio_pictureformat_4bit: /* FALLTHRU */ + case casio_pictureformat_4bit_code: + return (width * height / 2); + + case casio_pictureformat_4bit_color: /* FALLTHRU */ + case casio_pictureformat_4bit_mono: + return (((width / 8) + !!(width % 8)) * height * 4); + + case casio_pictureformat_casemul: + return (width * height); + + case casio_pictureformat_16bit: + return (width * height * 2); + } + + return (0); +}