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 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 lenrgb = (classv == C_OS2) ? 3 : 4;
96 return (1 << bitcount) * lenrgb;
97}
98
99/*
100 * length, in bytes, of a line of the image
101 *
102 * Evidently each row is padded on the right as needed to make it a
103 * multiple of 4 bytes long. This appears to be true of both
104 * OS/2 and Windows BMP files.
105 */
106static unsigned long
107BMPlenline(int classv, unsigned long bitcount, unsigned long x)
108{
109 unsigned long bitsperline;
110
111 bitsperline = x * bitcount;
112
113 /*
114 * if bitsperline is not a multiple of 32, then round
115 * bitsperline up to the next multiple of 32.
116 */
117 if ((bitsperline % 32) != 0)
118 {
119 bitsperline += (32 - (bitsperline % 32));
120 }
121
122 if ((bitsperline % 32) != 0)
123 {
124 pm_error(er_internal, "BMPlenline");
125 return 0;
126 }
127
128 /* number of bytes per line == bitsperline/8 */
129 return bitsperline >> 3;
130}
131
132/* return the number of bytes used to store the image bits */
133static unsigned long
134BMPlenbits(
135 int classv,
136 unsigned long bitcount,
137 unsigned long x,
138 unsigned long y)
139{
140 return y * BMPlenline(classv, bitcount, x);
141}
142
143/* return the offset to the BMP image bits */
144static unsigned long
145BMPoffbits(
146 int classv,
147 unsigned long bitcount)
148{
149 return BMPlenfileheader(classv)
150 + BMPleninfoheader(classv)
151 + BMPlenrgbtable(classv, bitcount);
152}
153
154/* return the size of the BMP file in bytes */
155static unsigned long
156BMPlenfile(
157 int classv,
158 unsigned long bitcount,
159 unsigned long x,
160 unsigned long y)
161{
162 return BMPoffbits(classv, bitcount)
163 + BMPlenbits(classv, bitcount, x, y);
164}
165
166#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.