Packages

 

com.aspose.imaging.fileformats.tiff

Class TiffFrame

  • All Implemented Interfaces:
    IObjectWithBounds, IRasterImageArgb32PixelLoader, IRasterImageRawDataLoader, com.aspose.imaging_internal.IPixelsSaver, com.aspose.imaging_internal.progressmanagement.IProgressEventHandler, com.aspose.imaging_internal.progressmanagement.IProgressInformer, com.aspose.ms.System.IDisposable, Closeable, AutoCloseable


    public final class TiffFrame
    extends RasterCachedImage

    The tiff frame.

    Code example:

    This example shows how to create a TIFF image from scratch and save it to a file.


    String dir = "c:\\temp\\";
    
    com.aspose.imaging.imageoptions.TiffOptions createOptions =
            new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
    
    // Set 8 bits for each color component.
    createOptions.setBitsPerSample(new int[]{8, 8, 8});
    
    // Set the Big Endian byte order (Motorola)
    createOptions.setByteOrder(com.aspose.imaging.fileformats.tiff.enums.TiffByteOrder.BigEndian);
    
    // Set the LZW compression.
    createOptions.setCompression(com.aspose.imaging.fileformats.tiff.enums.TiffCompressions.Lzw);
    
    // Set the RGB color model.
    createOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
    
    // All color components will be stored within a single plane.
    createOptions.setPlanarConfiguration(com.aspose.imaging.fileformats.tiff.enums.TiffPlanarConfigs.Contiguous);
    
    // Create a TIFF Frame of 100x100 px.
    // Note that you don't have to dispose a frame explicitly if it is included into TiffImage.
    // When the container is disposed all frames will be disposed automatically.
    com.aspose.imaging.fileformats.tiff.TiffFrame firstFrame = new com.aspose.imaging.fileformats.tiff.TiffFrame(createOptions, 100, 100);
    
    // Fill the entire frame with the blue-yellow gradient.
    com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
            new com.aspose.imaging.Point(0, 0),
            new com.aspose.imaging.Point(firstFrame.getWidth(), firstFrame.getHeight()),
            com.aspose.imaging.Color.getBlue(),
            com.aspose.imaging.Color.getYellow());
    
    com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(firstFrame);
    graphics.fillRectangle(gradientBrush, firstFrame.getBounds());
    
    // Create a TIFF image.
    com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = new com.aspose.imaging.fileformats.tiff.TiffImage(firstFrame);
    try {
        tiffImage.save(dir + "output.tif");
    } finally {
        tiffImage.dispose();
    }
    

    • Constructor Detail

      • TiffFrame

        public TiffFrame(InputStream stream)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        stream - The stream to load an image from and initialize frame pixel and palette data with.
      • TiffFrame

        public TiffFrame(InputStream stream,
                         TiffOptions options)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        stream - The stream to load an image from and initialize frame pixel and palette data with.
        options - The options to use for the newly created frame.
      • TiffFrame

        public TiffFrame(String path)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        path - The path to load an image from and initialize frame pixel and palette data with.
      • TiffFrame

        public TiffFrame(String path,
                         TiffOptions options)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        path - The path to load an image from and initialize frame pixel and palette data with.
        options - The options to use for the newly created frame.
      • TiffFrame

        public TiffFrame(RasterImage image)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        image - The image to initialize frame pixel and palette data with.
        Code example:

        The following example shows how to compose a mutlipage TIFF from individual raster images.


        
        com.aspose.imaging.imageoptions.TiffOptions createTiffOptions
                = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
        createTiffOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("c:\\temp\\multipage.tif", false));
        createTiffOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
        createTiffOptions.setBitsPerSample(new int[]{8, 8, 8});
        
        com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) com.aspose.imaging.Image.create(createTiffOptions, 100, 100);
        try {
            // This is Font and Brush for drawing text on individual frames.
            com.aspose.imaging.Font font = new com.aspose.imaging.Font("Arial", 64);
            com.aspose.imaging.brushes.SolidBrush brush = new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite());
        
            // Create 5 frames
            for (int i = 1; i <= 5; i++) {
                com.aspose.imaging.imageoptions.PngOptions createPngOptions = new com.aspose.imaging.imageoptions.PngOptions();
                createPngOptions.setSource(new com.aspose.imaging.sources.StreamSource(new java.io.ByteArrayInputStream(new byte[0])));
        
                // Create a PNG image and draw the number of page on it.
                com.aspose.imaging.fileformats.png.PngImage pngImage = (com.aspose.imaging.fileformats.png.PngImage) com.aspose.imaging.Image.create(createPngOptions, 100, 100);
                com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(pngImage);
                gr.drawString(Integer.toString(i), font, brush, 10, 10);
        
                // Create a frame based on the PNG image.
                com.aspose.imaging.fileformats.tiff.TiffFrame frame = new com.aspose.imaging.fileformats.tiff.TiffFrame(pngImage);
        
                // Add the frame to the TIFF image.
                tiffImage.addFrame(frame);
            }
        
            // The image was created with a single default frame. Let's remove it.
            com.aspose.imaging.fileformats.tiff.TiffFrame activeFrame = tiffImage.getActiveFrame();
            tiffImage.setActiveFrame(tiffImage.getFrames()[1]);
            tiffImage.removeFrame(0);
        
            // Don't forget to dispose the frame if you won't add it to some other TiffImage
            activeFrame.dispose();
        
            tiffImage.save();
        } finally {
            tiffImage.dispose();
        }
        

      • TiffFrame

        public TiffFrame(RasterImage image,
                         TiffOptions options)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        image - The image to initialize frame pixel and palette data with.
        options - The options to use for the newly created frame.
      • TiffFrame

        public TiffFrame(TiffOptions options,
                         int width,
                         int height)

        Initializes a new instance of the TiffFrame class.

        Parameters:
        options - The frame options.
        width - The width.
        height - The height.
        Throws:
        com.aspose.ms.System.ArgumentNullException - Options parameter is null.
        Code example:

        This example shows how to create a TIFF image with 2 frames and save it to a file.


        String dir = "c:\\temp\\";
        
        // Options for the first frame
        com.aspose.imaging.imageoptions.TiffOptions createOptions1 =
                new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
        
        // Set 8 bits for each color component.
        createOptions1.setBitsPerSample(new int[]{8, 8, 8});
        
        // Set the Big Endian byte order (Motorola)
        createOptions1.setByteOrder(com.aspose.imaging.fileformats.tiff.enums.TiffByteOrder.BigEndian);
        
        // Set the LZW compression.
        createOptions1.setCompression(com.aspose.imaging.fileformats.tiff.enums.TiffCompressions.Lzw);
        
        // Set the RGB color model.
        createOptions1.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
        
        // All color components will be stored within a single plane.
        createOptions1.setPlanarConfiguration(com.aspose.imaging.fileformats.tiff.enums.TiffPlanarConfigs.Contiguous);
        
        // Create the first TIFF frame of 100x100 px.
        // Note that you don't have to dispose frames explicitly if they are included into TiffImage.
        // When the container is disposed all frames will be disposed automatically.
        com.aspose.imaging.fileformats.tiff.TiffFrame frame1 = new com.aspose.imaging.fileformats.tiff.TiffFrame(createOptions1, 100, 100);
        
        // Fill the first frame with the blue-yellow gradient.
        com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
                new com.aspose.imaging.Point(0, 0),
                new com.aspose.imaging.Point(frame1.getWidth(), frame1.getHeight()),
                com.aspose.imaging.Color.getBlue(),
                com.aspose.imaging.Color.getYellow());
        
        com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(frame1);
        graphics.fillRectangle(gradientBrush, frame1.getBounds());
        
        // Options for the first frame
        com.aspose.imaging.imageoptions.TiffOptions createOptions2
                = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
        
        // Set 1 bit per pixel for a B/W image.
        createOptions2.setBitsPerSample(new int[]{1});
        
        // Set the Little Endian byte order (Intel)
        createOptions2.setByteOrder(com.aspose.imaging.fileformats.tiff.enums.TiffByteOrder.LittleEndian);
        
        // Set the CCITT Group 3 Fax compression.
        createOptions2.setCompression(com.aspose.imaging.fileformats.tiff.enums.TiffCompressions.CcittFax3);
        
        // Set the B/W color model where 0 is black, 1 is white.
        createOptions2.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.MinIsBlack);
        
        // Create the second TIFF frame of 200x200px.
        com.aspose.imaging.fileformats.tiff.TiffFrame frame2 = new com.aspose.imaging.fileformats.tiff.TiffFrame(createOptions2, 200, 200);
        
        // Fill the second frame with the blue-yellow gradient.
        // It will be automatically converted to the B/W format due to the corresponding settings of the frame.
        com.aspose.imaging.Graphics graphics2 = new com.aspose.imaging.Graphics(frame2);
        graphics2.fillRectangle(gradientBrush, frame2.getBounds());
        
        // Create a TIFF image.
        com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = new com.aspose.imaging.fileformats.tiff.TiffImage(
                new com.aspose.imaging.fileformats.tiff.TiffFrame[]{frame1, frame2});
        try {
            tiffImage.save(dir + "output.mutliframe.tif");
        } finally {
            tiffImage.dispose();
        }
        

    • Method Detail

      • getBackgroundColor

        public Color getBackgroundColor()

        Gets a value for the background color.

        Overrides:
        getBackgroundColor in class Image
      • setBackgroundColor

        public void setBackgroundColor(Color value)

        Sets a value for the background color.

        Overrides:
        setBackgroundColor in class Image
      • hasAlpha

        public boolean hasAlpha()

        Gets a value indicating whether this instance has alpha.

        Overrides:
        hasAlpha in class RasterImage
        Returns:
        true if this instance has alpha; otherwise, false.
        Code example:

        The following example loads a TIFF image and prints information about raw data format and alpha channel.


        String dir = "c:\\temp\\";
        
        String fileName = dir + "sample.tif";
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName);
        try {
            com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
        
            // If the active TIFF frame has alpha channel, then the entire TIFF image is considered to have alpha channel.
            System.out.printf("ImageFile=%s, FileFormat=%s, HasAlpha=%s\r\n", fileName, tiffImage.getRawDataFormat(), tiffImage.hasAlpha());
        
            int i = 0;
            for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
                System.out.printf("Frame=%s, FileFormat=%s, HasAlpha=%s\r\n", ++i, frame.getRawDataFormat(), frame.hasAlpha());
            }
        } finally {
            image.dispose();
        }
        
        // The output may look like this:
        // ImageFile=c:\temp\sample.tif, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
        // Frame=1, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
        // Frame=2, FileFormat=RgbIndexed1Bpp, used channels: 1, HasAlpha=False
        

      • hasTransparentColor

        public boolean hasTransparentColor()

        Gets a value indicating whether image has transparent color.

        Overrides:
        hasTransparentColor in class RasterImage
      • setTransparentColor

        public void setTransparentColor(boolean value)

        Gets a value indicating whether image has transparent color.

        Overrides:
        setTransparentColor in class RasterImage
      • getBitsPerPixel

        public int getBitsPerPixel()

        Gets the image bits per pixel count.

        Specified by:
        getBitsPerPixel in class Image
        Returns:
        The image bits per pixel count.
      • getFrameOptions

        public TiffOptions getFrameOptions()

        Gets the frame create options.

      • getExifData

        public ExifData getExifData()

        Gets EXIF data from frame.

        Returns:
        EXIF data container
      • setExifData

        public void setExifData(ExifData value)

        Sets EXIF data from frame.

        Parameters:
        value - EXIF data container
      • getHorizontalResolution

        public double getHorizontalResolution()

        Gets the horizontal resolution, in pixels per inch, of this RasterImage.

        Overrides:
        getHorizontalResolution in class RasterImage
        Returns:
        The horizontal resolution.
        Code example:

        The following example shows how to set horizontal/vertical resolution of a separate TIFF frame.


        String dir = "c:\\temp\\";
        
        // Load a TIFF image from a file.
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.tif");
        try {
            com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
        
            int i = 0;
            for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
                // Get horizontal and vertical resolution of the TiffFrame.
                double horizontalResolution = frame.getHorizontalResolution();
                double verticalResolution = frame.getVerticalResolution();
                System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
                System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
        
                if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
                    // Use the SetResolution method for updating both resolution values in a single call.
                    System.out.println("Set resolution values to 96 dpi");
                    frame.setResolution(96.0, 96.0);
        
                    System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
                    System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
                }
        
                ++i;
            }
        } finally {
            image.dispose();
        }
        

      • setHorizontalResolution

        public void setHorizontalResolution(double value)

        Sets the horizontal resolution, in pixels per inch, of this RasterImage.

        Overrides:
        setHorizontalResolution in class RasterImage
        Parameters:
        value - The horizontal resolution.
      • getVerticalResolution

        public double getVerticalResolution()

        Gets the vertical resolution, in pixels per inch, of this RasterImage.

        Overrides:
        getVerticalResolution in class RasterImage
        Returns:
        The vertical resolution.
        Code example:

        The following example shows how to set horizontal/vertical resolution of a separate TIFF frame.


        String dir = "c:\\temp\\";
        
        // Load a TIFF image from a file.
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.tif");
        try {
            com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) image;
        
            int i = 0;
            for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : tiffImage.getFrames()) {
                // Get horizontal and vertical resolution of the TiffFrame.
                double horizontalResolution = frame.getHorizontalResolution();
                double verticalResolution = frame.getVerticalResolution();
                System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
                System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
        
                if (horizontalResolution != 96.0 || verticalResolution != 96.0) {
                    // Use the SetResolution method for updating both resolution values in a single call.
                    System.out.println("Set resolution values to 96 dpi");
                    frame.setResolution(96.0, 96.0);
        
                    System.out.printf("The horizontal resolution of frame %s, pixels per inch: %s\r\n", i, horizontalResolution);
                    System.out.printf("The vertical resolution, of frame %s, pixels per inch: %s\r\n", i, verticalResolution);
                }
        
                ++i;
            }
        } finally {
            image.dispose();
        }
        

      • setVerticalResolution

        public void setVerticalResolution(double value)

        Sets the vertical resolution, in pixels per inch, of this RasterImage.

        Overrides:
        setVerticalResolution in class RasterImage
        Parameters:
        value - The vertical resolution.
      • alignResolutions

        public void alignResolutions()

        Helper method to make horizontal and vertical resolutions equal.

      • copyFrame

        public static TiffFrame copyFrame(TiffFrame tiffFrame)

        Copies the entire frame (duplicates).

        Parameters:
        tiffFrame - The tiff frame to copy.
        Returns:
        The newly copied tiff frame.
      • createFrameFrom

        public static TiffFrame createFrameFrom(TiffFrame tiffFrame,
                                                TiffOptions options)

        Creates the frame from specified tiffFrame using the specified options. The pixel data is preserved but converted to the desired format.

        Parameters:
        tiffFrame - The tiff frame to create from.
        options - The new options to use.
        Returns:
        The newly created frame.
        Code example:

        The following example shows how to create a grayscale copy of an existing frame and add it to a TIFF image.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.imageoptions.TiffOptions createTiffOptions
                = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
        
        // Create a permanent, not temporary file source.
        createTiffOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "multipage.tif", false));
        createTiffOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.Rgb);
        createTiffOptions.setBitsPerSample(new int[]{8, 8, 8});
        
        com.aspose.imaging.fileformats.tiff.TiffImage tiffImage = (com.aspose.imaging.fileformats.tiff.TiffImage) com.aspose.imaging.Image.create(createTiffOptions, 100, 100);
        try {
            // The linear gradient from the left-top to the right-bottom corner of the image.
            com.aspose.imaging.brushes.LinearGradientBrush brush =
                    new com.aspose.imaging.brushes.LinearGradientBrush(
                            new com.aspose.imaging.Point(0, 0),
                            new com.aspose.imaging.Point(tiffImage.getWidth(), tiffImage.getHeight()),
                            com.aspose.imaging.Color.getRed(),
                            com.aspose.imaging.Color.getGreen());
        
            // Fill the active frame with a linear gradient brush.
            com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(tiffImage.getActiveFrame());
            gr.fillRectangle(brush, tiffImage.getBounds());
        
            // Grayscale options
            com.aspose.imaging.imageoptions.TiffOptions createTiffFrameOptions
                    = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.Default);
            createTiffFrameOptions.setSource(new com.aspose.imaging.sources.StreamSource(new java.io.ByteArrayInputStream(new byte[0])));
            createTiffFrameOptions.setPhotometric(com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics.MinIsBlack);
            createTiffFrameOptions.setBitsPerSample(new int[]{8});
        
            // Create a grayscale copy of the active frame.
            // The pixel data is preserved but converted to the desired format.
            com.aspose.imaging.fileformats.tiff.TiffFrame grayscaleFrame
                    = com.aspose.imaging.fileformats.tiff.TiffFrame.createFrameFrom(tiffImage.getActiveFrame(), createTiffFrameOptions);
        
            // Add the newly created frame to the TIFF image.
            tiffImage.addFrame(grayscaleFrame);
        
            tiffImage.save();
        } finally {
            tiffImage.dispose();
        }