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