+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ image->comps[0].data[index] = (OPJ_INT32)pSrc[3 * x + 2]; /* R */
+ image->comps[1].data[index] = (OPJ_INT32)pSrc[3 * x + 1]; /* G */
+ image->comps[2].data[index] = (OPJ_INT32)pSrc[3 * x + 0]; /* B */
+ index++;
+ }
+ pSrc -= stride;
+ }
+}
+
+static void bmp_mask_get_shift_and_prec(OPJ_UINT32 mask, OPJ_UINT32* shift,
+ OPJ_UINT32* prec)
+{
+ OPJ_UINT32 l_shift, l_prec;
+
+ l_shift = l_prec = 0U;
+
+ if (mask != 0U) {
+ while ((mask & 1U) == 0U) {
+ mask >>= 1;
+ l_shift++;
+ }
+ while (mask & 1U) {
+ mask >>= 1;
+ l_prec++;
+ }
+ }
+ *shift = l_shift;
+ *prec = l_prec;
+}
+
+static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
+ opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask,
+ OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
+{
+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+ OPJ_BOOL hasAlpha;
+ OPJ_UINT32 redShift, redPrec;
+ OPJ_UINT32 greenShift, greenPrec;
+ OPJ_UINT32 blueShift, bluePrec;
+ OPJ_UINT32 alphaShift, alphaPrec;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ hasAlpha = image->numcomps > 3U;
+
+ bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
+ bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
+ bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
+ bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
+
+ image->comps[0].bpp = redPrec;
+ image->comps[0].prec = redPrec;
+ image->comps[1].bpp = greenPrec;
+ image->comps[1].prec = greenPrec;
+ image->comps[2].bpp = bluePrec;
+ image->comps[2].prec = bluePrec;
+ if (hasAlpha) {
+ image->comps[3].bpp = alphaPrec;
+ image->comps[3].prec = alphaPrec;
+ }
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ OPJ_UINT32 value = 0U;
+
+ value |= ((OPJ_UINT32)pSrc[4 * x + 0]) << 0;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 1]) << 8;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 2]) << 16;
+ value |= ((OPJ_UINT32)pSrc[4 * x + 3]) << 24;
+
+ image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >>
+ redShift); /* R */
+ image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
+ greenShift); /* G */
+ image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >>
+ blueShift); /* B */
+ if (hasAlpha) {
+ image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >>
+ alphaShift); /* A */
+ }
+ index++;
+ }
+ pSrc -= stride;
+ }
+}
+
+static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
+ opj_image_t* image, OPJ_UINT32 redMask, OPJ_UINT32 greenMask,
+ OPJ_UINT32 blueMask, OPJ_UINT32 alphaMask)
+{
+ int index;
+ OPJ_UINT32 width, height;
+ OPJ_UINT32 x, y;
+ const OPJ_UINT8 *pSrc = NULL;
+ OPJ_BOOL hasAlpha;
+ OPJ_UINT32 redShift, redPrec;
+ OPJ_UINT32 greenShift, greenPrec;
+ OPJ_UINT32 blueShift, bluePrec;
+ OPJ_UINT32 alphaShift, alphaPrec;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ hasAlpha = image->numcomps > 3U;
+
+ bmp_mask_get_shift_and_prec(redMask, &redShift, &redPrec);
+ bmp_mask_get_shift_and_prec(greenMask, &greenShift, &greenPrec);
+ bmp_mask_get_shift_and_prec(blueMask, &blueShift, &bluePrec);
+ bmp_mask_get_shift_and_prec(alphaMask, &alphaShift, &alphaPrec);
+
+ image->comps[0].bpp = redPrec;
+ image->comps[0].prec = redPrec;
+ image->comps[1].bpp = greenPrec;
+ image->comps[1].prec = greenPrec;
+ image->comps[2].bpp = bluePrec;
+ image->comps[2].prec = bluePrec;
+ if (hasAlpha) {
+ image->comps[3].bpp = alphaPrec;
+ image->comps[3].prec = alphaPrec;
+ }
+
+ index = 0;
+ pSrc = pData + (height - 1U) * stride;
+ for (y = 0; y < height; y++) {
+ for (x = 0; x < width; x++) {
+ OPJ_UINT32 value = 0U;
+
+ value |= ((OPJ_UINT32)pSrc[2 * x + 0]) << 0;
+ value |= ((OPJ_UINT32)pSrc[2 * x + 1]) << 8;
+
+ image->comps[0].data[index] = (OPJ_INT32)((value & redMask) >>
+ redShift); /* R */
+ image->comps[1].data[index] = (OPJ_INT32)((value & greenMask) >>
+ greenShift); /* G */
+ image->comps[2].data[index] = (OPJ_INT32)((value & blueMask) >>
+ blueShift); /* B */
+ if (hasAlpha) {
+ image->comps[3].data[index] = (OPJ_INT32)((value & alphaMask) >>
+ alphaShift); /* A */
+ }
+ index++;
+ }
+ pSrc -= stride;
+ }
+}
+
+static opj_image_t* bmp8toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride,
+ opj_image_t* image, OPJ_UINT8 const* const* pLUT)
+{
+ OPJ_UINT32 width, height;
+ const OPJ_UINT8 *pSrc = NULL;
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+
+ pSrc = pData + (height - 1U) * stride;
+ if (image->numcomps == 1U) {
+ opj_applyLUT8u_8u32s_C1R(pSrc, -(OPJ_INT32)stride, image->comps[0].data,
+ (OPJ_INT32)width, pLUT[0], width, height);
+ } else {
+ OPJ_INT32* pDst[3];
+ OPJ_INT32 pDstStride[3];
+
+ pDst[0] = image->comps[0].data;
+ pDst[1] = image->comps[1].data;
+ pDst[2] = image->comps[2].data;
+ pDstStride[0] = (OPJ_INT32)width;
+ pDstStride[1] = (OPJ_INT32)width;
+ pDstStride[2] = (OPJ_INT32)width;
+ opj_applyLUT8u_8u32s_C1P3R(pSrc, -(OPJ_INT32)stride, pDst, pDstStride, pLUT,
+ width, height);
+ }
+ return image;
+}
+
+static OPJ_BOOL bmp_read_file_header(FILE* IN, OPJ_BITMAPFILEHEADER* header)
+{
+ header->bfType = (OPJ_UINT16)getc(IN);
+ header->bfType |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ if (header->bfType != 19778) {
+ fprintf(stderr, "Error, not a BMP file!\n");
+ return OPJ_FALSE;
+ }
+
+ /* FILE HEADER */
+ /* ------------- */
+ header->bfSize = (OPJ_UINT32)getc(IN);
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->bfSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->bfReserved1 = (OPJ_UINT16)getc(IN);
+ header->bfReserved1 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->bfReserved2 = (OPJ_UINT16)getc(IN);
+ header->bfReserved2 |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->bfOffBits = (OPJ_UINT32)getc(IN);
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 8;
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 16;
+ header->bfOffBits |= (OPJ_UINT32)getc(IN) << 24;
+ return OPJ_TRUE;
+}
+static OPJ_BOOL bmp_read_info_header(FILE* IN, OPJ_BITMAPINFOHEADER* header)
+{
+ memset(header, 0, sizeof(*header));
+ /* INFO HEADER */
+ /* ------------- */
+ header->biSize = (OPJ_UINT32)getc(IN);
+ header->biSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->biSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->biSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ switch (header->biSize) {
+ case 12U: /* BITMAPCOREHEADER */
+ case 40U: /* BITMAPINFOHEADER */
+ case 52U: /* BITMAPV2INFOHEADER */
+ case 56U: /* BITMAPV3INFOHEADER */
+ case 108U: /* BITMAPV4HEADER */
+ case 124U: /* BITMAPV5HEADER */
+ break;
+ default:
+ fprintf(stderr, "Error, unknown BMP header size %d\n", header->biSize);
+ return OPJ_FALSE;
+ }
+
+ header->biWidth = (OPJ_UINT32)getc(IN);
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 8;
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 16;
+ header->biWidth |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biHeight = (OPJ_UINT32)getc(IN);
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 8;
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 16;
+ header->biHeight |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biPlanes = (OPJ_UINT16)getc(IN);
+ header->biPlanes |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ header->biBitCount = (OPJ_UINT16)getc(IN);
+ header->biBitCount |= (OPJ_UINT16)((OPJ_UINT32)getc(IN) << 8);
+
+ if (header->biSize >= 40U) {
+ header->biCompression = (OPJ_UINT32)getc(IN);
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 8;
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 16;
+ header->biCompression |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biSizeImage = (OPJ_UINT32)getc(IN);
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 8;
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 16;
+ header->biSizeImage |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biXpelsPerMeter = (OPJ_UINT32)getc(IN);
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
+ header->biXpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biYpelsPerMeter = (OPJ_UINT32)getc(IN);
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 8;
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 16;
+ header->biYpelsPerMeter |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biClrUsed = (OPJ_UINT32)getc(IN);
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 8;
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 16;
+ header->biClrUsed |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biClrImportant = (OPJ_UINT32)getc(IN);
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 8;
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 16;
+ header->biClrImportant |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 56U) {
+ header->biRedMask = (OPJ_UINT32)getc(IN);
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biRedMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biGreenMask = (OPJ_UINT32)getc(IN);
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biGreenMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biBlueMask = (OPJ_UINT32)getc(IN);
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biBlueMask |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biAlphaMask = (OPJ_UINT32)getc(IN);
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 8;
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 16;
+ header->biAlphaMask |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 108U) {
+ header->biColorSpaceType = (OPJ_UINT32)getc(IN);
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 8;
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 16;
+ header->biColorSpaceType |= (OPJ_UINT32)getc(IN) << 24;
+
+ if (fread(&(header->biColorSpaceEP), 1U, sizeof(header->biColorSpaceEP),
+ IN) != sizeof(header->biColorSpaceEP)) {
+ fprintf(stderr, "Error, can't read BMP header\n");
+ return OPJ_FALSE;
+ }
+
+ header->biRedGamma = (OPJ_UINT32)getc(IN);
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biRedGamma |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biGreenGamma = (OPJ_UINT32)getc(IN);
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biGreenGamma |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biBlueGamma = (OPJ_UINT32)getc(IN);
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 8;
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 16;
+ header->biBlueGamma |= (OPJ_UINT32)getc(IN) << 24;
+ }
+
+ if (header->biSize >= 124U) {
+ header->biIntent = (OPJ_UINT32)getc(IN);
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIntent |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biIccProfileData = (OPJ_UINT32)getc(IN);
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIccProfileData |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biIccProfileSize = (OPJ_UINT32)getc(IN);
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 8;
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 16;
+ header->biIccProfileSize |= (OPJ_UINT32)getc(IN) << 24;
+
+ header->biReserved = (OPJ_UINT32)getc(IN);
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 8;
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 16;
+ header->biReserved |= (OPJ_UINT32)getc(IN) << 24;
+ }
+ return OPJ_TRUE;