Skip to main content

I have Offset values for six images which are compressed in one tiff file. Need to split the tiff file in 6 different tiff image

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
dukead
Offline
Joined: 2013-08-22

I have to read a tiff image file which is compressed of 6 images and need to separate the images into 6 different tiff files. To identify the different images I am getting offset values like this from a xml file.

First image :data_offset 0 data_offset :data_length 7827 data_length
Second Image: :data_offset 7827 data_offset :data_length 9624 data_length
Third Image: : data_offset 17451 ( i.e 7827+9624) data_offset :data_length 5713 data_length
Fourth Image: :data_offset 23164 (7827+9624+5713) data_offset :data_length 9624 data_length
… similarly for all 6 images.
I have offset and length of individual images.How to split the original tiff file into different tiff image as per offset and length.
The code I am using below is reading the original tiff file and coping the same file as Output.

public class TiffImageReader {

public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("C://DS.tiff");
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
//Writes to this byte array output stream
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
}

byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("tiff");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
File imageFile = new File("C:// DSCOPY.tiff");
ImageIO.write(bufferedImage, "tiff", imageFile);
System.out.println(imageFile.getPath());
}
}
If I make byte to 1 and put one check at first offset 7827 and try to write the image ..Its showing ArrayOutofIndex..

"for (int readNum; (readNum = fis.read(buf)) !== 7827;)"

How to use offset and length to split my file.

Plz help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Abigail111
Offline
Joined: 2013-08-26

Following method list is for splitting TIFFdocument,you can have a try:

public static REDocument OpenDocumentFile(string filePath);
public static REDocument OpenDocumentFile(string filePath, BaseDecoder dec);
public abstract List SplitDocument(int pageIndex);
But if you just want to read tiff files,you can create a tiff document viewer which can help you view or read all kinds of tiff files

raybrownyy
Offline
Joined: 2013-09-26

Abigail111 wrote:
Following method list is for splitting TIFF document,you can have a try:

public static REDocument OpenDocumentFile(string filePath);
public static REDocument OpenDocumentFile(string filePath, BaseDecoder dec);
public abstract List SplitDocument(int pageIndex);
But if you just want to read tiff files,you can create a tiff document viewer which can help you view or read all kinds of tiff files
Thank you, it is quite an easy solution. This TIFF processing control really integrates multiple functions, including TIFF splitting, merging, sorting, etc.

arronlee
Offline
Joined: 2013-05-08

Hi, Abigail.
I have only tried to split Tiff files with the following code:

<br />
            private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            string FolderName ="C:/YGdemo.tif";</p>
<p>            YGDoc doc = YGFolder.OpenDocFile(folderName, new TIFReader());</p>
<p>            List<BaseDoc> baseDocs = doc.BreakDoc(3);</p>
<p>            int index = 0;</p>
<p>            foreach (BaseDoc baseDoc in baseDocs)<br />
            {</p>
<p>            YGFolder.SaveDocFolder((YGDoc)baseDoc, "c:/ygDoc" + index + ".tif", new TIFReader());<br />
            }</p>
<p>        }<br />

You can also google it and select a fine Tiff Processor whose way of processing is simple and fast to help you with the related work. Remember to check its free trial package first if possible. I hope you success. Good luck.

Best regards,
Arron