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 */
19 static unsigned long BMPlenfileheader(int classv);
20 static unsigned long BMPleninfoheader(int classv);
21 static unsigned long BMPlenrgbtable(int classv, unsigned long bitcount);
22 static unsigned long BMPlenline(int classv, unsigned long bitcount, unsigned long x);
23 static unsigned long BMPlenbits(int classv, unsigned long bitcount, unsigned long x, unsigned long y);
24 static unsigned long BMPlenfile(int classv, unsigned long bitcount, unsigned long x, unsigned long y);
25 static 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 
37 static char er_internal[] = "%s: internal error!";
38 
39 static unsigned long
40 BMPlenfileheader(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 
58 static unsigned long
59 BMPleninfoheader(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 
81 static unsigned long
82 BMPlenrgbtable(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  */
118 static unsigned long
119 BMPlenline(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 */
145 static unsigned long
146 BMPlenbits(
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 */
156 static unsigned long
157 BMPoffbits(
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 */
167 static unsigned long
168 BMPlenfile(
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.
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.