Manipulating DjVu Formats

Converting DjVu to TIFF Format

Aspose.Imaging APIs are capable of loading DjVu files for possible conversion to raster image formats. This example demonstrates the usage of Aspose.Imaging for Java API to convert a DjVu file having multiple pages to a multipage TIFF image.

You can convert a DjVu file having multiple pages to TIFF image as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of TiffOptions while using any of the TiffExpectedFormat enumeration fields.
  3. Create an instance of DjvuMultiPageOptions and set it as MultiPageOptions property of the TiffOptions created in previous step.
  4. Call Image.save by passing the file path as well as the instance of TiffOptions.

Here is the complete source code.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
//Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
//Create an instance of TiffOptions & use preset options for Black n While with Deflate compression
TiffOptions exportOptions = new TiffOptions(TiffExpectedFormat.TiffDeflateBw);
//Initialize the DjvuMultiPageOptions
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions());
//Call Save method while passing instance of TiffOptions
image.save(dataDir + "ConvertDjvuToTiff_out.tiff", exportOptions);
// Display Status.
System.out.println("File conveted");

Converting Range of DjVu Pages

A DjVu image may have more than one pages like a multipage TIFF or multiframe GIF. If such DjVu image has to be converted to any raster format that could have more than one page or frame then be default, all pages of DjVu are exported. Aspose.Imaging APIs also provides the facility to convert only specific range of DjVu pages to multipage TIFF or multiframe GIF.

You can convert a specific range of DjVu pages to multiframe GIF or TIFF using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of TiffOptions or GifOptions.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions with instance of IntRange as parameter and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  5. Call Image.save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert first 2 pages of DjVu to TIFF format.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
// Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
// Create an instance of BmpOptions
BmpOptions exportOptions = new BmpOptions();
// Set BitsPerPixel for resultant images
exportOptions.setBitsPerPixel(32);
// Create an instance of IntRange and initialize it with range of pages
// to be exported
IntRange range = new IntRange(0, 2); //Export first 2 pages
int counter = 0;
for (int i : range.getRange()) {
// Save each page in separate file, as BMP do not support layering
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range.getArrayOneItemFromIndex(counter)));
image.save(String.format("{0}.bmp", counter++), exportOptions);
}

Following code snippet converts the first 2 pages of DjVu to GIF format.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
//Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
//Create an instance of GifOptions with its default constructor
GifOptions exportOptions = new GifOptions();
//Set the resolution of resultant image
exportOptions.setResolutionSettings(new ResolutionSetting(300, 300));
//Set PaletteCorrection to false
exportOptions.setDoPaletteCorrection(false);
//Create an instance of 8bit palette and set it as Palette property for instance of GifOptions
exportOptions.setPalette(ColorPaletteHelper.create8Bit());
//Create an instance of IntRange and initialize it with range of pages to be exported
IntRange range = new IntRange(0, 2); //Export first 2 pages
//Initialize an instance of DjvuMultiPageOptions while passing instance of IntRange
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range));
//Call Save method while passing instance of GifOptions
image.save(dataDir + "ConvertDjvuPagesToGif_out.gif", exportOptions);
System.out.println("File conveted");

Converting Range of DjVu Pages to Separate Images

It is possible to convert only a specific range of DjVu pages to multipage TIFF or multiframe GIF where the resultant image will contain multiple pages/frames. Aspose.Imaging APIs also provide the means to convert the specific range of DjVu pages to raster image formats that do not support layering such as BMP, PNG & JPEG. In this case each DjVu page will be converted to a separate image.

You can convert a specific range of DjVu pages to separate images using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of ImageOptionsBase.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions while passing the indices of DjVu pages as parameter and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  5. Call Image.Save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert first 2 pages of DjVu to BMP format.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
//Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
//Create an instance of BmpOptions
BmpOptions exportOptions = new BmpOptions();
//Set BitsPerPixel for resultant images
exportOptions.setBitsPerPixel(32);
//Create an instance of IntRange and initialize it with range of pages to be exported
IntRange range = new IntRange(0, 2); //Export first 2 pages
int counter = 0;
for (int i : range.getRange()) {
//Save each page in separate file, as BMP do not support layering
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range.getArrayOneItemFromIndex(counter)));
String output = dataDir + "ConvertDjvuPagesToImages_out" + (counter++) + ".bmp";
image.save(output, exportOptions);
}
// Display Status.
System.out.println("File conveted");

Converting Specific Portion of DjVu Page

Aspose.Imaging APIs provides an easy to use mechanism to export only a specific portion of DjVu page to raster image formats. Aspose.Imaging for Java API has exposed an overload version of DjvuMultiPageOptions constructor that can accept an integer parameter for DjVu page index and an instance of Rectangle to specify the desired area to be exported.

You can convert a specific portion of DjVu page to image using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of ImageOptionsBase.
  3. Create an instance of DjvuMultiPageOptions while passing the index of DjVu page along with an instance of Rectangle as parameter, and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  4. Call Image.Save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert a portion of 1st page of DjVu to PNG format.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
// Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
// Create an instance of PngOptions
PngOptions exportOptions = new PngOptions();
// Set ColorType to Grayscale
exportOptions.setColorType(PngColorType.Grayscale);
// Create an instance of Rectangle and specify the portion on DjVu page
Rectangle exportArea = new Rectangle(0, 0, 500, 500);
// Specify the DjVu page index
int exportPageIndex = 2;
// Initialize an instance of DjvuMultiPageOptions
// while passing index of DjVu page index and instance of Rectangle
// covering the area to be exported
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(exportPageIndex, exportArea));
// Save the image
image.save(dataDir + "ConvertSpecificPortionOfDjVuPage_out.djvu", exportOptions);

Converting DjVu to PDF Format

Aspose.Imaging APIs provides the functionality to export the DjVu files to raster images as well as PDF format. This article demonstrates the usage of Aspose.Imaging for Java API to export the DjVu pages to PDF format.

You can convert a specific range of DjVu pages to PDF format using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of PdfOptions.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions while passing the indices of DjVu pages as parameter and set it as MultiPageOptions property of the PdfOptions created in step 2.
  5. Call Image.save by passing the file path as well as the instance of PdfOptions.

Here is the source code to convert first 5 pages of DjVu to PDF format.

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
//Load a DjVu image
DjvuImage image = (DjvuImage) Image.load(dataDir + "Sample.djvu");
//Create an instance of PdfOptions
PdfOptions exportOptions = new PdfOptions();
//Initialize the metadata for Pdf document
exportOptions.setPdfDocumentInfo(new PdfDocumentInfo());
//Create an instance of IntRange and initialize it with the range of DjVu pages to be exported
IntRange range = new IntRange(0, 3); //Export first 3 pages
//Initialize an instance of DjvuMultiPageOptions with range of DjVu pages to be exported
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range));
//Save the result in PDF format
image.save(dataDir + "ConvertDjvuToPdf_out.pdf", exportOptions);
// Display Status.
System.out.println("File conveted");

Image Processing using Multithreading

Aspose.Imaging is multithread safe as long as only one thread works on a Document at a time. It is a typical scenario to have one thread working on one document.This article demonstrates how Aspose.Imaging for Java supports parallel DJVU images processing using multithreading.

String dir = Utils.getSharedDataDir(ParallelDJVUImagesProcessingUsingMultithreading.class) + "djvu/";
final String fileName = dir + "Sample.djvu";
final String filePath = dir + "Sample.djvu";
String outDir = dir;
int numThreads = 20;
ExecutorService execServ = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numThreads; i++)
{
final String outputFile = outDir + fileName + "_task" + i+ ".png";
execServ.execute(new Runnable()
{
@Override
public void run()
{
RandomAccessFile fs;
try
{
fs = new RandomAccessFile(fileName, "r");
}
catch (FileNotFoundException e)
{
throw new RuntimeException(e.getMessage(), e);
}
try
{
Image image = Image.load(fs);
try
{
image.save(outputFile, new PngOptions());
}
finally
{
image.close();
}
}
finally
{
try
{
fs.close();
}
catch (IOException ignore)
{
}
}
}
});
}
execServ.shutdown();
while (!execServ.awaitTermination(1, TimeUnit.SECONDS))
{
Thread.yield();
}

Memory Strategy optimization

Loading of Djvu images can be proceeded using memory strategy optimization - ie limiting memory buffer size for operation.