minidjvu: how to decode a DjVu page

This file describes minidjvu 0.3 library; this is also fully applicable to 0.33 version.

The library interface is unstable.

See also: how to compress a DjVu file


Step 0: get things working

Add this include line to you source files that use minidjvu:
    #include <minidjvu.h>
I'll assume that your compiler can find the minidjvu headers and your linker can link against the library. If not, try to read INSTALL or README, or try to add the parent release directory into the header search path.

This examples also require

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>

Step 1: load the image

You can load DjVu pages with mdjvu_load_djvu_page(). That's an example of doing full error handling:
    const char *input = "your_input_file_name_here.djvu";
    mdjvu_error_t error;
    mdjvu_image_t image = mdjvu_load_djvu_page(input, &error);
    if (!image)
    {
        fprintf(stderr, "%s: %s\n", input, mdjvu_get_error_message(error));
        exit(1);
    }

Step 2: render into a bitmap

An image is not yet a bitmap. It's a sequence of commands like "put (a bitmap) at point x = (an integer), y = (an integer)". These commands need to be interpreted. Function mdjvu_render() does this:
    mdjvu_bitmap_t bitmap = mdjvu_render(image);
    assert(bitmap);

Step 3: save the bitmap

You can save your bitmap into one of bitmap formats supported by minidjvu: PBM and Windows BMP. In any case, an output file will be rewritten if it exists. That's how to save a PBM:
    const char *output = "your_output_file_name_here.pbm";
    mdjvu_error_t error;
    if (!mdjvu_save_pbm(bitmap, output, &error))
    {
        fprintf(stderr, "%s: %s\n", output, mdjvu_get_error_message(error));
        exit(1);
    }
Doing it with BMPs is pretty similar:
    const char *output = "your_output_file_name_here.bmp";
    mdjvu_error_t error;
    if (!mdjvu_save_bmp(bitmap, output, &error))
    {
        fprintf(stderr, "%s: %s\n", output, mdjvu_get_error_message(error));
        exit(1);
    }
With TIFF, you can just call mdjvu_save_tiff(), but it's a good idea to check first whether TIFF support is compiled into minidjvu by calling
    int have_tiff = mdjvu_have_tiff_support();

For the sake of completeness, there's a second declaration of error in this examples. Obviously, you should remove it if you plan to compile this.


Step 4: clean up

If you no longer need the image and the bitmap, destroy them:
    mdjvu_image_destroy(image);
    mdjvu_bitmap_destroy(bitmap);