1 /* bitmap.h: definition for a bitmap type.  No packing is done by
2  *   default; each pixel is represented by an entire byte.  Among other
3  *   things, this means the type can be used for both grayscale and binary
4  *   images.
5  *
6  * Copyright (C) 1992 Free Software Foundation, Inc.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef BITMAP_H
23 #define BITMAP_H
24 
25 #include <stdio.h>
26 #include "bounding-box.h"
27 #include "types.h"
28 
29 
30 /* If the bitmap holds 8-bit values, rather than one-bit, the
31    definition of BLACK here is wrong.  So don't use it in that case!  */
32 #define WHITE 0
33 #define BLACK 1
34 
35 
36 /* The basic structure and macros to access it.  */
37 typedef struct
38 {
39   dimensions_type dimensions;
40   one_byte *bitmap;
41 } bitmap_type;
42 
43 /* The dimensions of the bitmap, in pixels.  */
44 #define BITMAP_DIMENSIONS(b)  ((b).dimensions)
45 
46 /* The pixels, represented as an array of bytes (in contiguous storage).
47    Each pixel is a single byte, even for binary fonts.  */
48 #define BITMAP_BITS(b)  ((b).bitmap)
49 
50 /* These are convenient abbreviations for getting inside the members.  */
51 #define BITMAP_WIDTH(b)  DIMENSIONS_WIDTH (BITMAP_DIMENSIONS (b))
52 #define BITMAP_HEIGHT(b)  DIMENSIONS_HEIGHT (BITMAP_DIMENSIONS (b))
53 
54 /* This is the address of the first pixel in the row ROW.  */
55 #define BITMAP_ROW(b, row)  (BITMAP_BITS (b) + (row) * BITMAP_WIDTH (b))
56 
57 /* This is the pixel at [ROW,COL].  */
58 #define BITMAP_PIXEL(b, row, col)					\
59   (*(BITMAP_BITS (b) + (row) * BITMAP_WIDTH (b) + (col)))
60 
61 #define BITMAP_VALID_PIXEL(b, row, col)					\
62    	(0 <= (row) && (row) < BITMAP_HEIGHT (b) 			\
63          && 0 <= (col) && (col) < BITMAP_WIDTH (b))
64 
65 /* Assume that the pixel at [ROW,COL] itself is black.  */
66 
67 #define BITMAP_INTERIOR_PIXEL(b, row, col)				\
68    	(0 != (row) && (row) != BITMAP_HEIGHT (b) - 1 			\
69          && 0 != (col) && (col) != BITMAP_WIDTH (b) - 1			\
70          && BITMAP_PIXEL (b, row - 1, col - 1) == BLACK			\
71          && BITMAP_PIXEL (b, row - 1, col) == BLACK			\
72          && BITMAP_PIXEL (b, row - 1, col + 1) == BLACK			\
73          && BITMAP_PIXEL (b, row, col - 1) == BLACK			\
74          && BITMAP_PIXEL (b, row, col + 1) == BLACK			\
75          && BITMAP_PIXEL (b, row + 1, col - 1) == BLACK			\
76          && BITMAP_PIXEL (b, row + 1, col) == BLACK			\
77          && BITMAP_PIXEL (b, row + 1, col + 1) == BLACK)
78 
79 /* Allocate storage for the bits, set them all to white, and return an
80    initialized structure.  */
81 extern bitmap_type new_bitmap (dimensions_type);
82 
83 /* Free that storage.  */
84 extern void free_bitmap (bitmap_type *);
85 
86 /* Make a fresh copy of BITMAP in a new structure, and return it.  */
87 extern bitmap_type copy_bitmap (bitmap_type bitmap);
88 
89 /* Return the pixels in the bitmap B enclosed by the bounding box BB.
90    The result is put in newly allocated storage.  */
91 extern bitmap_type extract_subbitmap (bitmap_type b, bounding_box_type bb);
92 
93 /* Consider the dimensions of a bitmap as a bounding box.  The bounding
94    box returned is in bitmap coordinates, rather than Cartesian, and
95    refers to pixels, rather than edges.  Specifically, this means that
96    the maximum column is one less than results from `dimensions_to_bb
97    (BITMAP_DIMENSIONS ())'.  */
98 extern bounding_box_type bitmap_to_bb (const bitmap_type);
99 
100 /* Return a vector of zero-based column numbers marking transitions from
101    black to white or white to black in ROW, which is of length WIDTH.
102    The end of the vector is marked with an element of length WIDTH + 1.
103    The first element always marks a white-to-black transition (or it's
104    0, if the first pixel in ROW is black).  */
105 extern unsigned *bitmap_find_transitions (const one_byte *row, unsigned width);
106 
107 /* Print part of or all of a bitmap.  */
108 extern void print_bounded_bitmap (FILE *, bitmap_type, bounding_box_type);
109 extern void print_bitmap (FILE *, bitmap_type);
110 
111 #endif /* not BITMAP_H */
112 
113