Esempio n. 1
0
void PLFilterGetAlpha::Apply(PLBmpBase * pBmpSource, PLBmp * pBmpDest) const
{
  // Only works for 32 bpp bitmaps.
  PLASSERT (pBmpSource->GetBitsPerPixel() == 32);

  PLASSERT (pBmpSource->HasAlpha());

  pBmpDest->Create (pBmpSource->GetWidth(), pBmpSource->GetHeight(), 
                    PLPixelFormat::L8, NULL, 0, 
                    pBmpSource->GetResolution());
  PLPixel32 ** pSrcLines = pBmpSource->GetLineArray32();
  PLBYTE ** pDstLines = pBmpDest->GetLineArray();

  for (int y = 0; y<pBmpDest->GetHeight(); ++y)
  { // For each line
    PLPixel32 * pSrcPixel = pSrcLines[y];
    PLBYTE * pDstPixel = pDstLines[y];

    for (int x = 0; x < pBmpDest->GetWidth(); ++x)
    { // For each pixel
      *pDstPixel = pSrcPixel->GetA();
      ++pSrcPixel;
      ++pDstPixel;
    }
  }
}
Esempio n. 2
0
void
HeightToNormalMap::Apply(PLBmpBase * theSource, PLBmp * theDestination) const {
  theDestination->Create(theSource->GetWidth(), theSource->GetHeight(), PLPixelFormat::A8R8G8B8);
    //theDestination->SetHasAlpha(true);
    PLPixel8 ** mySourceLines = (PLPixel8**)theSource->GetLineArray();
    PLPixel32 ** myDestinationLines = (PLPixel32**)theDestination->GetLineArray();

    for (int y = 0; y < theSource->GetHeight(); ++y) {
        PLPixel8  * mySourceLine = mySourceLines[y];
        PLPixel32 * myDestinationLine = myDestinationLines[y];

        PLPixel8 * myNextLine;
        if (y + 1 == theSource->GetHeight()) {
            myNextLine = mySourceLines[y];
        } else {
            myNextLine = mySourceLines[y + 1];
        }
        for (int x = 0; x < theSource->GetWidth(); ++x) {
            float myHeight = mySourceLine->Get();
            float aboveNeighbourHeight = myNextLine->Get();
            float rightNeighbourHeight;
            if (x + 1 == theSource->GetWidth()) {
                rightNeighbourHeight = mySourceLine->Get();
            } else {
                rightNeighbourHeight = (mySourceLine + 1)->Get();
            }
            myHeight /= 255;
            aboveNeighbourHeight /= 255;
            rightNeighbourHeight /= 255;
            float myZ = 1.0f / float((sqrt(pow(myHeight - aboveNeighbourHeight, 2) +
                                    pow(myHeight - rightNeighbourHeight, 2) + 1)));
            float myX = (myHeight - rightNeighbourHeight) * myZ;
            float myY = (myHeight - aboveNeighbourHeight) * myZ;

            myDestinationLine->SetR((PLBYTE)minimum(128.0 * myX + 128, 255.0));
            myDestinationLine->SetG((PLBYTE)minimum(128.0 * -myY + 128, 255.0));
            myDestinationLine->SetB((PLBYTE)minimum(128.0 * myZ + 128, 255.0));
            myDestinationLine->SetA(255);

            mySourceLine++;
            myDestinationLine++;
            myNextLine++;
        }
    }
}
Esempio n. 3
0
void CPalViewDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
  int x,y;
  CRect Rect;
  CBrush Brush;
  for (x = 0; x < 16; x++)
    for (y = 0; y < 16; y++)
    {
      Rect = CRect (x*12, y*13, x*12+11, y*13+12);
      Rect.OffsetRect (14, 14);
      PLPixel32 Col = m_pPal[x*8+y];
      Brush.CreateSolidBrush (RGB (Col.GetR(), Col.GetG(), Col.GetB ()));
      dc.FillRect (Rect, &Brush);
      Brush.DeleteObject();
    }
}
Esempio n. 4
0
PLPixel32 PLPPMDecoder::readASCIIPixel32( int MaxSampleValue, PLDataSource *pDataSrc) {
  skipPpmASCIISeparators(pDataSrc);
  PLBYTE r = readASCIIDecimal(pDataSrc);

  skipPpmASCIISeparators(pDataSrc);
  PLBYTE g = readASCIIDecimal(pDataSrc);

  skipPpmASCIISeparators(pDataSrc);
  PLBYTE b = readASCIIDecimal(pDataSrc);

  if(MaxSampleValue != 255) {
    r = (r * 255) / MaxSampleValue;
    g = (g * 255) / MaxSampleValue;
    b = (b * 255) / MaxSampleValue;
  }

  PLPixel32 Dest;
  Dest.Set(r, g, b, 0);

  return Dest;
}
Esempio n. 5
0
void multAndStore(PLPixel32 & theResult, PLPixel32 * theSource, int theScale ) {
    theResult.SetR( static_cast<PLBYTE>(minimum(theResult.GetR() + (((theSource->GetR() * theScale)+128) / 256), 255)) );
    theResult.SetG( static_cast<PLBYTE>(minimum(theResult.GetG() + (((theSource->GetG() * theScale)+128) / 256), 255)) );
    theResult.SetB( static_cast<PLBYTE>(minimum(theResult.GetB() + (((theSource->GetB() * theScale)+128) / 256), 255)) );
    theResult.SetA( static_cast<PLBYTE>(minimum(theResult.GetA() + (((theSource->GetA() * theScale)+128) / 256), 255)) );
}
Esempio n. 6
0
void clearPixel(PLPixel32& theResult ) { theResult.Set(0,0,0,0); }
Esempio n. 7
0
void PLTIFFDecoder::doHiColor (TIFF * tif, PLBmpBase * pBmp, uint16 SamplePerPixel)
{
  int ok;
  PLULONG x, y;

  TIFFRGBAImage img;
  char emsg[1024];
  PLBYTE * pBits;

  ok = TIFFRGBAImageBegin(&img, tif, 0, emsg);

  if (ok == 0)
  {
    raiseError (PL_ERRWRONG_SIGNATURE, "TIFF subformat not supported.");
  }

  //bool bHasAlpha = pBmp->HasAlpha();
  PLASSERT (int(img.width) == pBmp->GetWidth());
  PLASSERT (int(img.height) == pBmp->GetHeight());
  PLASSERT (pBmp->GetBitsPerPixel() == 32);
  pBits = new PLBYTE [img.width*img.height*4];
  if (pBits == NULL)
    raiseError (PL_ERRNO_MEMORY, "Out of memory allocating TIFF buffer.");

  // Hack for photoshop alpha channel support
  if (SamplePerPixel == 4 && img.bitspersample == 8 && img.photometric == 2)
  {
    img.put.contig = putRGBAAcontig8bittile;
  }

  ok = TIFFRGBAImageGet(&img, (uint32 *) pBits, img.width, img.height);
  if (!ok)
  {
    TIFFRGBAImageEnd(&img);
    raiseError (PL_ERRWRONG_SIGNATURE, m_szLastErr);
  }

  PLPixel32 ** pLineArray = pBmp->GetLineArray32();

  // Correct the byte ordering. This could be replaced by appropriate 
  // putRGBAcontig... routines.
  for (y=0; y<img.height; y++)
  {
    PLBYTE * pSrc = pBits+(img.height-y-1)*img.width*4;
    PLPixel32 * pPixel = pLineArray[y];
    for  (x=0; x<img.width; x++)
    {
#ifdef WORDS_BIGENDIAN
      pPixel->Set (*(pSrc+3), *(pSrc+2), *(pSrc+1), *(pSrc));
#else
      pPixel->Set (*pSrc, *(pSrc+1), *(pSrc+2), *(pSrc+3));
#endif    
      pPixel++;
      pSrc += 4;
    }
  }

  // Clean up.
  delete [] pBits;
  TIFFRGBAImageEnd(&img);
}
Esempio n. 8
0
void PLTIFFEncoder::DoTiffEncode (PLBmpBase* pBmp, TIFF* tif)
{
  uint32 l, c, image_length, image_width;
  // iterate over data
  PLBYTE **pla = pBmp->GetLineArray();
  PLASSERT( pla );

  image_length = (uint32) pBmp->GetHeight();
  image_width  = (uint32) pBmp->GetWidth();
  switch (pBmp->GetBitsPerPixel())
  {
    case 8:
      {
        // first, save the colormap
        uint16 red[256];
        uint16 green[256];
        uint16 blue[256];

        PLPixel32 * pPal = pBmp->GetPalette();
        PLASSERT( pPal );
        for (int i = 0; i < pBmp->GetNumColors(); i++, pPal++)
        {
          red[i]   = pPal->GetR ();
          green[i] = pPal->GetG ();
          blue[i]  = pPal->GetB ();
        }
        SetField( tif, TIFFTAG_COLORMAP, red, green, blue );
      }
      // fall-through

    case 1:  // TODO: a bit of error checking
      for (l = 0; l < image_length; l++)
        if(TIFFWriteScanline( tif, pla[l], l, 0 ) < 1) {
            throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed");
        }
      break;

    case 24:
      {
        // TODO: check whether (r,g,b) components come in the correct order here...
        PLBYTE* pBuf = new PLBYTE[3*image_width];
        for (l = 0; l < image_length; l++)
        {
          for (c = 0; c < image_width; c++)
          {
            pBuf[c*3 + 0] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_RED];
            pBuf[c*3 + 1] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_GREEN];
            pBuf[c*3 + 2] = pla[l][c*sizeof(PLPixel24) + PL_RGBA_BLUE];
          }
          if(TIFFWriteScanline( tif, pBuf, l, 0 ) < 1) {
              throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed");
          }
        }
        delete [] pBuf;
      }
      break;
    case 32:
      {
        // TODO: check whether (r,g,b) components come in the correct order here...
        if (pBmp->HasAlpha())
        {
          uint32 *plBuf = new uint32[image_width];
          for (l = 0; l < image_length; l++)
          {
            for (c = 0; c < image_width; c++)
            {
                // For 32 bit, TIFFLIB wants abgr long word packed...
                plBuf[c] = 
                    ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_ALPHA]) << 24) |
                    ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_BLUE])  << 16) |
                    ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_GREEN]) << 8 ) |
                    ((uint32)(pla[l][c*sizeof(PLPixel32) + PL_RGBA_RED]));
            }
            if(TIFFWriteScanline( tif, plBuf, l, 0 ) < 1) {
                throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed");
            }
          }
          delete [] plBuf;
        }
        else
        {
          PLBYTE* pBuf = new PLBYTE[3*image_width];
          for (l = 0; l < image_length; l++)
          {
            for (c = 0; c < image_width; c++)
            {
              pBuf[c*3 + 0] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_RED];
              pBuf[c*3 + 1] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_GREEN];
              pBuf[c*3 + 2] = pla[l][c*sizeof(PLPixel32) + PL_RGBA_BLUE];
            }
            if(TIFFWriteScanline( tif, pBuf, l, 0 ) < 1) {
                throw PLTextException(PL_ERRINTERNAL, "TIFFWriteScanline failed");
            }
          }
          delete [] pBuf;
        }
      }
      break;

    default:
      PLASSERT(false);
      throw PLTextException(PL_ERRFORMAT_NOT_SUPPORTED, "unsupported bitsPerPixel");
  }
  // we could flush at this point, but TIFFClose will do it anyway
}
Esempio n. 9
0
void PLFilterSubtract::Apply(PLBmpBase * pBmpSource, PLBmp * pBmpDest) const
{
  // Calculate the size of the new bitmap
  pBmpDest->Create (pBmpSource->GetWidth(), pBmpSource->GetHeight(), pBmpSource->GetPixelFormat(),
                    NULL, 0, pBmpSource->GetResolution());
  for (int y = 0; y < pBmpSource->GetHeight(); ++y)
  {
    for (int x = 0; x < pBmpSource->GetWidth(); ++x)
    {
      PLPixel32 srcPixel = pBmpSource->GetPixel32(x,y);
      PLPixel32 differencePixel;

      if (x < m_pBmpSubtrahend->GetWidth() &&
          y < m_pBmpSubtrahend->GetHeight())
      {
        PLPixel32 substrahendPixel = m_pBmpSubtrahend->GetPixel32(x,y);
        differencePixel.SetR( static_cast<PLBYTE>(abs(srcPixel.GetR() - substrahendPixel.GetR())) );
        differencePixel.SetG( static_cast<PLBYTE>(abs(srcPixel.GetG() - substrahendPixel.GetG())) );
        differencePixel.SetB( static_cast<PLBYTE>(abs(srcPixel.GetB() - substrahendPixel.GetB())) );
        differencePixel.SetA( static_cast<PLBYTE>(abs(srcPixel.GetA() - substrahendPixel.GetA())) );
      } else {
        differencePixel = srcPixel;
      }
      pBmpDest->SetPixel(x,y,differencePixel);
    }
  }
}