diff --git a/src/osgPlugins/bmp/ReaderWriterBMP.cpp b/src/osgPlugins/bmp/ReaderWriterBMP.cpp index 111949daa..b278c8f2a 100644 --- a/src/osgPlugins/bmp/ReaderWriterBMP.cpp +++ b/src/osgPlugins/bmp/ReaderWriterBMP.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -40,8 +41,9 @@ struct bmpheader { short FileType; //always MB unsigned short siz[2]; // a dword for whole file size - make unsigned Feb 2002 short Reserved1, Reserved2; //reserved for future purposes - short offset[2]; //offset to image in bytes + unsigned short offset[2]; //offset to image in bytes }; + struct BMPInfo { long width; //width of the image in pixels long height; // height of the image in pixels @@ -53,7 +55,8 @@ struct BMPInfo { long YpixPerMeter; //pixels per meter in Y long ColorUsed; //number of colors used long Important; //number of "important" colors - long os2stuff[6]; // allows os2.1 with 64 bytes to be read. Dont know what these are yet. +// unsigned char rgbquad[4]; + // long os2stuff[6]; // storage for os2.1 with 64 bytes to be read. Dont know what these are yet. }; int @@ -141,7 +144,6 @@ int *numComponents_ret) unsigned char *buffer=NULL; // returned to sender & as read from the disk bmperror = ERROR_NO_FILE; - FILE *fp = fopen(filename, "rb"); if (!fp) return NULL; @@ -167,94 +169,99 @@ int *numComponents_ret) unsigned char *imbuff; // returned to sender & as read from the disk fread((char *)&infsize, sizeof(long), 1, fp); // insert inside 'the file is bmp' clause if (swap) swapbyte(&infsize); - if ((infsize-sizeof(long))>16; - int npln = inf.height&0xffff; // number of planes - int cbits = inf.height>>16; - inf.width=wd; - inf.height=ht; - inf.planes=npln; - inf.Colorbits=cbits; - inf.ColorUsed=(long)pow(2.0,(double)inf.Colorbits); // infer the colours - } - long size = hd.siz[1]*65536+hd.siz[0]; - int ncpal=4; // default number of colours per palette entry - size -= sizeof(bmpheader)+infsize; - if (inf.ImageSize0) buffer = (unsigned char *)malloc( (ncomp==BW?3:ncomp)*inf.width*inf.height*sizeof(unsigned char)); // to be returned - else buffer = (unsigned char *)malloc( 3*inf.width*inf.height*sizeof(unsigned char)); // default full colour to be returned - - unsigned long off=0; - unsigned long rowbytes=ncomp*sizeof(unsigned char)*inf.width; - unsigned long doff=(rowbytes)/4; - if ((rowbytes%4)) doff++; // round up if needed - doff*=4; // to find dword alignment - for(int j=0; jBW) memcpy(buffer+j*rowbytes, imbuff+off, rowbytes); // pack bytes closely - else { // find from the palette.. - unsigned char *imptr=imbuff+inf.ColorUsed*ncpal; // add size of the palette- start of image - int npixperbyte=8/inf.Colorbits; // no of pixels per byte - for (int ii=0; ii>jj); // fill N High end bits - for (jj=0; jj>((npixperbyte-1-jj)*inf.Colorbits); - buffer[3*(j*inf.width+ii*npixperbyte+jj)+0]=cols[ncpal*colidx+2]; - buffer[3*(j*inf.width+ii*npixperbyte+jj)+1]=cols[ncpal*colidx+1]; - buffer[3*(j*inf.width+ii*npixperbyte+jj)+2]=cols[ncpal*colidx]; - mask>>=inf.Colorbits; - } - } - } - off+=doff; - if (ncomp>2) { // yes bill, colours are usually BGR aren't they - for(int i=0; i>16; + int npln = inf.height&0xffff; // number of planes + int cbits = inf.height>>16; + inf.width=wd; + inf.height=ht; + inf.planes=npln; + inf.Colorbits=cbits; + inf.ColorUsed=(long)pow(2.0,(double)inf.Colorbits); // infer the colours + } + osg::notify(osg::WARN) << "readbmp " <0) buffer = (unsigned char *)malloc( (ncomp==BW?3:ncomp)*inf.width*inf.height*sizeof(unsigned char)); // to be returned + else buffer = (unsigned char *)malloc( 3*inf.width*inf.height*sizeof(unsigned char)); // default full colour to be returned + + unsigned long off=0; + unsigned long rowbytes=ncomp*sizeof(unsigned char)*inf.width; + unsigned long doff=(rowbytes)/4; + if ((rowbytes%4)) doff++; // round up if needed + doff*=4; // to find dword alignment + for(int j=0; jBW) memcpy(buffer+j*rowbytes, imbuff+off, rowbytes); // pack bytes closely + else { // find from the palette.. + unsigned char *imptr=imbuff+inf.ColorUsed*ncpal; // add size of the palette- start of image + int npixperbyte=8/inf.Colorbits; // no of pixels per byte + for (int ii=0; ii>jj); // fill N High end bits + for (jj=0; jj>((npixperbyte-1-jj)*inf.Colorbits); + buffer[3*(j*inf.width+ii*npixperbyte+jj)+0]=cols[ncpal*colidx+2]; + buffer[3*(j*inf.width+ii*npixperbyte+jj)+1]=cols[ncpal*colidx+1]; + buffer[3*(j*inf.width+ii*npixperbyte+jj)+2]=cols[ncpal*colidx]; + mask>>=inf.Colorbits; + } + } + } + off+=doff; + if (ncomp>2) { // yes bill, colours are usually BGR aren't they + for(int i=0; i>16; // high word + hd.siz[1]=(size&0xffff); // low word + fwrite(&hd, sizeof(hd), 1, fp); + struct BMPInfo inf; + osg::notify(osg::WARN) << "sizes "<