| Bsoft 2.0.5 Bernard's software package

rwMIFF.cpp
Go to the documentation of this file.
1
9 #include "rwMIFF.h"
10 #include "utilities.h"
11 #include <fstream>
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
30 int readMIFF(Bimage* p, int readdata, int img_select)
31 {
32 ifstream fimg(p->file_name());
33 if ( fimg.fail() ) return -1;
34
35 int depth(8), colors(0), rle(0);
36 long i, j, k, c(3);
37 long n, nimg(0), imgsize(0), datatypesize(1);
38 char aline[MAXLINELEN], header[10000], *tag;
39 unsigned char** buf = new unsigned char*[10000];
40 unsigned char* lut = NULL;
41 Vector3<long> size(1,1,1), pagesize(1,1,1);
42
43 memset(aline, 0, MAXLINELEN);
44 memset(header, 0, 10000);
45 memset(buf, 0, 10000);
46
47 p->compound_type(TRGB);
48
49 // Read multiple images
50 while ( fimg.getline(header, MAXLINELEN) ) {
51 if ( strstr(header, "id=ImageMagick") ) {
52 if ( verbose & VERB_DEBUG )
53 cout << "DEBUG readMIFF: image " << nimg << endl;
54
55 while ( fimg.getline(aline, MAXLINELEN) && !strchr( aline, ':' ) )
56 strcat( header, aline );
57 if ( verbose & VERB_DEBUG )
58 cout << "DEBUG readMIFF: " << header << endl;
59 if ( ( tag = strstr( header, "columns" ) ) )
60 sscanf( tag, "columns=%ld", &size[0] );
61 if ( ( tag = strstr( header, "rows" ) ) )
62 sscanf( tag, "rows=%ld", &size[1] );
63 if ( ( tag = strstr( header, "page" ) ) )
64 sscanf( tag, "page=%ldx%ld", &pagesize[0], &pagesize[1] );
65 if ( ( tag = strstr( header, "depth" ) ) )
66 sscanf( tag, "depth=%d", &depth );
67 if ( ( tag = strstr( header, "colors" ) ) )
68 sscanf( tag, "colors=%d", &colors );
69 if ( ( tag = strstr( header, "compression=RunlengthEncoded" ) ) ) rle = 1;
70 switch ( depth ) {
71 case 8: p->data_type(UCharacter); break;
72 case 16: p->data_type(UShort); break;
73 case 32: p->data_type(Integer); break;
74 default: break;
75 }
76 if ( ( tag = strstr( header, "class" ) ) ) { // Default is DirectClass
77 if ( strstr(tag, "PseudoClass") ) {
78 c = 1;
79 lut = new unsigned char[3*colors];
80 // fread( lut, 3*colors, 1, fimg );
81 fimg.read((char *)lut, 3*colors);
82 }
83 }
84 if ( nimg == 0 ) {
85 datatypesize = p->data_type_size();
86 imgsize = size[0]*size[1]*c;
87 p->data_offset(fimg.tellg());
88 }
89 if ( readdata ) {
90 buf[nimg] = new unsigned char[imgsize];
91 // fread( buf[nimg], imgsize, datatypesize, fimg );
92 fimg.read((char *)buf[nimg], imgsize*datatypesize);
93 } else {
94 // fseek(fimg, imgsize*datatypesize, SEEK_CUR);
95 fimg.seekg(imgsize*datatypesize, ios_base::cur);
96 }
97 nimg++;
98 }
99 }
100
101 fimg.close();
102
103 p->size(size);
104 p->page_size(pagesize);
105 p->channels(3);
106
107 if ( img_select > -1 ) {
108 if ( img_select >= nimg ) img_select = nimg - 1;
109 nimg = 1;
110 } else img_select = -1;
111
112 p->images(nimg);
113
114 if ( !readdata ) {
115 delete[] buf;
116 if ( lut ) delete [] lut;
117 return 0;
118 }
119
120 if ( verbose & VERB_DEBUG )
121 cout << "DEBUG rwMIFF: Packing " << p->images() << " 2D images of size " << p->size() << endl;
122
123 imgsize = size[0]*size[1]*p->channels();
124
125 p->data_alloc();
126
127 for ( n=i=j=0; n<nimg; n++ ) {
128 if ( img_select < 0 || img_select == n ) {
129 if ( colors ) {
130 for ( j=0; j<size[0]*size[1]; j++ )
131 for ( c=0, k=3*buf[n][j]; c<3; c++, i++, k++ ) p->set(i, lut[k]);
132 } else {
133 for ( j=0; j<imgsize; j++, i++ ) p->set(i, buf[n][j]);
134 }
135 }
136 delete [] buf[n];
137 }
138 delete [] buf;
139 if ( lut ) delete [] lut;
140
141 return 0;
142 }
143
150 int writeMIFF(Bimage* p)
151 {
152 p->simple_to_rgb();
153
154 ofstream fimg(p->file_name());
155 if ( fimg.fail() ) return -1;
156
157 long typesize = p->data_type_size();
158 long imgsize = p->sizeX()*p->sizeY()*p->channels()*typesize;
159 long z, n;
160 unsigned char* data = p->data_pointer();
161
162 for ( n=0; n<p->images(); n++ ) {
163 for ( z=0; z<p->sizeZ(); z++, data+=imgsize ) {
164 if ( verbose & VERB_DEBUG )
165 cout << "DEBUG writeMIFF: Writing image " << n << " slice " << z << endl;
166
167 fimg << "id=ImageMagick" << endl;
168 fimg << "class=DirectClass" << endl;
169 fimg << "columns=" << p->size()[0] << " rows=" << p->size()[1] << " depth=" << typesize*8 << endl;
170 fimg << "page=" << p->size()[0] << "x" << p->size()[1] << "+0+0" << endl;
171 fimg << "\f" << endl << ":" << endl;
172
173 fimg.write((char *)data, imgsize);
174 }
175 }
176
177 fimg.close();
178
179 return 0;
180 }
readMIFF
int readMIFF(Bimage *p, int readdata, int img_select)
Reading an Image Magick map image file format. A 2D image format used for X-windows display....
Definition: rwMIFF.cpp:30
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1585
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Integer
Definition: utilities.h:154
Bimage::simple_to_rgb
void simple_to_rgb()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:21
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
writeMIFF
int writeMIFF(Bimage *p)
Writing an Image Magick map image file format. A 2D image format used for X-windows display.
Definition: rwMIFF.cpp:150
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
verbose
int verbose
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::data_offset
long data_offset()
Definition: Bimage.h:265
Bimage::channels
long channels()
Definition: Bimage.h:371
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
TRGB
Definition: Bimage.h:62
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
UCharacter
Definition: utilities.h:149
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< long >
UShort
Definition: utilities.h:151
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:480
Bimage::images
long images()
Definition: Bimage.h:491
rwMIFF.h
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349