PDL-IO-Image
view release on metacpan or search on metacpan
int cu = FreeImage_GetColorsUsed(self->dib);
if (cu==0) XSRETURN_UNDEF;
PDL_Byte *pdata;
int i;
dims[0] = 3;
dims[1] = cu;
pal_pdl = PDL->pdlnew();
pal_pdl->datatype = PDL_B;
PDL->setdims (pal_pdl, dims, 2);
PDL->allocdata (pal_pdl);
pdata = (PDL_Byte *) pal_pdl->data;
RGBQUAD *pal = FreeImage_GetPalette(self->dib);
for (i = 0; i < cu; i++) {
pdata[i*3] = (PDL_Byte) pal[i].rgbRed;
pdata[i*3 + 1] = (PDL_Byte) pal[i].rgbGreen;
pdata[i*3 + 2] = (PDL_Byte) pal[i].rgbBlue;
}
RETVAL = pal_pdl;
}
OUTPUT:
switch (it) {
case FIT_BITMAP: /* Standard image: 1-, 4-, 8-, 16-, 24-, 32-bit */
if (bpp==32) {
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 4;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
RGBQUAD *bits = (RGBQUAD*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Byte) bits[x+x1].rgbRed;
pdata[y*wp + x + wxh] = (PDL_Byte) bits[x+x1].rgbGreen;
pdata[y*wp + x + 2*wxh] = (PDL_Byte) bits[x+x1].rgbBlue;
pdata[y*wp + x + 3*wxh] = (PDL_Byte) bits[x+x1].rgbReserved;
}
}
}
else if (bpp==24) {
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 3;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
RGBTRIPLE *bits = (RGBTRIPLE*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Byte) bits[x+x1].rgbtRed;
pdata[y*wp + x + wxh] = (PDL_Byte) bits[x+x1].rgbtGreen;
pdata[y*wp + x + 2*wxh] = (PDL_Byte) bits[x+x1].rgbtBlue;
}
}
}
else if (bpp==16) {
unsigned red_mask, green_mask, blue_mask;
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 3;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
red_mask = FreeImage_GetRedMask(self->dib);
green_mask = FreeImage_GetGreenMask(self->dib);
blue_mask = FreeImage_GetBlueMask(self->dib);
if ( (red_mask == FI16_565_RED_MASK) && (green_mask == FI16_565_GREEN_MASK) && (blue_mask == FI16_565_BLUE_MASK) ) {
for(y = 0; y < hp; y++) {
WORD *bits = (WORD*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
PDL_Byte r5 = (PDL_Byte)((bits[x+x1] & FI16_565_RED_MASK) >> FI16_565_RED_SHIFT) ;
PDL_Byte g6 = (PDL_Byte)((bits[x+x1] & FI16_565_GREEN_MASK) >> FI16_565_GREEN_SHIFT);
}
}
}
else if (bpp==8) {
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
BYTE *bits = FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Byte) bits[x+x1];
}
}
}
else if (bpp==4) {
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
BYTE *bits = FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Byte) ((bits[(x+x1)/2] >> 4*(1-(x+x1)%2)) & 0x0F);
}
}
}
else if (bpp==1) {
PDL_Byte *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_B;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Byte *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
BYTE *bits = FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Byte) ((bits[(x+x1)/8] >> (7-(x+x1)%8)) & 0x01);
}
}
}
else {
warn("FAIL: unknown bits per pixel '%d'", bpp);
}
break;
case FIT_UINT16: /* Array of unsigned short: unsigned 16-bit */
{
PDL_Ushort *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_US;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Ushort *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
PDL_Ushort *bits = (PDL_Ushort*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = bits[x+x1];
}
}
}
break;
case FIT_INT16: /* Array of short: signed 16-bit */
{
PDL_Short *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_S;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Short *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
PDL_Short *bits = (PDL_Short*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = bits[x+x1];
}
}
}
break;
case FIT_UINT32: /* Array of unsigned long: unsigned 32-bit */
{ /* XXX hack: using INT64 for UINT32 */
PDL_LongLong *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_LL;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_LongLong *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
DWORD *bits = (DWORD*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_LongLong)bits[x+x1];
}
}
}
break;
case FIT_INT32: /* Array of long: signed 32-bit */
{
PDL_Long *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_L;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Long *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
PDL_Long *bits = (PDL_Long*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = bits[x+x1];
}
}
}
break;
case FIT_FLOAT: /* Array of float: 32-bit IEEE floating point */
{
PDL_Float *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_F;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Float *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
PDL_Float *bits = (PDL_Float*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = bits[x+x1];
}
}
}
break;
case FIT_DOUBLE: /* Array of double: 64-bit IEEE floating point */
{
PDL_Double *pdata;
dims[0] = wp;
dims[1] = hp;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_D;
PDL->setdims (bmp_pdl, dims, 2);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Double *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
PDL_Double *bits = (PDL_Double*)FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = bits[x+x1];
}
}
}
break;
case FIT_COMPLEX: /* Array of FICOMPLEX: 2 x 64-bit IEEE floating point */
break;
case FIT_RGB16: /* 48-bit RGB image: 3 x 16-bit */
{
PDL_Ushort *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 3;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_US;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Ushort *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
FIRGB16 *bits = (FIRGB16*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Ushort) bits[x+x1].red;
pdata[y*wp + x + wxh] = (PDL_Ushort) bits[x+x1].green;
pdata[y*wp + x + 2*wxh] = (PDL_Ushort) bits[x+x1].blue;
}
}
}
break;
case FIT_RGBA16: /* 64-bit RGBA image: 4 x 16-bit */
{
PDL_Ushort *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 4;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_US;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Ushort *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
FIRGBA16 *bits = (FIRGBA16*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Ushort) bits[x+x1].red;
pdata[y*wp + x + wxh] = (PDL_Ushort) bits[x+x1].green;
pdata[y*wp + x + 2*wxh] = (PDL_Ushort) bits[x+x1].blue;
pdata[y*wp + x + 3*wxh] = (PDL_Ushort) bits[x+x1].alpha;
}
}
break;
case FIT_RGBF: /* 96-bit RGB float image: 3 x 32-bit IEEE floating point */
{
PDL_Float *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 3;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_F;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Float *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
FIRGBF *bits = (FIRGBF*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Float) bits[x+x1].red;
pdata[y*wp + x + wxh] = (PDL_Float) bits[x+x1].green;
pdata[y*wp + x + 2*wxh] = (PDL_Float) bits[x+x1].blue;
}
}
}
break;
case FIT_RGBAF: /* 128-bit RGBA float image: 4 x 32-bit IEEE floating point */
{
PDL_Float *pdata;
dims[0] = wp;
dims[1] = hp;
dims[2] = 4;
bmp_pdl = PDL->pdlnew();
bmp_pdl->datatype = PDL_F;
PDL->setdims (bmp_pdl, dims, 3);
PDL->allocdata (bmp_pdl);
pdata = (PDL_Float *) bmp_pdl->data;
for(y = 0; y < hp; y++) {
FIRGBAF *bits = (FIRGBAF*) FreeImage_GetScanLine(self->dib, h-y-1-y1);
for(x = 0; x < wp; x++) {
pdata[y*wp + x] = (PDL_Float) bits[x+x1].red;
pdata[y*wp + x + wxh] = (PDL_Float) bits[x+x1].green;
pdata[y*wp + x + 2*wxh] = (PDL_Float) bits[x+x1].blue;
pdata[y*wp + x + 3*wxh] = (PDL_Float) bits[x+x1].alpha;
}
}
( run in 0.690 second using v1.01-cache-2.11-cpan-454fe037f31 )