- /* BMPs are stored upside down */
- for (line = (height - 1); line >= 0; line--)
- {
- int linepos = 0;
- for (column = 0; column < width;)
- {
- if (bpp < 16)
- {
- int index = 0;
- linepos++;
- aByte = stream.GetC();
- if (bpp == 1)
- {
- int bit = 0;
- for (bit = 0; bit < 8; bit++)
- {
- index = ((aByte & (0x80 >> bit)) ? 1 : 0);
- ptr[poffset] = cmap[index].r;
- ptr[poffset + 1] = cmap[index].g;
- ptr[poffset + 2] = cmap[index].b;
- column++;
- }
- }
- else if (bpp == 4)
- {
- if (comp == BI_RLE4)
- {
- wxLogError( _T("Can't deal with 4bit encoded yet.") );
- image->Destroy();
- free(cmap);
- return FALSE;
- }
- else
- {
- int nibble = 0;
- for (nibble = 0; nibble < 2; nibble++)
- {
- index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
- if (index >= 16)
- index = 15;
- ptr[poffset] = cmap[index].r;
- ptr[poffset + 1] = cmap[index].g;
- ptr[poffset + 2] = cmap[index].b;
- column++;
- }
- }
- }
- else if (bpp == 8)
- {
- if (comp == BI_RLE8)
- {
- unsigned char first;
- first = aByte;
- aByte = stream.GetC();
- if (first == 0)
- {
- if (aByte == 0)
- {
- /* column = width; */
- }
- else if (aByte == 1)
- {
- column = width;
- line = -1;
- }
- else if (aByte == 2)
- {
- aByte = stream.GetC();
- column += aByte;
- linepos = column * bpp / 8;
- aByte = stream.GetC();
- line += aByte;
- }
- else
- {
- int absolute = aByte;
- for (int k = 0; k < absolute; k++)
- {
- linepos++;
- aByte = stream.GetC();
- ptr[poffset ] = cmap[aByte].r;
- ptr[poffset + 1] = cmap[aByte].g;
- ptr[poffset + 2] = cmap[aByte].b;
- column++;
- }
- if (absolute & 0x01)
- aByte = stream.GetC();
- }
- }
- else
- {
- for (int l = 0; l < first; l++)
- {
- ptr[poffset ] = cmap[aByte].r;
- ptr[poffset + 1] = cmap[aByte].g;
- ptr[poffset + 2] = cmap[aByte].b;
- column++;
- linepos++;
- }
- }
- }
- else
- {
- ptr[poffset ] = cmap[aByte].r;
- ptr[poffset + 1] = cmap[aByte].g;
- ptr[poffset + 2] = cmap[aByte].b;
- column++;
- linepos += size;
- }
- }
- }
- else if (bpp == 24)
- {
- stream.Read( &bbuf, 3 );
- linepos += 3;
- ptr[poffset ] = (unsigned char)bbuf[2];
- ptr[poffset + 1] = (unsigned char)bbuf[1];
- ptr[poffset + 2] = (unsigned char)bbuf[0];
- column++;
- }
- else if (bpp == 16)
- {
- unsigned char temp;
- stream.Read( &aWord, 2 );
- aWord = wxUINT16_SWAP_FROM_LE( aWord );
- linepos += 2;
- temp = (aWord & rmask) >> rshift;
- ptr[poffset] = temp;
- temp = (aWord & gmask) >> gshift;
- ptr[poffset + 1] = temp;
- temp = (aWord & bmask) >> gshift;
- ptr[poffset + 2] = temp;
- column++;
- }
- else
- {
- unsigned char temp;
- stream.Read( &aDword, 4 );
- aDword = wxINT32_SWAP_FROM_LE( aDword );
- linepos += 4;
- temp = (aDword & rmask) >> rshift;
- ptr[poffset] = temp;
- temp = (aDword & gmask) >> gshift;
- ptr[poffset + 1] = temp;
- temp = (aDword & bmask) >> bshift;
- ptr[poffset + 2] = temp;
- column++;
- }
- }
- while ((linepos < linesize) && (comp != 1) && (comp != 2))
- {
- stream.Read( &aByte, 1 );
- linepos += 1;
- if (stream.LastError() != wxStream_NOERROR)
- break;
- }
- }
- if (cmap)
- free(cmap);