Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pnmimage/pnmimage_base.cxx

Go to the documentation of this file.
00001 // Filename: pnmimage_base.cxx
00002 // Created by:  drose (04Aug02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "pnmimage_base.h"
00020 #include "streamReader.h"
00021 #include "streamWriter.h"
00022 
00023 #include <stdarg.h>
00024 #include <stdio.h>   // for sprintf()
00025 
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: pm_message
00029 //  Description: Outputs the given printf-style message to the user
00030 //               and returns.
00031 ////////////////////////////////////////////////////////////////////
00032 void
00033 pm_message(const char *format, ...) {
00034   va_list ap;
00035   va_start(ap, format);
00036 
00037   static const size_t buffer_size = 1024;
00038   char buffer[buffer_size];
00039 #ifdef WIN32_VC
00040   // Windows doesn't define vsnprintf().  Hope we don't overflow.
00041   vsprintf(buffer, format, ap);
00042 #else
00043   vsnprintf(buffer, buffer_size, format, ap);
00044 #endif
00045   nassertv(strlen(buffer) < buffer_size);
00046 
00047   pnmimage_cat.info() << buffer << "\n";
00048 
00049   va_end(ap);
00050 }
00051 
00052 ////////////////////////////////////////////////////////////////////
00053 //     Function: pm_error
00054 //  Description: Outputs the given printf-style message to the user
00055 //               and terminates messily.  Minimize use of this
00056 //               function.
00057 ////////////////////////////////////////////////////////////////////
00058 void
00059 pm_error(const char *format, ...) {
00060   va_list ap;
00061   va_start(ap, format);
00062 
00063   static const size_t buffer_size = 1024;
00064   char buffer[buffer_size];
00065 #ifdef WIN32_VC
00066   // Windows doesn't define vsnprintf().  Hope we don't overflow.
00067   vsprintf(buffer, format, ap);
00068 #else
00069   vsnprintf(buffer, buffer_size, format, ap);
00070 #endif
00071   nassertv(strlen(buffer) < buffer_size);
00072 
00073   pnmimage_cat.error() << buffer << "\n";
00074 
00075   va_end(ap);
00076 
00077   // Now we're supposed to exit.  Inconvenient if we were running
00078   // Panda interactively, but that's the way it is.
00079   exit(1);
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: pm_maxvaltobits
00084 //  Description: Returns the number of bits sufficient to hold the
00085 //               indicated maxval value.
00086 ////////////////////////////////////////////////////////////////////
00087 int
00088 pm_maxvaltobits(int maxval) {
00089   int bits = 1;
00090   while (maxval > pm_bitstomaxval(bits)) {
00091     bits++;
00092     nassertr(bits != 0, 16);
00093   }
00094   return bits;
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: pm_bitstomaxval
00099 //  Description: Returns the highest maxval that can be represented in
00100 //               the indicated number of bits.
00101 ////////////////////////////////////////////////////////////////////
00102 int
00103 pm_bitstomaxval(int bits) {
00104   return ( 1 << bits ) - 1;
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //     Function: pm_allocrow
00109 //  Description: Allocates a row of cols * size bytes.
00110 ////////////////////////////////////////////////////////////////////
00111 char *
00112 pm_allocrow(int cols, int size) {
00113   return new char[cols * size];
00114 }
00115 
00116 ////////////////////////////////////////////////////////////////////
00117 //     Function: pm_freerow
00118 //  Description: Frees the row previously allocated withm pm_allocrow().
00119 ////////////////////////////////////////////////////////////////////
00120 void
00121 pm_freerow(char *itrow) {
00122   delete[] itrow;
00123 }
00124 
00125 
00126 /* Endian I/O.
00127 */
00128 
00129 int
00130 pm_readbigshort(istream *in, short *sP) {
00131   StreamReader reader(in, false);
00132   *sP = reader.get_be_int16();
00133   return (!in->eof() && !in->fail()) ? 0 : -1;
00134 }
00135 
00136 int
00137 pm_writebigshort(ostream *out, short s) {
00138   StreamWriter writer(out);
00139   writer.add_be_int16(s);
00140   return (!out->fail()) ? 0 : -1;
00141 }
00142 
00143 int
00144 pm_readbiglong(istream *in, long *lP) {
00145   StreamReader reader(in, false);
00146   *lP = reader.get_be_int32();
00147   return (!in->eof() && !in->fail()) ? 0 : -1;
00148 }
00149 
00150 int
00151 pm_writebiglong(ostream *out, long l) {
00152   StreamWriter writer(out);
00153   writer.add_be_int32(l);
00154   return (!out->fail()) ? 0 : -1;
00155 }
00156 
00157 int
00158 pm_readlittleshort(istream *in, short *sP) {
00159   StreamReader reader(in, false);
00160   *sP = reader.get_int16();
00161   return (!in->eof() && !in->fail()) ? 0 : -1;
00162 }
00163 
00164 int
00165 pm_writelittleshort(ostream *out, short s) {
00166   StreamWriter writer(out);
00167   writer.add_int16(s);
00168   return (!out->fail()) ? 0 : -1;
00169 }
00170 
00171 int
00172 pm_readlittlelong(istream *in, long *lP) {
00173   StreamReader reader(in, false);
00174   *lP = reader.get_int32();
00175   return (!in->eof() && !in->fail()) ? 0 : -1;
00176 }
00177 
00178 int
00179 pm_writelittlelong(ostream *out, long l) {
00180   StreamWriter writer(out);
00181   writer.add_int32(l);
00182   return (!out->fail()) ? 0 : -1;
00183 }

Generated on Fri May 2 00:43:09 2003 for Panda by doxygen1.3