Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/bmp.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* bmp.c - Handles output to Windows Bitmap file */ | |
| 2 /* | |
| 3 libzint - the open source barcode library | |
| 4 Copyright (C) 2009-2024 Robin Stuart <rstuart114@gmail.com> | |
| 5 | |
| 6 Redistribution and use in source and binary forms, with or without | |
| 7 modification, are permitted provided that the following conditions | |
| 8 are met: | |
| 9 | |
| 10 1. Redistributions of source code must retain the above copyright | |
| 11 notice, this list of conditions and the following disclaimer. | |
| 12 2. Redistributions in binary form must reproduce the above copyright | |
| 13 notice, this list of conditions and the following disclaimer in the | |
| 14 documentation and/or other materials provided with the distribution. | |
| 15 3. Neither the name of the project nor the names of its contributors | |
| 16 may be used to endorse or promote products derived from this software | |
| 17 without specific prior written permission. | |
| 18 | |
| 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
| 23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 29 SUCH DAMAGE. | |
| 30 */ | |
| 31 /* SPDX-License-Identifier: BSD-3-Clause */ | |
| 32 | |
| 33 #include <errno.h> | |
| 34 #include <math.h> | |
| 35 #include <stdio.h> | |
| 36 #include "common.h" | |
| 37 #include "filemem.h" | |
| 38 #include "output.h" | |
| 39 #include "bmp.h" /* Bitmap header structure */ | |
| 40 | |
| 41 INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, const unsigned char *pixelbuf) { | |
| 42 int i, row, column; | |
| 43 int bits_per_pixel; | |
| 44 int colour_count; | |
| 45 int resolution; | |
| 46 size_t row_size, data_offset, file_size; | |
| 47 struct filemem fm; | |
| 48 struct filemem *const fmp = &fm; | |
| 49 bitmap_file_header_t file_header; | |
| 50 bitmap_info_header_t info_header; | |
| 51 color_ref_t bg; | |
| 52 color_ref_t fg; | |
| 53 color_ref_t palette[8]; | |
| 54 int ultra_fg_index = 9; | |
| 55 unsigned char map[128]; | |
| 56 unsigned char *rowbuf; | |
| 57 | |
| 58 (void) out_colour_get_rgb(symbol->fgcolour, &fg.red, &fg.green, &fg.blue, NULL /*alpha*/); | |
| 59 fg.reserved = 0x00; | |
| 60 | |
| 61 (void) out_colour_get_rgb(symbol->bgcolour, &bg.red, &bg.green, &bg.blue, NULL /*alpha*/); | |
| 62 bg.reserved = 0x00; | |
| 63 | |
| 64 if (symbol->symbology == BARCODE_ULTRA) { | |
| 65 static const unsigned char ultra_chars[8] = { 'C', 'B', 'M', 'R', 'Y', 'G', 'K', 'W' }; | |
| 66 for (i = 0; i < 8; i++) { | |
| 67 out_colour_char_to_rgb(ultra_chars[i], &palette[i].red, &palette[i].green, &palette[i].blue); | |
| 68 palette[i].reserved = 0x00; | |
| 69 if (memcmp(&palette[i], &fg, sizeof(fg)) == 0) { | |
| 70 ultra_fg_index = i + 1; | |
| 71 } | |
| 72 map[ultra_chars[i]] = i + 1; | |
| 73 } | |
| 74 bits_per_pixel = 4; | |
| 75 colour_count = ultra_fg_index == 9 ? 10 : 9; | |
| 76 map['0'] = 0; | |
| 77 map['1'] = ultra_fg_index; | |
| 78 } else { | |
| 79 bits_per_pixel = 1; | |
| 80 colour_count = 2; | |
| 81 map['0'] = 0; | |
| 82 map['1'] = 0x80; | |
| 83 } | |
| 84 row_size = 4 * (((size_t) symbol->bitmap_width * bits_per_pixel + 31) / 32); | |
| 85 data_offset = sizeof(bitmap_file_header_t) + sizeof(bitmap_info_header_t); | |
| 86 data_offset += sizeof(color_ref_t) * colour_count; | |
| 87 file_size = data_offset + row_size * symbol->bitmap_height; | |
| 88 | |
| 89 /* Must fit in `uint32_t` field in header */ | |
| 90 if (file_size != (uint32_t) file_size) { | |
| 91 return errtxt(ZINT_ERROR_MEMORY, symbol, 606, "Output size too large for file size field of BMP header"); | |
| 92 } | |
| 93 | |
| 94 if (!(rowbuf = (unsigned char *) malloc(row_size))) { | |
| 95 return errtxt(ZINT_ERROR_MEMORY, symbol, 602, "Insufficient memory for BMP row buffer"); | |
| 96 } | |
| 97 | |
| 98 out_le_u16(file_header.header_field, 0x4d42); /* "BM" */ | |
| 99 out_le_u32(file_header.file_size, file_size); | |
| 100 out_le_u32(file_header.reserved, 0); | |
| 101 out_le_u32(file_header.data_offset, data_offset); | |
| 102 | |
| 103 out_le_u32(info_header.header_size, sizeof(bitmap_info_header_t)); | |
| 104 out_le_i32(info_header.width, symbol->bitmap_width); | |
| 105 out_le_i32(info_header.height, symbol->bitmap_height); | |
| 106 out_le_u16(info_header.colour_planes, 1); | |
| 107 out_le_u16(info_header.bits_per_pixel, bits_per_pixel); | |
| 108 out_le_u32(info_header.compression_method, 0); /* BI_RGB */ | |
| 109 out_le_u32(info_header.image_size, 0); | |
| 110 resolution = symbol->dpmm ? (int) roundf(stripf(symbol->dpmm * 1000.0f)) : 0; /* pixels per metre */ | |
| 111 out_le_i32(info_header.horiz_res, resolution); | |
| 112 out_le_i32(info_header.vert_res, resolution); | |
| 113 out_le_u32(info_header.colours, colour_count); | |
| 114 out_le_u32(info_header.important_colours, colour_count); | |
| 115 | |
| 116 /* Open output file in binary mode */ | |
| 117 if (!fm_open(fmp, symbol, "wb")) { | |
| 118 errtxtf(0, symbol, 601, "Could not open BMP output file (%1$d: %2$s)", fmp->err, strerror(fmp->err)); | |
| 119 free(rowbuf); | |
| 120 return ZINT_ERROR_FILE_ACCESS; | |
| 121 } | |
| 122 | |
| 123 fm_write(&file_header, sizeof(bitmap_file_header_t), 1, fmp); | |
| 124 fm_write(&info_header, sizeof(bitmap_info_header_t), 1, fmp); | |
| 125 | |
| 126 fm_write(&bg, sizeof(color_ref_t), 1, fmp); | |
| 127 if (bits_per_pixel == 4) { | |
| 128 for (i = 0; i < 8; i++) { | |
| 129 fm_write(&palette[i], sizeof(color_ref_t), 1, fmp); | |
| 130 } | |
| 131 if (ultra_fg_index == 9) { | |
| 132 fm_write(&fg, sizeof(color_ref_t), 1, fmp); | |
| 133 } | |
| 134 } else { | |
| 135 fm_write(&fg, sizeof(color_ref_t), 1, fmp); | |
| 136 } | |
| 137 | |
| 138 /* Pixel Plotting */ | |
| 139 if (bits_per_pixel == 4) { | |
| 140 for (row = 0; row < symbol->bitmap_height; row++) { | |
| 141 const unsigned char *pb = pixelbuf + ((size_t) symbol->bitmap_width * (symbol->bitmap_height - row - 1)); | |
| 142 memset(rowbuf, 0, row_size); | |
| 143 for (column = 0; column < symbol->bitmap_width; column++) { | |
| 144 rowbuf[column >> 1] |= map[pb[column]] << (!(column & 1) << 2); | |
| 145 } | |
| 146 fm_write(rowbuf, 1, row_size, fmp); | |
| 147 } | |
| 148 } else { /* bits_per_pixel == 1 */ | |
| 149 for (row = 0; row < symbol->bitmap_height; row++) { | |
| 150 const unsigned char *pb = pixelbuf + ((size_t) symbol->bitmap_width * (symbol->bitmap_height - row - 1)); | |
| 151 memset(rowbuf, 0, row_size); | |
| 152 for (column = 0; column < symbol->bitmap_width; column++) { | |
| 153 rowbuf[column >> 3] |= map[pb[column]] >> (column & 7); | |
| 154 } | |
| 155 fm_write(rowbuf, 1, row_size, fmp); | |
| 156 } | |
| 157 } | |
| 158 free(rowbuf); | |
| 159 | |
| 160 if (fm_error(fmp)) { | |
| 161 errtxtf(0, symbol, 603, "Incomplete write of BMP output (%1$d: %2$s)", fmp->err, strerror(fmp->err)); | |
| 162 (void) fm_close(fmp, symbol); | |
| 163 return ZINT_ERROR_FILE_WRITE; | |
| 164 } | |
| 165 | |
| 166 if (!fm_close(fmp, symbol)) { | |
| 167 return errtxtf(ZINT_ERROR_FILE_WRITE, symbol, 605, "Failure on closing BMP output file (%1$d: %2$s)", | |
| 168 fmp->err, strerror(fmp->err)); | |
| 169 } | |
| 170 | |
| 171 return 0; | |
| 172 } | |
| 173 | |
| 174 /* vim: set ts=4 sw=4 et : */ |
