Improved PCX algorithm, uses less memory

This commit is contained in:
Robin Stuart 2016-07-23 19:14:57 +01:00
parent babeaec41d
commit 7eaa82bfa1

View File

@ -42,9 +42,8 @@
int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) { int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) {
int fgred, fggrn, fgblu, bgred, bggrn, bgblu; int fgred, fggrn, fgblu, bgred, bggrn, bgblu;
int errno; int errno;
int row, column, i; int row, column, i, colour;
int run_count, sub_block; int run_count;
char pixel_colour;
FILE *pcx_file; FILE *pcx_file;
#ifndef _MSC_VER #ifndef _MSC_VER
@ -67,7 +66,7 @@ int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
} }
#ifndef _MSC_VER #ifndef _MSC_VER
unsigned char rle_row[symbol->bitmap_width * 6]; unsigned char rle_row[symbol->bitmap_width];
#else #else
unsignd char* rle_row = (unsigned char *) _alloca((symbol->bitmap_width * 6) * sizeof(unsigned char)); unsignd char* rle_row = (unsigned char *) _alloca((symbol->bitmap_width * 6) * sizeof(unsigned char));
#endif /* _MSC_VER */ #endif /* _MSC_VER */
@ -101,13 +100,13 @@ int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]);
bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]);
bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]);
/* Rotate image before plotting */ /* Rotate image before plotting */
switch (rotate_angle) { switch (rotate_angle) {
case 0: /* Plot the right way up */ case 0: /* Plot the right way up */
for (row = 0; row < image_height; row++) { for (row = 0; row < image_height; row++) {
for (column = 0; column < image_width; column++) { for (column = 0; column < image_width; column++) {
rotated_bitmap[(row * image_width) + column] = rotated_bitmap[(row * symbol->bitmap_width) + column] =
*(pixelbuf + (image_width * row) + column); *(pixelbuf + (image_width * row) + column);
} }
} }
@ -115,7 +114,7 @@ int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
case 90: /* Plot 90 degrees clockwise */ case 90: /* Plot 90 degrees clockwise */
for (row = 0; row < image_width; row++) { for (row = 0; row < image_width; row++) {
for (column = 0; column < image_height; column++) { for (column = 0; column < image_height; column++) {
rotated_bitmap[(row * image_width) + column] = rotated_bitmap[(row * image_height) + column] =
*(pixelbuf + (image_width * (image_height - column - 1)) + row); *(pixelbuf + (image_width * (image_height - column - 1)) + row);
} }
} }
@ -131,7 +130,7 @@ int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
case 270: /* Plot 90 degrees anti-clockwise */ case 270: /* Plot 90 degrees anti-clockwise */
for (row = 0; row < image_width; row++) { for (row = 0; row < image_width; row++) {
for (column = 0; column < image_height; column++) { for (column = 0; column < image_height; column++) {
rotated_bitmap[(row * image_width) + column] = rotated_bitmap[(row * image_height) + column] =
*(pixelbuf + (image_width * column) + (image_width - row - 1)); *(pixelbuf + (image_width * column) + (image_width - row - 1));
} }
} }
@ -191,62 +190,51 @@ int pcx_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
fwrite(&header, sizeof(pcx_header_t), 1, pcx_file); fwrite(&header, sizeof(pcx_header_t), 1, pcx_file);
for(row = 0; row < symbol->bitmap_height; row++) { for(row = 0; row < symbol->bitmap_height; row++) {
run_count = 1; for (colour = 0; colour < 3; colour++) {
sub_block = 0; for (column = 0; column < symbol->bitmap_width; column++) {
for(column = 1; column < symbol->bitmap_width; column++) { switch(colour) {
if ((rotated_bitmap[(row * image_width) + column - 1] case 0:
== rotated_bitmap[(row * image_width) + column]) if (rotated_bitmap[(row * symbol->bitmap_width) + column] == '1') {
&& (run_count < 63)) { rle_row[column] = fgred;
run_count++; } else {
} else { rle_row[column] = bgred;
pixel_colour = rotated_bitmap[(row * image_width) + column - 1]; }
rle_row[(sub_block * 2)] = run_count + 0xc0; break;
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width)] = run_count + 0xc0; case 1:
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width)] = run_count + 0xc0; if (rotated_bitmap[(row * symbol->bitmap_width) + column] == '1') {
if (pixel_colour == '1') { rle_row[column] = fggrn;
rle_row[(sub_block * 2) + 1] = fgred; } else {
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width) + 1] = fggrn; rle_row[column] = bggrn;
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width) + 1] = fgblu; }
} else { break;
rle_row[(sub_block * 2) + 1] = bgred; case 2:
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width) + 1] = bggrn; if (rotated_bitmap[(row * symbol->bitmap_width) + column] == '1') {
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width) + 1] = bgblu; rle_row[column] = fgblu;
} else {
rle_row[column] = bgblu;
}
break;
} }
sub_block++;
run_count = 1;
} }
} run_count = 1;
for (column = 1; column < symbol->bitmap_width; column++) {
if (run_count > 1) { if ((rle_row[column - 1] == rle_row[column]) && (run_count < 63)) {
pixel_colour = rotated_bitmap[(row * image_width) + column - 1]; run_count++;
rle_row[(sub_block * 2)] = run_count + 0xc0; } else {
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width)] = run_count + 0xc0; run_count += 0xc0;
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width)] = run_count + 0xc0; fputc(run_count, pcx_file);
if (pixel_colour == '1') { fputc(rle_row[column - 1], pcx_file);
rle_row[(sub_block * 2) + 1] = fgred; run_count = 1;
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width) + 1] = fggrn; }
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width) + 1] = fgblu;
} else {
rle_row[(sub_block * 2) + 1] = bgred;
rle_row[(sub_block * 2) + (2 * symbol->bitmap_width) + 1] = bggrn;
rle_row[(sub_block * 2) + (4 * symbol->bitmap_width) + 1] = bgblu;
} }
sub_block++;
} if (run_count > 1) {
run_count += 0xc0;
for (i = 0; i < (sub_block * 2); i++) { fputc(run_count, pcx_file);
fputc(rle_row[i], pcx_file); fputc(rle_row[column - 1], pcx_file);
} }
}
for (i = 0; i < (sub_block * 2); i++) {
fputc(rle_row[i + (2 * symbol->bitmap_width)], pcx_file);
}
for (i = 0; i < (sub_block * 2); i++) {
fputc(rle_row[i + (4 * symbol->bitmap_width)], pcx_file);
}
} }
fclose(pcx_file); fclose(pcx_file);