Hello,

I have an image of a signature with height=50 and width=135 that I would
like to insert into a PDF.  I would like to mask it with the signature
itself so that the white background of the image is gone and only the
signature remains.  I've tried using the code below to do this, but I get
the following exception.  Am I constructing the mask correctly?  I assumed
that the number of bytes in the mask should be equal to the ceiling of the
number of pixels divided by 8(844 in this case).  Is this correct?

java.lang.ArrayIndexOutOfBoundsException: 844
        at com.lowagie.text.pdf.CCITTG4Encoder.nextState(Unknown Source)
        at com.lowagie.text.pdf.CCITTG4Encoder.encodeT6(Unknown Source)
        at com.lowagie.text.pdf.CCITTG4Encoder.compress(Unknown Source)
        at com.lowagie.text.Image.getInstance(Unknown Source)
        at com.lowagie.text.Image.getInstance(Unknown Source)


Here is the code:
     <snip>
         PlanarImage planarImage = getPlanarImage(value);
         byte[] mask = createMask(planarImage);
         Image maskImage =
            Image.getInstance(
               planarImage.getData().getWidth(),
               planarImage.getData().getHeight(),
               1,
               1,
               mask);

         maskImage.makeMask();
         maskImage.setInvertMask(true);

         image.setAbsolutePosition(pageTokenDefinition.getX(),
pageTokenDefinition.getY());
         image.setImageMask(maskImage);
         pdfTemplate.addImage(image);
     </snip>

   private static byte[] createMask(PlanarImage image) {
      double[][] pixels = imageToBandArray(image, 0);
      int width = pixels[0].length;
      int height = pixels.length;
      byte[] mask = new byte[(int)Math.ceil(width * height / 8.0)];
      int maskIndex = 0;
      for (int pixelIndex = 0; pixelIndex < width * height; pixelIndex++) {
         if (pixels[pixelIndex / width][pixelIndex % width] == 0) {
            mask[(int)Math.floor(pixelIndex / 8.0)] |= (1 << (7 -
pixelIndex % 8));
         }
      }
      return mask;
   }

   public static double[][] imageToBandArray(PlanarImage image, int band) {
      Raster raster = image.getData();
      int width = raster.getWidth();
      int height = raster.getHeight();
      int bands = raster.getNumBands();
      double[][] bandArray = new double[height][width];
      for (int n = 0; n < height; n++) {
         raster.getSamples(0, n, width, 1, band, bandArray[n]);
      }
      return bandArray;
   }

Thanks,
Chris



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to