2016-07-29 06:58:33 +12:00
|
|
|
/* gif.c - Handles output to gif file */
|
|
|
|
/*
|
|
|
|
libzint - the open source barcode library
|
2024-01-05 09:11:04 +13:00
|
|
|
Copyright (C) 2009-2024 Robin Stuart <rstuart114@gmail.com>
|
2016-07-29 06:58:33 +12:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
2017-10-24 08:37:52 +13:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
2016-07-29 06:58:33 +12:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
2017-10-24 08:37:52 +13:00
|
|
|
documentation and/or other materials provided with the distribution.
|
2016-07-29 06:58:33 +12:00
|
|
|
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
|
2017-10-24 08:37:52 +13:00
|
|
|
without specific prior written permission.
|
2016-07-29 06:58:33 +12:00
|
|
|
|
|
|
|
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
|
2017-10-24 08:37:52 +13:00
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
2016-07-29 06:58:33 +12:00
|
|
|
SUCH DAMAGE.
|
|
|
|
*/
|
2022-07-15 03:01:30 +12:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
2016-07-29 06:58:33 +12:00
|
|
|
|
2021-06-10 22:15:39 +12:00
|
|
|
#include <errno.h>
|
2016-07-29 06:58:33 +12:00
|
|
|
#include <stdio.h>
|
2022-09-14 09:08:08 +12:00
|
|
|
#include "common.h"
|
2023-12-28 08:20:19 +13:00
|
|
|
#include "filemem.h"
|
- API: add new zint_symbol `dpmm` field for output resolution (BMP/
EMF/PCX/PNG/TIF only, i.e. excluding EPS, GIF & SVG)
- Add support for specifying scale by X-dimension and resolution
with new option `--scalexdimdp` for CLI/Tcl & new API function
`ZBarcode_Scale_From_XdimDp()` (+ `ZBarcode_XdimDp_From_Scale()`
& `ZBarcode_Default_Xdim()`) and new GUI popup; manual: document
- BMP/EMF/PCX/PNG/TIF: use new `dpmm` resolution field (for EMF
following Inkscape)
- backend_qt: add `dpmm()`, `vectorWidth()`, `vectorHeight()`,
`noPng()`, `getVersion()`, `takesGS1AIData()`, & `XdimDp` stuff
incl. new `QZintXdimDp` struct for passing around scale vars &
use in `getAsCLI()`; add comments
- Raise `scale` limit to 200 (from 100) to allow for large dpmm
- output: create directories & subdirectories as necessary for
output path using new function `out_fopen()` and use in BMP/EMF/
EPS/GIF/PCX/PNG/SVG/TIF
- DPLEIT/DPIDENT: format HRT according to (incomplete)
documentation, and set default height to 72X (from 50X)
- CODE128B renamed to CODE128AB as can use subsets A and/or B
- CODABAR: fix minimum height calc
- EMF: fix indexing of handles (zero-based not 1-based)
- GUI: fix symbology zap (previous technique of clearing and
re-loading settings without doing a sync no longer works);
fix UPCEAN guard descent enable
- MAILMARK: better error message if input < 14 characters
- GUI: add "Default" button for DAFT tracker ratio & enable/disable
various default buttons; use new `takesGS1AIData()` to
enable/disable GS1-specific checkboxes
- CLI: use new `validate_float()` to parse float options (7
significant digits allowed only, no scientific notation)
- DATAMATRIX/GRIDMATRIX/PDF417/QR/ULTRA: micro-optimize structapp
ID parse
- library/CLI: fiddle with static asserts (make CHAR_BIT sensitive,
supposedly)
- win32/README: update building libpng (assembly removed)
- README.linux: document incompatibility of Qt6 >= 6.3
- manual: expand Barcode Studio waffle
- test suite: change range separator to hyphen and allow multiple
excludes
2022-12-03 10:39:01 +13:00
|
|
|
#include "output.h"
|
2016-07-29 06:58:33 +12:00
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
/* Set LZW buffer paging size to this in expectation that compressed data will fit for typical scalings */
|
2021-09-21 01:56:27 +12:00
|
|
|
#define GIF_LZW_PAGE_SIZE 0x100000 /* Megabyte */
|
2016-09-04 05:45:09 +12:00
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
struct gif_state {
|
2023-12-28 08:20:19 +13:00
|
|
|
struct filemem *fmp;
|
2021-07-27 02:29:05 +12:00
|
|
|
unsigned char *pOut;
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
const unsigned char *pIn;
|
2023-12-23 10:29:54 +13:00
|
|
|
const unsigned char *pInEnd;
|
|
|
|
size_t OutLength;
|
|
|
|
size_t OutPosCur;
|
|
|
|
size_t OutByteCountPos;
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned short ClearCode;
|
|
|
|
unsigned short FreeCode;
|
|
|
|
char fByteCountByteSet;
|
2023-12-23 10:29:54 +13:00
|
|
|
char fOutPaged;
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned char OutBitsFree;
|
|
|
|
unsigned short NodeAxon[4096];
|
|
|
|
unsigned short NodeNext[4096];
|
|
|
|
unsigned char NodePix[4096];
|
2023-12-23 10:29:54 +13:00
|
|
|
unsigned char map[256];
|
2024-01-05 09:11:04 +13:00
|
|
|
};
|
2016-09-04 05:45:09 +12:00
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static void gif_BufferNextByte(struct gif_state *pState) {
|
2016-09-06 09:06:50 +12:00
|
|
|
(pState->OutPosCur)++;
|
2023-12-23 10:29:54 +13:00
|
|
|
if (pState->fOutPaged && pState->OutPosCur + 2 >= pState->OutLength) {
|
|
|
|
/* Keep last 256 bytes so `OutByteCountPos` within range */
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(pState->pOut, 1, pState->OutPosCur - 256, pState->fmp);
|
2023-12-23 10:29:54 +13:00
|
|
|
memmove(pState->pOut, pState->pOut + pState->OutPosCur - 256, 256);
|
|
|
|
pState->OutByteCountPos -= pState->OutPosCur - 256;
|
|
|
|
pState->OutPosCur = 256;
|
|
|
|
}
|
2016-09-06 09:06:50 +12:00
|
|
|
/* Check if this position is a byte count position
|
2023-12-23 10:29:54 +13:00
|
|
|
* `fByteCountByteSet` indicates, if byte count position bytes should be
|
2016-09-06 09:06:50 +12:00
|
|
|
* inserted in general.
|
|
|
|
* If this is true, and the distance to the last byte count position is 256
|
|
|
|
* (e.g. 255 bytes in between), a byte count byte is inserted, and the value
|
|
|
|
* of the last one is set to 255.
|
|
|
|
* */
|
|
|
|
if (pState->fByteCountByteSet && (pState->OutByteCountPos + 256 == pState->OutPosCur)) {
|
|
|
|
(pState->pOut)[pState->OutByteCountPos] = 255;
|
|
|
|
pState->OutByteCountPos = pState->OutPosCur;
|
|
|
|
(pState->OutPosCur)++;
|
|
|
|
}
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2016-09-06 09:06:50 +12:00
|
|
|
(pState->pOut)[pState->OutPosCur] = 0x00;
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static void gif_AddCodeToBuffer(struct gif_state *pState, unsigned short CodeIn, unsigned char CodeBits) {
|
2016-09-06 09:06:50 +12:00
|
|
|
/* Check, if we may fill up the current byte completely */
|
|
|
|
if (CodeBits >= pState->OutBitsFree) {
|
2021-07-27 02:29:05 +12:00
|
|
|
(pState->pOut)[pState->OutPosCur] |= (unsigned char) (CodeIn << (8 - pState->OutBitsFree));
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_BufferNextByte(pState);
|
2016-09-06 09:06:50 +12:00
|
|
|
CodeIn = (unsigned short) (CodeIn >> pState->OutBitsFree);
|
|
|
|
CodeBits -= pState->OutBitsFree;
|
|
|
|
pState->OutBitsFree = 8;
|
|
|
|
/* Write a full byte if there are at least 8 code bits left */
|
|
|
|
if (CodeBits >= pState->OutBitsFree) {
|
|
|
|
(pState->pOut)[pState->OutPosCur] = (unsigned char) CodeIn;
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_BufferNextByte(pState);
|
2016-09-06 09:06:50 +12:00
|
|
|
CodeIn = (unsigned short) (CodeIn >> 8);
|
|
|
|
CodeBits -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The remaining bits of CodeIn fit in the current byte. */
|
|
|
|
if (CodeBits > 0) {
|
2021-07-27 02:29:05 +12:00
|
|
|
(pState->pOut)[pState->OutPosCur] |= (unsigned char) (CodeIn << (8 - pState->OutBitsFree));
|
2016-09-06 09:06:50 +12:00
|
|
|
pState->OutBitsFree -= CodeBits;
|
|
|
|
}
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static void gif_FlushStringTable(struct gif_state *pState) {
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned short Pos;
|
|
|
|
for (Pos = 0; Pos < pState->ClearCode; Pos++) {
|
|
|
|
(pState->NodeAxon)[Pos] = 0;
|
|
|
|
}
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static unsigned short gif_FindPixelOutlet(struct gif_state *pState, unsigned short HeadNode, unsigned char Byte) {
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned short Outlet;
|
2016-09-04 05:45:09 +12:00
|
|
|
|
2016-09-06 09:06:50 +12:00
|
|
|
Outlet = (pState->NodeAxon)[HeadNode];
|
|
|
|
while (Outlet) {
|
|
|
|
if ((pState->NodePix)[Outlet] == Byte)
|
|
|
|
return Outlet;
|
|
|
|
Outlet = (pState->NodeNext)[Outlet];
|
|
|
|
}
|
|
|
|
return 0;
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static int gif_NextCode(struct gif_state *pState, unsigned char *pPixelValueCur, unsigned char CodeBits) {
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned short UpNode;
|
|
|
|
unsigned short DownNode;
|
|
|
|
/* start with the root node for last pixel chain */
|
|
|
|
UpNode = *pPixelValueCur;
|
2023-12-23 10:29:54 +13:00
|
|
|
if (pState->pIn == pState->pInEnd) {
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_AddCodeToBuffer(pState, UpNode, CodeBits);
|
2023-12-23 10:29:54 +13:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pPixelValueCur = pState->map[*pState->pIn++];
|
2016-09-06 09:06:50 +12:00
|
|
|
/* Follow the string table and the data stream to the end of the longest string that has a code */
|
2024-01-05 09:11:04 +13:00
|
|
|
while (0 != (DownNode = gif_FindPixelOutlet(pState, UpNode, *pPixelValueCur))) {
|
2016-09-06 09:06:50 +12:00
|
|
|
UpNode = DownNode;
|
2023-12-23 10:29:54 +13:00
|
|
|
if (pState->pIn == pState->pInEnd) {
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_AddCodeToBuffer(pState, UpNode, CodeBits);
|
2023-12-23 10:29:54 +13:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pPixelValueCur = pState->map[*pState->pIn++];
|
2016-09-06 09:06:50 +12:00
|
|
|
}
|
|
|
|
/* Submit 'UpNode' which is the code of the longest string */
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_AddCodeToBuffer(pState, UpNode, CodeBits);
|
2016-09-06 09:06:50 +12:00
|
|
|
/* ... and extend the string by appending 'PixelValueCur' */
|
|
|
|
/* Create a successor node for 'PixelValueCur' whose code is 'freecode' */
|
|
|
|
(pState->NodePix)[pState->FreeCode] = *pPixelValueCur;
|
|
|
|
(pState->NodeAxon)[pState->FreeCode] = (pState->NodeNext)[pState->FreeCode] = 0;
|
|
|
|
/* ...and link it to the end of the chain emanating from fg_axon[UpNode]. */
|
|
|
|
DownNode = (pState->NodeAxon)[UpNode];
|
|
|
|
if (!DownNode) {
|
|
|
|
(pState->NodeAxon)[UpNode] = pState->FreeCode;
|
|
|
|
} else {
|
|
|
|
while ((pState->NodeNext)[DownNode]) {
|
|
|
|
DownNode = (pState->NodeNext)[DownNode];
|
|
|
|
}
|
|
|
|
(pState->NodeNext)[DownNode] = pState->FreeCode;
|
|
|
|
}
|
|
|
|
return 1;
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
static int gif_lzw(struct gif_state *pState, int paletteBitSize) {
|
2016-09-06 09:06:50 +12:00
|
|
|
unsigned char PixelValueCur;
|
|
|
|
unsigned char CodeBits;
|
|
|
|
unsigned short Pos;
|
|
|
|
|
2022-07-15 03:01:30 +12:00
|
|
|
/* > Get first data byte */
|
2023-12-23 10:29:54 +13:00
|
|
|
if (pState->pIn == pState->pInEnd)
|
2016-09-06 09:06:50 +12:00
|
|
|
return 0;
|
2023-12-23 10:29:54 +13:00
|
|
|
PixelValueCur = pState->map[*pState->pIn++];
|
2020-04-09 07:36:22 +12:00
|
|
|
/* Number of bits per data item (=pixel)
|
|
|
|
* We need at least a value of 2, otherwise the cc and eoi code consumes
|
|
|
|
* the whole string table
|
|
|
|
*/
|
|
|
|
if (paletteBitSize == 1)
|
|
|
|
paletteBitSize = 2;
|
|
|
|
|
|
|
|
/* initial size of compression codes */
|
2021-07-27 02:29:05 +12:00
|
|
|
CodeBits = paletteBitSize + 1;
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->ClearCode = (1 << paletteBitSize);
|
2021-07-27 02:29:05 +12:00
|
|
|
pState->FreeCode = pState->ClearCode + 2;
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->OutBitsFree = 8;
|
2023-12-23 10:29:54 +13:00
|
|
|
pState->OutPosCur = 0;
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->fByteCountByteSet = 0;
|
|
|
|
|
|
|
|
for (Pos = 0; Pos < pState->ClearCode; Pos++)
|
|
|
|
(pState->NodePix)[Pos] = (unsigned char) Pos;
|
2016-09-06 09:06:50 +12:00
|
|
|
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_FlushStringTable(pState);
|
2016-09-06 09:06:50 +12:00
|
|
|
|
|
|
|
/* Write what the GIF specification calls the "code size". */
|
2020-04-09 07:36:22 +12:00
|
|
|
(pState->pOut)[pState->OutPosCur] = paletteBitSize;
|
2016-09-06 09:06:50 +12:00
|
|
|
/* Reserve first bytecount byte */
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_BufferNextByte(pState);
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->OutByteCountPos = pState->OutPosCur;
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_BufferNextByte(pState);
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->fByteCountByteSet = 1;
|
2016-09-06 09:06:50 +12:00
|
|
|
/* Submit one 'ClearCode' as the first code */
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_AddCodeToBuffer(pState, pState->ClearCode, CodeBits);
|
2016-09-06 09:06:50 +12:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* generate and save the next code, which may consist of multiple input pixels. */
|
2024-01-05 09:11:04 +13:00
|
|
|
if (!gif_NextCode(pState, &PixelValueCur, CodeBits)) { /* Check for end of data stream */
|
2016-09-06 09:06:50 +12:00
|
|
|
/* submit 'eoi' as the last item of the code stream */
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_AddCodeToBuffer(pState, (unsigned short) (pState->ClearCode + 1), CodeBits);
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->fByteCountByteSet = 0;
|
|
|
|
if (pState->OutBitsFree < 8) {
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_BufferNextByte(pState);
|
2016-09-06 09:06:50 +12:00
|
|
|
}
|
2022-07-15 03:01:30 +12:00
|
|
|
/* > Update last bytecount byte; */
|
2020-04-09 07:36:22 +12:00
|
|
|
if (pState->OutByteCountPos < pState->OutPosCur) {
|
2021-07-27 02:29:05 +12:00
|
|
|
(pState->pOut)[pState->OutByteCountPos]
|
|
|
|
= (unsigned char) (pState->OutPosCur - pState->OutByteCountPos - 1);
|
2016-09-06 09:06:50 +12:00
|
|
|
}
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->OutPosCur++;
|
2023-12-23 10:29:54 +13:00
|
|
|
return 1;
|
2016-09-06 09:06:50 +12:00
|
|
|
}
|
|
|
|
/* Check for currently last code */
|
2020-04-09 07:36:22 +12:00
|
|
|
if (pState->FreeCode == (1U << CodeBits))
|
2016-09-06 09:06:50 +12:00
|
|
|
CodeBits++;
|
2020-04-09 07:36:22 +12:00
|
|
|
pState->FreeCode++;
|
2023-12-23 10:29:54 +13:00
|
|
|
/* Check for full stringtable - for widest compatibility with gif decoders, empty when 0xfff, not 0x1000 */
|
2020-04-09 07:36:22 +12:00
|
|
|
if (pState->FreeCode == 0xfff) {
|
2024-01-05 09:11:04 +13:00
|
|
|
gif_FlushStringTable(pState);
|
|
|
|
gif_AddCodeToBuffer(pState, pState->ClearCode, CodeBits);
|
2016-09-06 09:06:50 +12:00
|
|
|
|
2020-04-09 07:36:22 +12:00
|
|
|
CodeBits = (unsigned char) (1 + paletteBitSize);
|
|
|
|
pState->FreeCode = (unsigned short) (pState->ClearCode + 2);
|
2016-09-06 09:06:50 +12:00
|
|
|
}
|
|
|
|
}
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
|
2020-04-09 07:36:22 +12:00
|
|
|
/*
|
|
|
|
* Called function to save in gif format
|
|
|
|
*/
|
2020-11-02 07:32:55 +13:00
|
|
|
INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf) {
|
2023-12-28 08:20:19 +13:00
|
|
|
struct filemem fm;
|
2020-04-08 06:36:49 +12:00
|
|
|
unsigned char outbuf[10];
|
2020-04-09 07:36:22 +12:00
|
|
|
unsigned char paletteRGB[10][3];
|
2023-12-23 10:29:54 +13:00
|
|
|
int paletteCount, i;
|
2020-04-09 07:36:22 +12:00
|
|
|
int paletteBitSize;
|
|
|
|
int paletteSize;
|
2024-01-05 09:11:04 +13:00
|
|
|
struct gif_state State;
|
2020-08-04 04:13:25 +12:00
|
|
|
int transparent_index;
|
2020-08-04 11:36:27 +12:00
|
|
|
int bgindex = -1, fgindex = -1;
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
static const unsigned char RGBUnused[3] = {0,0,0};
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
unsigned char RGBfg[3];
|
|
|
|
unsigned char RGBbg[3];
|
|
|
|
unsigned char fgalpha;
|
|
|
|
unsigned char bgalpha;
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
const size_t bitmapSize = (size_t) symbol->bitmap_height * symbol->bitmap_width;
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
(void) out_colour_get_rgb(symbol->fgcolour, &RGBfg[0], &RGBfg[1], &RGBfg[2], &fgalpha);
|
|
|
|
(void) out_colour_get_rgb(symbol->bgcolour, &RGBbg[0], &RGBbg[1], &RGBbg[2], &bgalpha);
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
/* prepare state array */
|
|
|
|
State.pIn = pixelbuf;
|
|
|
|
State.pInEnd = pixelbuf + bitmapSize;
|
2020-05-06 12:33:56 +12:00
|
|
|
/* Allow for overhead of 4 == code size + byte count + overflow byte + zero terminator */
|
2023-12-23 10:29:54 +13:00
|
|
|
State.OutLength = bitmapSize + 4;
|
|
|
|
State.fOutPaged = State.OutLength > GIF_LZW_PAGE_SIZE;
|
|
|
|
if (State.fOutPaged) {
|
|
|
|
State.OutLength = GIF_LZW_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
if (!(State.pOut = (unsigned char *) malloc(State.OutLength))) {
|
|
|
|
strcpy(symbol->errtxt, "614: Insufficient memory for LZW buffer");
|
|
|
|
return ZINT_ERROR_MEMORY;
|
2021-09-19 04:15:16 +12:00
|
|
|
}
|
2016-07-29 06:58:33 +12:00
|
|
|
|
2023-12-28 08:20:19 +13:00
|
|
|
State.fmp = &fm;
|
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
/* Open output file in binary mode */
|
2023-12-28 08:20:19 +13:00
|
|
|
if (!fm_open(State.fmp, symbol, "wb")) {
|
|
|
|
sprintf(symbol->errtxt, "611: Could not open output file (%d: %.30s)", State.fmp->err,
|
|
|
|
strerror(State.fmp->err));
|
|
|
|
free(State.pOut);
|
|
|
|
return ZINT_ERROR_FILE_ACCESS;
|
2023-12-23 10:29:54 +13:00
|
|
|
}
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
|
2020-04-09 07:36:22 +12:00
|
|
|
/*
|
|
|
|
* Build a table of the used palette items.
|
|
|
|
* Currently, there are the following 10 colour codes:
|
|
|
|
* '0': standard background
|
|
|
|
* '1': standard foreground
|
|
|
|
* 'W': white
|
|
|
|
* 'C': cyan
|
|
|
|
* 'B': blue
|
|
|
|
* 'M': magenta
|
|
|
|
* 'R': red
|
|
|
|
* 'Y': yellow
|
|
|
|
* 'G': green
|
|
|
|
* 'K': black
|
|
|
|
* '0' and '1' may be identical to one of the other values
|
2016-09-06 09:06:50 +12:00
|
|
|
*/
|
2023-12-23 10:29:54 +13:00
|
|
|
memset(State.map, 0, sizeof(State.map));
|
|
|
|
if (symbol->symbology == BARCODE_ULTRA) {
|
|
|
|
static const unsigned char ultra_chars[8] = { 'W', 'C', 'B', 'M', 'R', 'Y', 'G', 'K' };
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
State.map[ultra_chars[i]] = i;
|
|
|
|
out_colour_char_to_rgb(ultra_chars[i], &paletteRGB[i][0], &paletteRGB[i][1], &paletteRGB[i][2]);
|
2020-04-09 07:36:22 +12:00
|
|
|
}
|
2023-12-23 10:29:54 +13:00
|
|
|
paletteCount = 8;
|
|
|
|
paletteBitSize = 3;
|
|
|
|
|
|
|
|
/* For Ultracode, have foreground only if have bind/box */
|
|
|
|
if (symbol->border_width > 0 && (symbol->output_options & (BARCODE_BIND | BARCODE_BOX | BARCODE_BIND_TOP))) {
|
|
|
|
/* Check whether can re-use black */
|
|
|
|
if (RGBfg[0] == 0 && RGBfg[1] == 0 && RGBfg[2] == 0) {
|
|
|
|
State.map['1'] = fgindex = 7; /* Re-use black */
|
|
|
|
} else {
|
|
|
|
State.map['1'] = fgindex = paletteCount;
|
|
|
|
memcpy(paletteRGB[paletteCount++], RGBfg, 3);
|
|
|
|
paletteBitSize = 4;
|
2020-04-09 07:36:22 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 10:29:54 +13:00
|
|
|
/* For Ultracode, have background only if have whitespace/quiet zones */
|
|
|
|
if (symbol->whitespace_width > 0 || symbol->whitespace_height > 0
|
|
|
|
|| ((symbol->output_options & BARCODE_QUIET_ZONES)
|
|
|
|
&& !(symbol->output_options & BARCODE_NO_QUIET_ZONES))) {
|
|
|
|
/* Check whether can re-use white */
|
|
|
|
if (RGBbg[0] == 0xff && RGBbg[1] == 0xff && RGBbg[2] == 0xff && bgalpha == fgalpha) {
|
|
|
|
State.map['0'] = bgindex = 0; /* Re-use white */
|
|
|
|
} else {
|
|
|
|
State.map['0'] = bgindex = paletteCount;
|
|
|
|
memcpy(paletteRGB[paletteCount++], RGBbg, 3);
|
|
|
|
paletteBitSize = 4;
|
|
|
|
}
|
2020-04-09 07:36:22 +12:00
|
|
|
}
|
2023-12-23 10:29:54 +13:00
|
|
|
} else {
|
|
|
|
State.map['0'] = bgindex = 0;
|
|
|
|
memcpy(paletteRGB[bgindex], RGBbg, 3);
|
|
|
|
State.map['1'] = fgindex = 1;
|
|
|
|
memcpy(paletteRGB[fgindex], RGBfg, 3);
|
|
|
|
paletteCount = 2;
|
|
|
|
paletteBitSize = 1;
|
2020-04-09 07:36:22 +12:00
|
|
|
}
|
2020-05-06 12:33:56 +12:00
|
|
|
|
2020-08-04 04:13:25 +12:00
|
|
|
/* Set transparency */
|
|
|
|
/* Note: does not allow both transparent foreground and background -
|
2021-07-27 02:29:05 +12:00
|
|
|
* background takes priority */
|
2020-08-04 04:13:25 +12:00
|
|
|
transparent_index = -1;
|
2023-12-23 10:29:54 +13:00
|
|
|
if (bgalpha == 0 && bgindex != -1) {
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
/* Transparent background */
|
|
|
|
transparent_index = bgindex;
|
2023-12-23 10:29:54 +13:00
|
|
|
} else if (fgalpha == 0 && fgindex != -1) {
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
/* Transparent foreground */
|
|
|
|
transparent_index = fgindex;
|
2020-08-04 04:13:25 +12:00
|
|
|
}
|
|
|
|
|
2020-04-09 07:36:22 +12:00
|
|
|
/* palette size 2 ^ bit size */
|
2021-07-27 02:29:05 +12:00
|
|
|
paletteSize = 1 << paletteBitSize;
|
2016-09-04 05:45:09 +12:00
|
|
|
|
|
|
|
/* GIF signature (6) */
|
2024-01-05 09:11:04 +13:00
|
|
|
fm_write(transparent_index == -1 ? "GIF87a" : "GIF89a", 1, 6, State.fmp);
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Screen Descriptor (7) */
|
|
|
|
/* Screen Width */
|
2024-01-05 09:11:04 +13:00
|
|
|
outbuf[0] = (unsigned char) (0xff & symbol->bitmap_width);
|
|
|
|
outbuf[1] = (unsigned char) (0xff & (symbol->bitmap_width >> 8));
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Screen Height */
|
2024-01-05 09:11:04 +13:00
|
|
|
outbuf[2] = (unsigned char) (0xff & symbol->bitmap_height);
|
|
|
|
outbuf[3] = (unsigned char) (0xff & (symbol->bitmap_height >> 8));
|
2016-09-04 05:45:09 +12:00
|
|
|
/* write ImageBits-1 to the three least significant bits of byte 5 of
|
|
|
|
* the Screen Descriptor
|
2020-04-08 06:36:49 +12:00
|
|
|
* Bits 76543210
|
|
|
|
* 1 : Global colour map
|
|
|
|
* 111 : 8 bit colour depth of the palette
|
|
|
|
* 0 : Not ordered in decreasing importance
|
2023-12-23 10:29:54 +13:00
|
|
|
* xxx : palette bit size - 1
|
2020-04-09 07:36:22 +12:00
|
|
|
*/
|
|
|
|
outbuf[4] = (unsigned char) (0xf0 | (0x7 & (paletteBitSize - 1)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Background colour index
|
|
|
|
* Default to 0. If colour code 0 or K is present, it is used as index
|
2016-09-04 05:45:09 +12:00
|
|
|
*/
|
2023-12-23 10:29:54 +13:00
|
|
|
outbuf[5] = bgindex == -1 ? 0 : bgindex;
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Byte 7 must be 0x00 */
|
|
|
|
outbuf[6] = 0x00;
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(outbuf, 1, 7, State.fmp);
|
2020-04-09 07:36:22 +12:00
|
|
|
/* Global Color Table (paletteSize*3) */
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(paletteRGB, 1, 3*paletteCount, State.fmp);
|
2020-04-09 07:36:22 +12:00
|
|
|
/* add unused palette items to fill palette size */
|
2023-12-23 10:29:54 +13:00
|
|
|
for (i = paletteCount; i < paletteSize; i++) {
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(RGBUnused, 1, 3, State.fmp);
|
2020-04-09 07:36:22 +12:00
|
|
|
}
|
2016-09-04 05:45:09 +12:00
|
|
|
|
|
|
|
/* Graphic control extension (8) */
|
|
|
|
/* A graphic control extension block is used for overlay gifs.
|
|
|
|
* This is necessary to define a transparent color.
|
|
|
|
*/
|
2020-08-04 04:13:25 +12:00
|
|
|
if (transparent_index != -1) {
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Extension Introducer = '!' */
|
2024-05-28 07:55:04 +12:00
|
|
|
outbuf[0] = '!';
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Graphic Control Label */
|
2024-05-28 07:55:04 +12:00
|
|
|
outbuf[1] = 0xf9;
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Block Size */
|
|
|
|
outbuf[2] = 4;
|
|
|
|
/* Packet fields:
|
|
|
|
* 3 Reserved
|
|
|
|
* 3 Disposal Method: 0 No Action, 1 No Dispose, 2: Background, 3: Prev.
|
|
|
|
* 1 User Input Flag: 0: no user input, 1: user input
|
|
|
|
* 1 Transparent Color Flag: 0: No Transparency, 1: Transparency index
|
|
|
|
*/
|
|
|
|
outbuf[3] = 1;
|
|
|
|
/* Delay Time */
|
|
|
|
outbuf[4] = 0;
|
|
|
|
outbuf[5] = 0;
|
|
|
|
/* Transparent Color Index */
|
2020-08-04 04:13:25 +12:00
|
|
|
outbuf[6] = (unsigned char) transparent_index;
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Block Terminator */
|
|
|
|
outbuf[7] = 0;
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(outbuf, 1, 8, State.fmp);
|
2016-09-04 05:45:09 +12:00
|
|
|
}
|
|
|
|
/* Image Descriptor */
|
|
|
|
/* Image separator character = ',' */
|
2024-05-28 10:19:45 +12:00
|
|
|
outbuf[0] = ',';
|
2016-09-04 05:45:09 +12:00
|
|
|
/* "Image Left" */
|
|
|
|
outbuf[1] = 0x00;
|
|
|
|
outbuf[2] = 0x00;
|
|
|
|
/* "Image Top" */
|
|
|
|
outbuf[3] = 0x00;
|
|
|
|
outbuf[4] = 0x00;
|
|
|
|
/* Image Width (low byte first) */
|
2016-09-11 19:42:31 +12:00
|
|
|
outbuf[5] = (unsigned char) (0xff & symbol->bitmap_width);
|
2024-01-05 09:11:04 +13:00
|
|
|
outbuf[6] = (unsigned char) (0xff & (symbol->bitmap_width >> 8));
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Image Height */
|
2016-09-11 19:42:31 +12:00
|
|
|
outbuf[7] = (unsigned char) (0xff & symbol->bitmap_height);
|
2024-01-05 09:11:04 +13:00
|
|
|
outbuf[8] = (unsigned char) (0xff & (symbol->bitmap_height >> 8));
|
2016-09-06 09:06:50 +12:00
|
|
|
|
2016-09-04 05:45:09 +12:00
|
|
|
/* Byte 10 contains the interlaced flag and
|
|
|
|
* information on the local color table.
|
|
|
|
* There is no local color table if its most significant bit is reset.
|
|
|
|
*/
|
2020-04-08 06:36:49 +12:00
|
|
|
outbuf[9] = 0x00;
|
2023-12-28 08:20:19 +13:00
|
|
|
fm_write(outbuf, 1, 10, State.fmp);
|
2020-04-09 07:36:22 +12:00
|
|
|
|
2016-09-04 05:45:09 +12:00
|
|
|
/* call lzw encoding */
|
2023-12-23 10:29:54 +13:00
|
|
|
if (!gif_lzw(&State, paletteBitSize)) {
|
2021-09-19 04:15:16 +12:00
|
|
|
free(State.pOut);
|
2023-12-28 08:20:19 +13:00
|
|
|
(void) fm_close(State.fmp, symbol);
|
2021-07-27 02:29:05 +12:00
|
|
|
strcpy(symbol->errtxt, "613: Insufficient memory for LZW buffer");
|
2016-09-04 05:45:09 +12:00
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
2024-05-28 10:19:45 +12:00
|
|
|
fm_write(State.pOut, 1, State.OutPosCur, State.fmp);
|
2021-09-19 04:15:16 +12:00
|
|
|
free(State.pOut);
|
2016-09-06 09:06:50 +12:00
|
|
|
|
|
|
|
/* GIF terminator */
|
2024-05-28 07:55:04 +12:00
|
|
|
fm_putc(';', State.fmp);
|
2022-12-20 05:28:15 +13:00
|
|
|
|
2023-12-28 08:20:19 +13:00
|
|
|
if (fm_error(State.fmp)) {
|
|
|
|
sprintf(symbol->errtxt, "615: Incomplete write to output (%d: %.30s)", State.fmp->err,
|
|
|
|
strerror(State.fmp->err));
|
|
|
|
(void) fm_close(State.fmp, symbol);
|
2022-12-20 05:28:15 +13:00
|
|
|
return ZINT_ERROR_FILE_WRITE;
|
|
|
|
}
|
|
|
|
|
2023-12-28 08:20:19 +13:00
|
|
|
if (!fm_close(State.fmp, symbol)) {
|
|
|
|
sprintf(symbol->errtxt, "617: Failure on closing output file (%d: %.30s)", State.fmp->err,
|
|
|
|
strerror(State.fmp->err));
|
|
|
|
return ZINT_ERROR_FILE_WRITE;
|
2021-07-27 02:29:05 +12:00
|
|
|
}
|
2016-09-06 09:06:50 +12:00
|
|
|
|
2016-07-29 06:58:33 +12:00
|
|
|
return 0;
|
2017-10-24 08:37:52 +13:00
|
|
|
}
|
2022-07-15 03:01:30 +12:00
|
|
|
|
|
|
|
/* vim: set ts=4 sw=4 et : */
|