This tutorial illustrates how to load/save a DjVu file.
To use DjVu SDK, you firstly include several headers.
Since the base library is shared with PixelLive SDK, they're in Celartem namespace while DjVu related classes are in Celartem::DjVu namespace.
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:
There're also other Storage classes. For more information about them, see Storage Class Factory Functions.
You should create a Document instance from the Storage instance.
But you can also use the following easier way, which does not require you to open Storage instance separately:
The Document instance usually has several Page instances in it and pages are actually managed like as an array. The following code loads the first page:
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:
And basically, Document instance is the actual owner of page chunks. So you can also access chunks from Document 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.
Of course, Document class provides a way to save the Document :
The following does almost same thing:
When you modify Document instance, you have to synchronize the chunk before directly access it:
In the same way, when you modify some part of the chunk which is obtained from Document instance, you should synchronize the Document instance before using Document::save method: