Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

readrasterheader.cc

Go to the documentation of this file.
00001 // prog to read a header...
00002 // g++ -O readrasterheader.c -o readrasterheader
00003 // BK 21-Nov-2000
00004 
00005 using namespace std;
00006 #include <iostream>
00007 #include <fstream>
00008 #include <cstdlib>                      // exit()
00009 //#include <cstdio>
00010 #include <netinet/in.h>                 // ntohl byteorder x86-HP unix
00011 typedef unsigned long ulong;
00012 typedef unsigned char uchar;
00013 
00014 // changed reading get,read,put, functions for g++v3.2
00015 
00016 
00017 
00018 /* ************************************************************ */
00019 #define RAS_MAGIC       0x59a66a95
00020 struct header
00021   {
00022   ulong   magic;              /* magic number */
00023   ulong   width;              /* width (pixels) of image */
00024   ulong   height;             /* height (pixels) of image */
00025   ulong   depth;              /* depth (1, 8, or 24 bits) of pixel */
00026   ulong   length;             /* length (bytes) of image */
00027   ulong   type;               /* type of file; see RT_* below */
00028   ulong   maptype;            /* type of colormap; see RMT_* below */
00029   ulong   maplength;          /* length (bytes) of following map */
00030   /* color map follows for ras_maplength bytes, followed by image */
00031   };
00032         /* Sun supported ras_type's */
00033 #define RT_OLD          0       /* Raw pixrect image in 68000 byte order */
00034 #define RT_STANDARD     1       /* Raw pixrect image in 68000 byte order */
00035 #define RT_BYTE_ENCODED 2       /* Run-length compression of bytes */
00036 #define RT_FORMAT_RGB   3       /* XRGB or RGB instead of XBGR or BGR */
00037 #define RT_FORMAT_TIFF  4       /* tiff <-> standard rasterfile */
00038 #define RT_FORMAT_IFF   5       /* iff (TAAC format) <-> standard rasterfile */
00039 
00040 /* Sun registered ras_maptype's */
00041 #define RMT_RAW         2
00042 /* Sun supported ras_maptype's */
00043 #define RMT_NONE        0       /* ras_maplength is expected to be 0 */
00044 #define RMT_EQUAL_RGB   1       /* red[ras_maplength/3],green[],blue[] */
00045 
00046 typedef struct {
00047         int              type;
00048         int              length;
00049         unsigned char   *map[3];
00050 } colormap;
00051 
00052 
00053 /* ************************************************************ */
00054 int main (int argc,char *argv[])
00055   {
00056   if (argc<2)
00057     {
00058     cerr << argv[0] << " -- display header of SUN raster file.\n";
00059     cerr << "usage:   " << argv[0] << " rasfile\n";
00060     cerr << "example: " << argv[0] << " file.ras\n";
00061     cerr << "ex2:     " << argv[0] << " file.ras |& head -n 9\n";
00062     exit(1);
00063     }
00064 
00065   // ______ Open file ______
00066   ifstream ifp(argv[1]);
00067   if (!ifp) {cerr << "infile " << argv[1] << " does not exist\n"; exit(1);}
00068   
00069   // ______ Read HEADER 32 bytes and convert to big endian ______
00070   header HEADER;
00071   //cerr << "1: " << &HEADER << endl;
00072   //cerr << "1: " << (uchar *) &HEADER << endl;
00073   ifp.read(((char *) &HEADER), sizeof(HEADER));
00074 //  for(int i=0; i<int(sizeof(HEADER)); ++i)
00075 //    {
00076 //    char tmp;
00077 //    ifp.get(tmp);
00078 //    HEADER[i] = uchar(tmp);
00079 //    // orig: ifp.get(*((unsigned char*)(&HEADER) + i));
00080 //    }
00081   //
00082   HEADER.magic     = ntohl(HEADER.magic);
00083   HEADER.width     = ntohl(HEADER.width);
00084   HEADER.height    = ntohl(HEADER.height);
00085   HEADER.depth     = ntohl(HEADER.depth);
00086   HEADER.length    = ntohl(HEADER.length);
00087   HEADER.type      = ntohl(HEADER.type);
00088   HEADER.maptype   = ntohl(HEADER.maptype);
00089   HEADER.maplength = ntohl(HEADER.maplength);
00090 
00091   cerr << "Header for file: " << argv[1]
00092        << "\nmagic#:    " << HEADER.magic
00093        << "\nwidth:     " << HEADER.width
00094        << "\nheight:    " << HEADER.height
00095        << "\ndepth:     " << HEADER.depth
00096        << "\nlength:    " << HEADER.length
00097        << "\ntype:      " << HEADER.type
00098        << "\nmaptype:   " << HEADER.maptype
00099        << "\nmaplength: " << HEADER.maplength
00100        << endl;
00101 
00102   // ______ Colormap, which type/how to output ??? ______
00103   // original i had: unsigned char CMAP[3][HEADER.maplength/3];
00104   // but if.get not for unsigned? so cast or read differently?
00105   unsigned char CMAP[3][HEADER.maplength/3];
00106   for(int color=0; color<3; ++color)
00107     {
00108     for(int i=0; i<int(HEADER.maplength/3); ++i)
00109       {
00110       ifp.get(*((char*)(&CMAP[color][i])));
00111       //char tmp;
00112       //ifp.get(tmp);
00113       //CMAP[color][i] = uchar(tmp);// is this the same?
00114       }
00115       //ifp.get(*((unsigned char*)(&CMAP[color][i])));
00116     }
00117 
00118   // ______ Write cmap to screen ______
00119   for(int i=0; i<int(HEADER.maplength/3); ++i)
00120     cerr << "rgb: " << int(CMAP[0][i]) << " " 
00121                     << int(CMAP[1][i]) << " " 
00122                     << int(CMAP[2][i]) << "\n";
00123 
00124   // ______ Tidy tidy ______
00125   ifp.close();
00126   return 0;
00127   }

Generated on Fri Apr 22 15:58:00 2005 for Doris by doxygen 1.3.6