33 if ( fimg.fail() )
return -1;
35 int depth(8), colors(0), rle(0);
37 long n, nimg(0), imgsize(0), datatypesize(1);
39 unsigned char** buf =
new unsigned char*[10000];
40 unsigned char* lut = NULL;
44 memset(header, 0, 10000);
45 memset(buf, 0, 10000);
51 if ( strstr(header,
"id=ImageMagick") ) {
53 cout <<
"DEBUG readMIFF: image " << nimg << endl;
55 while ( fimg.getline(aline,
MAXLINELEN) && !strchr( aline,
':' ) )
56 strcat( header, aline );
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;
76 if ( ( tag = strstr( header,
"class" ) ) ) {
77 if ( strstr(tag,
"PseudoClass") ) {
79 lut =
new unsigned char[3*colors];
81 fimg.read((
char *)lut, 3*colors);
86 imgsize = size[0]*size[1]*c;
90 buf[nimg] =
new unsigned char[imgsize];
92 fimg.read((
char *)buf[nimg], imgsize*datatypesize);
95 fimg.seekg(imgsize*datatypesize, ios_base::cur);
107 if ( img_select > -1 ) {
108 if ( img_select >= nimg ) img_select = nimg - 1;
110 }
else img_select = -1;
116 if ( lut )
delete [] lut;
121 cout <<
"DEBUG rwMIFF: Packing " << p->
images() <<
" 2D images of size " << p->
size() << endl;
123 imgsize = size[0]*size[1]*p->
channels();
127 for ( n=i=j=0; n<nimg; n++ ) {
128 if ( img_select < 0 || img_select == n ) {
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]);
133 for ( j=0; j<imgsize; j++, i++ ) p->
set(i, buf[n][j]);
139 if ( lut )
delete [] lut;
155 if ( fimg.fail() )
return -1;
162 for ( n=0; n<p->
images(); n++ ) {
163 for ( z=0; z<p->
sizeZ(); z++, data+=imgsize ) {
165 cout <<
"DEBUG writeMIFF: Writing image " << n <<
" slice " << z << endl;
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;
173 fimg.write((
char *)data, imgsize);