Panda3D
Loading...
Searching...
No Matches
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
22using std::istream;
23using std::ostream;
24
25
26/**
27 * Outputs the given printf-style message to the user and returns.
28 */
29void
30pm_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 va_end(ap);
43
44 nassertv(strlen(buffer) < buffer_size);
45
46 pnmimage_cat.info() << buffer << "\n";
47}
48
49/**
50 * Outputs the given printf-style message to the user and terminates messily.
51 * Minimize use of this function.
52 */
53void
54pm_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 va_end(ap);
67
68 nassertv(strlen(buffer) < buffer_size);
69
70 pnmimage_cat.error() << buffer << "\n";
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 */
80int
81pm_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 */
94int
95pm_bitstomaxval(int bits) {
96 return ( 1 << bits ) - 1;
97}
98
99/**
100 * Allocates a row of cols * size bytes.
101 */
102char *
103pm_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 */
110void
111pm_freerow(char *itrow) {
112 PANDA_FREE_ARRAY(itrow);
113}
114
115
116/* Endian I/O.
117*/
118
119int
120pm_readbigshort(istream *in, short *sP) {
121 StreamReader reader(in, false);
122 *sP = reader.get_be_int16();
123 return (!in->fail()) ? 0 : -1;
124}
125
126int
127pm_writebigshort(ostream *out, short s) {
128 StreamWriter writer(out, false);
129 writer.add_be_int16(s);
130 return (!out->fail()) ? 0 : -1;
131}
132
133int
134pm_readbiglong(istream *in, long *lP) {
135 StreamReader reader(in, false);
136 *lP = reader.get_be_int32();
137 return (!in->fail()) ? 0 : -1;
138}
139
140int
141pm_writebiglong(ostream *out, long l) {
142 StreamWriter writer(out, false);
143 writer.add_be_int32(l);
144 return (!out->fail()) ? 0 : -1;
145}
146
147int
148pm_readlittleshort(istream *in, short *sP) {
149 StreamReader reader(in, false);
150 *sP = reader.get_int16();
151 return (!in->fail()) ? 0 : -1;
152}
153
154int
155pm_writelittleshort(ostream *out, short s) {
156 StreamWriter writer(out, false);
157 writer.add_int16(s);
158 return (!out->fail()) ? 0 : -1;
159}
160
161int
162pm_readlittlelong(istream *in, long *lP) {
163 StreamReader reader(in, false);
164 *lP = reader.get_int32();
165 return (!in->fail()) ? 0 : -1;
166}
167
168int
169pm_writelittlelong(ostream *out, long l) {
170 StreamWriter writer(out, false);
171 writer.add_int32(l);
172 return (!out->fail()) ? 0 : -1;
173}
A class to read sequential binary data directly from an istream.
A StreamWriter object is used to write sequential binary data directly to an ostream.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int pm_maxvaltobits(int maxval)
Returns the number of bits sufficient to hold the indicated maxval value.
char * pm_allocrow(int cols, int size)
Allocates a row of cols * size bytes.
int pm_bitstomaxval(int bits)
Returns the highest maxval that can be represented in the indicated number of bits.
void pm_freerow(char *itrow)
Frees the row previously allocated withm pm_allocrow().
void pm_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.
void pm_message(const char *format,...)
Outputs the given printf-style message to the user and returns.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.