This tutorial illustrates how to load/save a DjVu file.
To use DjVu SDK, you firstly include several headers.
In this step, you should create a Storage instance from an DjVu file.
The Storage class is the most easiest one to load files from local disk or a http based web servers. The following code is trying to load image from a web server:
But you can also use the following easier way, which does not require you to open Storage instance separately:
You can also know how much pages in the Document by the following code:
Now, you're ready to load the image data from the Page instance. But first, please let me introduce how to get the page size.
The code above can get the "pixel" size of the page but it's very large to show all the portion in a display. So we should calculate the size which is fit into the display. In this tutorial, I'll introduce a way to get physical size from the page's resolution, known as dot-per-inch.
OK, now render the pixel image:
The 5th,6th parameter of Page::render method specifies the rendering image size in pixels and the 4th paramether specifies which portion of the image is actually stored to the buffer; actual picture size rendered is controled by the 4th parameter.
As I've explained in the steps, you can easily render the page image without knowing how the actual data are stored in DjVu file. But for advanced operations, you may have to access the internal data.
DjVu data is actually stored in a structure called chunk and each page is corresponding to a chunk. So you can get the chunk of the page from Page instance:
The chunk returned by Document class is called DJVM, which manages multipage chunks. The most chunks are for retaining pages but some are for special purposes. To get these chunks, do like the following code:
Please note that not all the chunks has child chunks and if you call Chunk::getChildren method on a chunk which does not have children, an exception occures. So you had better check whether it has children or not like the following:
As you know, DjVu is actually a bunch of chunks. And we usually load the chunks from DjVu file using IFF::deserialize method like the following:
And to save the chunks using IFF::serialize method:
DiskStorageWithRollback class is a Storage class which provides the rollback feature in case of serialization failure. So you have to call DiskStorageWithRollback::commit method after successful serialization; otherwise the serialization result is to be rollbacked. For more, see StorageRollback class.
The following does almost same thing:
When you modify Document instance, you have to synchronize the chunk before directly access it: