Panda3D

pnmimage_base.cxx
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 pnmimage_base.cxx
10 * @author drose
11 * @date 2002-08-04
12 */
13
14 #include "pnmimage_base.h"
15 #include "streamReader.h"
16 #include "streamWriter.h"
17 #include "config_pnmimage.h"
18
19 #include <stdarg.h>
20 #include <stdio.h> // for sprintf()
21
22 using std::istream;
23 using std::ostream;
24
25
26 /**
27 * Outputs the given printf-style message to the user and returns.
28 */
29 void
30 pm_message(const char *format, ...) {
31 va_list ap;
32 va_start(ap, format);
33
34 static const size_t buffer_size = 1024;
35 char buffer[buffer_size];
36 #if defined(WIN32_VC) || defined(WIN64_VC)
37 // Windows doesn't define vsnprintf(). Hope we don't overflow.
38 vsprintf(buffer, format, ap);
39 #else
40 vsnprintf(buffer, buffer_size, format, ap);
41 #endif
42 nassertv(strlen(buffer) < buffer_size);
43
44 pnmimage_cat.info() << buffer << "\n";
45
46 va_end(ap);
47 }
48
49 /**
50 * Outputs the given printf-style message to the user and terminates messily.
51 * Minimize use of this function.
52 */
53 void
54 pm_error(const char *format, ...) {
55 va_list ap;
56 va_start(ap, format);
57
58 static const size_t buffer_size = 1024;
59 char buffer[buffer_size];
60 #if defined(WIN32_VC) || defined(WIN64_VC)
61 // Windows doesn't define vsnprintf(). Hope we don't overflow.
62 vsprintf(buffer, format, ap);
63 #else
64 vsnprintf(buffer, buffer_size, format, ap);
65 #endif
66 nassertv(strlen(buffer) < buffer_size);
67
68 pnmimage_cat.error() << buffer << "\n";
69
70 va_end(ap);
71
72 // Now we're supposed to exit. Inconvenient if we were running Panda
73 // interactively, but that's the way it is.
74 exit(1);
75 }
76
77 /**
78 * Returns the number of bits sufficient to hold the indicated maxval value.
79 */
80 int
81 pm_maxvaltobits(int maxval) {
82 int bits = 1;
83 while (maxval > pm_bitstomaxval(bits)) {
84 bits++;
85 nassertr(bits != 0, 16);
86 }
87 return bits;
88 }
89
90 /**
91 * Returns the highest maxval that can be represented in the indicated number
92 * of bits.
93 */
94 int
95 pm_bitstomaxval(int bits) {
96 return ( 1 << bits ) - 1;
97 }
98
99 /**
100 * Allocates a row of cols * size bytes.
101 */
102 char *
103 pm_allocrow(int cols, int size) {
104 return (char *)PANDA_MALLOC_ARRAY(cols * size);
105 }
106
107 /**
108 * Frees the row previously allocated withm pm_allocrow().
109 */
110 void
111 pm_freerow(char *itrow) {
112 PANDA_FREE_ARRAY(itrow);
113 }
114
115
116 /* Endian I/O.
117 */
118
119 int
120 pm_readbigshort(istream *in, short *sP) {
121 StreamReader reader(in, false);
122 *sP = reader.get_be_int16();
123 return (!in->fail()) ? 0 : -1;
124 }
125
126 int
127 pm_writebigshort(ostream *out, short s) {
128 StreamWriter writer(out, false);
129 writer.add_be_int16(s);
130 return (!out->fail()) ? 0 : -1;
131 }
132
133 int
134 pm_readbiglong(istream *in, long *lP) {
135 StreamReader reader(in, false);
136 *lP = reader.get_be_int32();
137 return (!in->fail()) ? 0 : -1;
138 }
139
140 int
141 pm_writebiglong(ostream *out, long l) {
142 StreamWriter writer(out, false);
143 writer.add_be_int32(l);
144 return (!out->fail()) ? 0 : -1;
145 }
146
147 int
148 pm_readlittleshort(istream *in, short *sP) {
149 StreamReader reader(in, false);
150 *sP = reader.get_int16();
151 return (!in->fail()) ? 0 : -1;
152 }
153
154 int
155 pm_writelittleshort(ostream *out, short s) {
156 StreamWriter writer(out, false);
157 writer.add_int16(s);
158 return (!out->fail()) ? 0 : -1;
159 }
160
161 int
162 pm_readlittlelong(istream *in, long *lP) {
163 StreamReader reader(in, false);
164 *lP = reader.get_int32();
165 return (!in->fail()) ? 0 : -1;
166 }
167
168 int
169 pm_writelittlelong(ostream *out, long l) {
170 StreamWriter writer(out, false);
171 writer.add_int32(l);
172 return (!out->fail()) ? 0 : -1;
173 }
StreamReader
A class to read sequential binary data directly from an istream.
Definition: streamReader.h:28
StreamWriter
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:29
config_pnmimage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pm_allocrow
char * pm_allocrow(int cols, int size)
Allocates a row of cols * size bytes.
Definition: pnmimage_base.cxx:103
pm_maxvaltobits
int pm_maxvaltobits(int maxval)
Returns the number of bits sufficient to hold the indicated maxval value.
Definition: pnmimage_base.cxx:81
pm_bitstomaxval
int pm_bitstomaxval(int bits)
Returns the highest maxval that can be represented in the indicated number of bits.
Definition: pnmimage_base.cxx:95
pm_freerow
void pm_freerow(char *itrow)
Frees the row previously allocated withm pm_allocrow().
Definition: pnmimage_base.cxx:111
pm_error
void pm_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.
Definition: pnmimage_base.cxx:54
pm_message
void pm_message(const char *format,...)
Outputs the given printf-style message to the user and returns.
Definition: pnmimage_base.cxx:30
pnmimage_base.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
streamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
streamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.