Panda3D
Loading...
Searching...
No Matches
bmp.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file bmp.h
10 */
11
12#ifndef _BMP_H_
13#define _BMP_H_
14
15#include "pandabase.h"
16#include "pnmimage_base.h"
17
18/* prototypes */
19static unsigned long BMPlenfileheader(int classv);
20static unsigned long BMPleninfoheader(int classv);
21static unsigned long BMPlenrgbtable(int classv, unsigned long bitcount);
22static unsigned long BMPlenline(int classv, unsigned long bitcount, unsigned long x);
23static unsigned long BMPlenbits(int classv, unsigned long bitcount, unsigned long x, unsigned long y);
24static unsigned long BMPlenfile(int classv, unsigned long bitcount, unsigned long x, unsigned long y);
25static unsigned long BMPoffbits(int classv, unsigned long bitcount);
26/*
27 * Classves of BMP files
28 */
29
30#define C_WIN 1
31#define C_OS2 2
32#define C_WINV2 3
33#define C_WINV3 4
34#define C_WINV4 5
35#define C_WINV5 6
36
37static char er_internal[] = "%s: internal error!";
38
39static unsigned long
40BMPlenfileheader(int classv)
41{
42 switch (classv)
43 {
44 case C_WIN:
45 case C_OS2:
46 case C_WINV2:
47 case C_WINV3:
48 case C_WINV4:
49 case C_WINV5:
50 return 14;
51 default:
52 pm_error(er_internal, "BMPlenfileheader");
53 return 0;
54 }
55}
56
57static unsigned long
58BMPleninfoheader(int classv)
59{
60 switch (classv)
61 {
62 case C_WIN:
63 return 40;
64 case C_OS2:
65 return 12;
66 case C_WINV2:
67 return 52;
68 case C_WINV3:
69 return 56;
70 case C_WINV4:
71 return 108;
72 case C_WINV5:
73 return 124;
74 default:
75 pm_error(er_internal, "BMPleninfoheader");
76 return 0;
77 }
78}
79
80static unsigned long
81BMPlenrgbtable(int classv, unsigned long bitcount)
82{
83 unsigned long lenrgb;
84
85 if (bitcount > 8) {
86 return 0;
87 }
88
89 if (bitcount < 1)
90 {
91 pm_error(er_internal, "BMPlenrgbtable");
92 return 0;
93 }
94 lenrgb = (classv == C_OS2) ? 3 : 4;
95 return (1 << bitcount) * lenrgb;
96}
97
98/*
99 * length, in bytes, of a line of the image
100 *
101 * Evidently each row is padded on the right as needed to make it a
102 * multiple of 4 bytes long. This appears to be true of both
103 * OS/2 and Windows BMP files.
104 */
105static unsigned long
106BMPlenline(int classv, unsigned long bitcount, unsigned long x)
107{
108 unsigned long bitsperline;
109
110 bitsperline = x * bitcount;
111
112 /*
113 * if bitsperline is not a multiple of 32, then round
114 * bitsperline up to the next multiple of 32.
115 */
116 if ((bitsperline % 32) != 0)
117 {
118 bitsperline += (32 - (bitsperline % 32));
119 }
120
121 if ((bitsperline % 32) != 0)
122 {
123 pm_error(er_internal, "BMPlenline");
124 return 0;
125 }
126
127 /* number of bytes per line == bitsperline/8 */
128 return bitsperline >> 3;
129}
130
131/* return the number of bytes used to store the image bits */
132static unsigned long
133BMPlenbits(
134 int classv,
135 unsigned long bitcount,
136 unsigned long x,
137 unsigned long y)
138{
139 return y * BMPlenline(classv, bitcount, x);
140}
141
142/* return the offset to the BMP image bits */
143static unsigned long
144BMPoffbits(
145 int classv,
146 unsigned long bitcount)
147{
148 return BMPlenfileheader(classv)
149 + BMPleninfoheader(classv)
150 + BMPlenrgbtable(classv, bitcount);
151}
152
153/* return the size of the BMP file in bytes */
154static unsigned long
155BMPlenfile(
156 int classv,
157 unsigned long bitcount,
158 unsigned long x,
159 unsigned long y)
160{
161 return BMPoffbits(classv, bitcount)
162 + BMPlenbits(classv, bitcount, x, y);
163}
164
165#endif /* _BMP_H_ */
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void pm_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.