Fix PCX issue with odd bitmap width; tests; comment GRIDMATRIX byte count

This commit is contained in:
gitlost 2020-04-06 21:26:13 +01:00
parent 67fac5f600
commit 129fa81c41
14 changed files with 238 additions and 109 deletions

View File

@ -230,6 +230,8 @@ static void define_mode(char* mode, const unsigned int gbdata[], const size_t le
/* Add the length indicator for byte encoded blocks */
static void add_byte_count(char binary[], const size_t byte_count_posn, const int byte_count) {
/* AIMD014 6.3.7: "Let L be the number of bytes of input data to be encoded in the 8-bit binary data set.
* First output (L-1) as a 9-bit binary prefix to record the number of bytes..." */
bin_append_posn(byte_count - 1, 9, binary, byte_count_posn);
}

View File

@ -1,8 +1,9 @@
/* pcx.c - Handles output to ZSoft PCX file */
/* ZSoft PCX File Format Technical Reference Manual http://bespin.org/~qz/pc-gpe/pcx.txt */
/*
libzint - the open source barcode library
Copyright (C) 2009-2017 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2009 - 2020 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -32,7 +33,6 @@
/* vim: set ts=4 sw=4 et : */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "pcx.h" /* PCX header structure */
@ -43,24 +43,26 @@
#include <malloc.h>
#endif
#define SSET "0123456789ABCDEF"
INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, char *pixelbuf) {
int fgred, fggrn, fgblu, bgred, bggrn, bgblu;
int row, column, i, colour;
int run_count;
FILE *pcx_file;
pcx_header_t header;
int bytes_per_line = symbol->bitmap_width + (symbol->bitmap_width & 1); // Must be even
unsigned char previous;
#ifdef _MSC_VER
unsigned char* rle_row;
#endif
#ifndef _MSC_VER
unsigned char rle_row[symbol->bitmap_width];
unsigned char rle_row[bytes_per_line];
#else
rle_row = (unsigned char *) _alloca((symbol->bitmap_width * 6) * sizeof (unsigned char));
rle_row = (unsigned char *) _alloca(bytes_per_line);
#endif /* _MSC_VER */
rle_row[bytes_per_line - 1] = 0; // Will remain zero if bitmap_width odd
fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]);
fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]);
fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]);
@ -87,11 +89,7 @@ INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, char *pixelbuf) {
header.reserved = 0;
header.number_of_planes = 3;
if (symbol->bitmap_width % 2) {
header.bytes_per_line = symbol->bitmap_width + 1;
} else {
header.bytes_per_line = symbol->bitmap_width;
}
header.bytes_per_line = bytes_per_line;
header.palette_info = 1; // Colour
header.horiz_screen_size = 0;
@ -192,23 +190,29 @@ INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, char *pixelbuf) {
}
}
/* Based on ImageMagick/coders/pcx.c PCXWritePixels()
* Copyright 1999-2020 ImageMagick Studio LLC */
previous = rle_row[0];
run_count = 1;
for (column = 1; column < symbol->bitmap_width; column++) {
if ((rle_row[column - 1] == rle_row[column]) && (run_count < 63)) {
for (column = 1; column < bytes_per_line; column++) { // Note going up to bytes_per_line
if ((previous == rle_row[column]) && (run_count < 63)) {
run_count++;
} else {
if (run_count > 1 || (previous & 0xc0) == 0xc0) {
run_count += 0xc0;
fputc(run_count, pcx_file);
fputc(rle_row[column - 1], pcx_file);
}
fputc(previous, pcx_file);
previous = rle_row[column];
run_count = 1;
}
}
if (run_count > 1) {
if (run_count > 1 || (previous & 0xc0) == 0xc0) {
run_count += 0xc0;
fputc(run_count, pcx_file);
fputc(rle_row[column - 1], pcx_file);
}
fputc(previous, pcx_file);
}
}

View File

@ -1,4 +1,4 @@
# Copyright (C) 2019 Robin Stuart <rstuart114@gmail.com>
# Copyright (C) 2019 - 2020 Robin Stuart <rstuart114@gmail.com>
# Adapted from qrencode/tests/CMakeLists.txt
# Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
@ -70,6 +70,7 @@ zint_add_test(imail, test_imail)
zint_add_test(library, test_library)
zint_add_test(mailmark, test_mailmark)
zint_add_test(maxicode, test_maxicode)
zint_add_test(pcx, test_pcx)
zint_add_test(pdf417, test_pdf417)
zint_add_test(png, test_png)
zint_add_test(postal, test_postal)

View File

@ -188,14 +188,14 @@ static void test_gb18030_utf8tomb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = gb18030_utf8tomb(&symbol, data[i].data, &ret_length, gbdata);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] 0x%04X != 0x%04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}
@ -261,14 +261,14 @@ static void test_gb18030_utf8tosb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = gb18030_utf8tosb(data[i].eci, data[i].data, &ret_length, gbdata, data[i].full_multibyte);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] %04X != %04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}
@ -311,12 +311,12 @@ static void test_gb18030_cpy(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
gb18030_cpy(data[i].data, &ret_length, gbdata, data[i].full_multibyte);
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] %04X != %04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}

View File

@ -129,14 +129,14 @@ static void test_gb2312_utf8tomb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = gb2312_utf8tomb(&symbol, data[i].data, &ret_length, gbdata);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] %04X != %04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}
@ -200,14 +200,14 @@ static void test_gb2312_utf8tosb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = gb2312_utf8tosb(data[i].eci, data[i].data, &ret_length, gbdata, data[i].full_multibyte);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] %04X != %04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}
@ -250,12 +250,12 @@ static void test_gb2312_cpy(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
gb2312_cpy(data[i].data, &ret_length, gbdata, data[i].full_multibyte);
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(gbdata[j], data[i].expected_gbdata[j], "i:%d gbdata[%d] %04X != %04X\n", i, j, gbdata[j], data[i].expected_gbdata[j]);
}
}

View File

@ -173,7 +173,7 @@ static void test_input(void)
}
symbol->debug = ZINT_DEBUG_TEST; // Needed to get codeword dump in errtxt
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
ret = ZBarcode_Encode(symbol, data[i].data, length);
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);

118
backend/tests/test_pcx.c Normal file
View File

@ -0,0 +1,118 @@
/*
libzint - the open source barcode library
Copyright (C) 2020 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* vim: set ts=4 sw=4 et : */
#include "testcommon.h"
static void test_pcx(void)
{
testStart("");
if (system("identify --version > /dev/null") != 0) {
printf("SKIPPED. ImageMagick identify not available\n");
testFinish();
return;
}
int ret;
struct item {
int symbology;
int option_1;
int option_2;
char* fgcolour;
char* bgcolour;
float scale;
unsigned char* data;
};
// s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<"))
struct item data[] = {
/* 0*/ { BARCODE_CODE128, -1, -1, NULL, NULL, 0, "AIM" },
/* 1*/ { BARCODE_QRCODE, 2, 1, NULL, NULL, 0, "1234567890" },
/* 2*/ { BARCODE_DOTCODE, -1, -1, NULL, NULL, 0, "2741" },
/* 3*/ { BARCODE_MAXICODE, -1, -1, NULL, NULL, 0, "1" },
/* 4*/ { BARCODE_GRIDMATRIX, -1, -1, NULL, NULL, 0.75, "Grid Matrix" },
/* 5*/ { BARCODE_CODABLOCKF, -1, 20, "FFFFFF", "000000", 0, "1234567890123456789012345678901234567890" },
/* 6*/ { BARCODE_CODE128, -1, -1, "C3C3C3", NULL, 0, "AIM" },
/* 7*/ { BARCODE_QRCODE, 2, 1, NULL, "D2E3F4", 0, "1234567890" },
/* 8*/ { BARCODE_DOTCODE, -1, -1, "C2C100", "E0E1F2", 0, "2741" },
/* 9*/ { BARCODE_ULTRA, -1, -1, NULL, NULL, 0, "ULTRACODE_123456789!" },
};
int data_size = sizeof(data) / sizeof(struct item);
for (int i = 0; i < data_size; i++) {
struct zint_symbol* symbol = ZBarcode_Create();
assert_nonnull(symbol, "Symbol not created\n");
symbol->symbology = data[i].symbology;
if (data[i].option_1 != -1) {
symbol->option_1 = data[i].option_1;
}
if (data[i].option_2 != -1) {
symbol->option_2 = data[i].option_2;
}
if (data[i].fgcolour != NULL) {
strcpy(symbol->fgcolour, data[i].fgcolour);
}
if (data[i].bgcolour != NULL) {
strcpy(symbol->bgcolour, data[i].bgcolour);
}
if (data[i].scale != 0) {
symbol->scale = data[i].scale;
}
int length = strlen(data[i].data);
ret = ZBarcode_Encode(symbol, data[i].data, length);
assert_zero(ret, "i:%d %s ZBarcode_Encode ret %d != 0 %s\n", i, testUtilBarcodeName(data[i].symbology), ret, symbol->errtxt);
char* filename = "out.pcx";
strcpy(symbol->outfile, filename);
ret = ZBarcode_Print(symbol, 0);
assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
//ret = system("identify out.pcx > /dev/null");
ret = system("identify out.pcx");
assert_zero(ret, "i:%d %s identify %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
ZBarcode_Delete(symbol);
}
testFinish();
}
int main()
{
test_pcx();
testReport();
return 0;
}

View File

@ -33,6 +33,8 @@
#include <sys/stat.h>
//#define TEST_PRINT_GENERATE_EXPECTED 1
//#define TEST_PRINT_OVERWRITE_EXPECTED "bmp,emf,eps,gif,pcx,png,svg,tif,txt"
#define TEST_PRINT_OVERWRITE_EXPECTED ""
static void test_print(void)
{
@ -47,9 +49,9 @@ static void test_print(void)
char* expected_file;
};
struct item data[] = {
/* 0*/ { BARCODE_CODE128, -1, -1, "AIM", "code128_aim"},
/* 1*/ { BARCODE_QRCODE, 2, 1, "1234567890", "qr_v1_m"},
/* 2*/ { BARCODE_DOTCODE, -1, -1, "2741", "dotcode_aim_fig7"},
/* 0*/ { BARCODE_CODE128, -1, -1, "AIM", "code128_aim" },
/* 1*/ { BARCODE_QRCODE, 2, 1, "1234567890", "qr_v1_m" },
/* 2*/ { BARCODE_DOTCODE, -1, -1, "2741", "dotcode_aim_fig7" },
};
int data_size = sizeof(data) / sizeof(struct item);
@ -119,11 +121,13 @@ static void test_print(void)
#ifdef TEST_PRINT_GENERATE_EXPECTED
if (j == 0) {
printf(" /*%3d*/ { %s, %d, %d, \"%s\", \"%s\"},\n",
printf(" /*%3d*/ { %s, %d, %d, \"%s\", \"%s\" },\n",
i, testUtilBarcodeName(data[i].symbology), data[i].option_1, data[i].option_2, testUtilEscape(data[i].data, length, escaped, escaped_size), data[i].expected_file);
}
if (strstr(TEST_PRINT_OVERWRITE_EXPECTED, exts[j])) {
ret = rename(symbol->outfile, expected_file);
assert_zero(ret, "i:%d rename(%s, %s) ret %d != 0\n", i, symbol->outfile, expected_file, ret);
}
#else

View File

@ -147,14 +147,14 @@ static void test_sjis_utf8tomb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = sjis_utf8tomb(&symbol, data[i].data, &ret_length, jisdata);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] %04X != %04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
}
}
@ -211,14 +211,14 @@ static void test_sjis_utf8tosb(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
ret = sjis_utf8tosb(data[i].eci, data[i].data, &ret_length, jisdata, data[i].full_multibyte);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
if (ret == 0) {
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] %04X != %04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
}
}
@ -260,12 +260,12 @@ static void test_sjis_cpy(void)
for (int i = 0; i < data_size; i++) {
int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
size_t ret_length = length;
sjis_cpy(data[i].data, &ret_length, jisdata, data[i].full_multibyte);
assert_equal(ret_length, data[i].ret_length, "i:%d ret_length %zu != %zu\n", i, ret_length, data[i].ret_length);
for (int j = 0; j < ret_length; j++) {
for (int j = 0; j < (int) ret_length; j++) {
assert_equal(jisdata[j], data[i].expected_jisdata[j], "i:%d jisdata[%d] %04X != %04X\n", i, j, jisdata[j], data[i].expected_jisdata[j]);
}
}