Panda3D
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 return 14;
52 default:
53 pm_error(er_internal, "BMPlenfileheader");
54 return 0;
55 }
56}
57
58static unsigned long
59BMPleninfoheader(int classv)
60{
61 switch (classv)
62 {
63 case C_WIN:
64 return 40;
65 case C_OS2:
66 return 12;
67 case C_WINV2:
68 return 52;
69 case C_WINV3:
70 return 56;
71 case C_WINV4:
72 return 108;
73 case C_WINV5:
74 return 124;
75 default:
76 pm_error(er_internal, "BMPleninfoheader");
77 return 0;
78 }
79}
80
81static unsigned long
82BMPlenrgbtable(int classv, unsigned long bitcount)
83{
84 unsigned long lenrgb;
85
86 if (bitcount > 8) {
87 return 0;
88 }
89
90 if (bitcount < 1)
91 {
92 pm_error(er_internal, "BMPlenrgbtable");
93 return 0;
94 }
95 switch (classv)
96 {
97 case C_WIN:
98 lenrgb = 4;
99 break;
100 case C_OS2:
101 lenrgb = 3;
102 break;
103 default:
104 pm_error(er_internal, "BMPlenrgbtable");
105 return 0;
106 }
107
108 return (1 << bitcount) * lenrgb;
109}
110
111/*
112 * length, in bytes, of a line of the image
113 *
114 * Evidently each row is padded on the right as needed to make it a
115 * multiple of 4 bytes long. This appears to be true of both
116 * OS/2 and Windows BMP files.
117 */
118static unsigned long
119BMPlenline(int classv, unsigned long bitcount, unsigned long x)
120{
121 unsigned long bitsperline;
122
123 bitsperline = x * bitcount;
124
125 /*
126 * if bitsperline is not a multiple of 32, then round
127 * bitsperline up to the next multiple of 32.
128 */
129 if ((bitsperline % 32) != 0)
130 {
131 bitsperline += (32 - (bitsperline % 32));
132 }
133
134 if ((bitsperline % 32) != 0)
135 {
136 pm_error(er_internal, "BMPlenline");
137 return 0;
138 }
139
140 /* number of bytes per line == bitsperline/8 */
141 return bitsperline >> 3;
142}
143
144/* return the number of bytes used to store the image bits */
145static unsigned long
146BMPlenbits(
147 int classv,
148 unsigned long bitcount,
149 unsigned long x,
150 unsigned long y)
151{
152 return y * BMPlenline(classv, bitcount, x);
153}
154
155/* return the offset to the BMP image bits */
156static unsigned long
157BMPoffbits(
158 int classv,
159 unsigned long bitcount)
160{
161 return BMPlenfileheader(classv)
162 + BMPleninfoheader(classv)
163 + BMPlenrgbtable(classv, bitcount);
164}
165
166/* return the size of the BMP file in bytes */
167static unsigned long
168BMPlenfile(
169 int classv,
170 unsigned long bitcount,
171 unsigned long x,
172 unsigned long y)
173{
174 return BMPoffbits(classv, bitcount)
175 + BMPlenbits(classv, bitcount, x, y);
176}
177
178#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.