diff --git a/src/osgPlugins/bmp/ReaderWriterBMP.cpp b/src/osgPlugins/bmp/ReaderWriterBMP.cpp index 6aa910cc3..8dbed5f3d 100644 --- a/src/osgPlugins/bmp/ReaderWriterBMP.cpp +++ b/src/osgPlugins/bmp/ReaderWriterBMP.cpp @@ -148,117 +148,138 @@ int *numComponents_ret) int ncolours; int ncomp=0; bool swap=false; // dont need to swap bytes - long infsize; //size of BMPinfo in bytes // actual size of the bitmap header; 12=os2; 40 = normal; 64=os2.1 struct bmpheader hd; struct BMPInfo inf; bmperror = ERROR_NO_ERROR; fread((char *)&hd, sizeof(bmpheader), 1, fp); - fread((char *)&infsize, sizeof(long), 1, fp); - fread((char *)&inf, infsize-sizeof(long), 1, fp); if (hd.FileType != MB) { swapbyte(&(hd.FileType)); swap=true; if (hd.FileType != MB) { bmperror=ERROR_READING_HEADER; - return NULL; } } if (hd.FileType == MB) { + long infsize; //size of BMPinfo in bytes unsigned char *cols=NULL; // dynamic colour palette unsigned char *imbuff; // returned to sender & as read from the disk - if (swap) { // inverse the field of the header which need swapping - swapbyte(&hd.siz[0]); - swapbyte(&hd.siz[1]); - swapbyte(&inf.Colorbits); - swapbyte(&inf.width); - swapbyte(&inf.height); - swapbyte(&inf.ImageSize); - } - long size = hd.siz[1]*65536+hd.siz[0]; - size -= sizeof(bmpheader)+infsize; - if (inf.ImageSize0) buffer = (unsigned char *)malloc( 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 - if (ncomp==BW) { // BW currently error on display - osg::notify(osg::NOTICE)<<"BMP file: "<>=inf.Colorbits; - } - } - // osg::notify(osg::NOTICE) << endl; - } - off+=doff; - if (ncomp>2) { // yes bill, colours are usually BGR aren't they - for(int i=0; i0) 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; i0?ncomp:3; + switch (ncomp) { + case BW: + *numComponents_ret = 3; + break; + case IA: + case RGB: + case RGBA: + *numComponents_ret = ncomp; + break; + default: + *numComponents_ret = 3; + break; + } return buffer; }