Expand ofxPdf to support images

I have modified ofxPdf a bit so that it reads pdfs with images…

here is the core of the code:

void ofxPDF::loadPDF(string path, int pagenumber)
{

path = ofToDataPath(path);

fz_context *ctx;
fz_document* doc;

fz_device *dev;

fz_bbox box;
fz_rect bounds;
fz_matrix ctm;
fz_pixmap *image;
fz_colorspace *colorspace;

int i,j,rotation = 0;
int pagecount = 0;
fz_page *page;
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
fz_try(ctx){
    doc = fz_open_document(ctx, (char*)path.c_str());
}fz_catch(ctx){
    fz_free_context(ctx);
    return false;
}

colorspace = fz_device_rgb;
pagecount = fz_count_pages(doc);


for(i=0;i<pagecount;i++){
    cout<<i<<endl;
    page = fz_load_page(doc,i);
    
    if(i == 0){
        bounds= fz_bound_page(doc,page);
        box.x0 =bounds.x0;
        box.x1 = bounds.x1;
        box.y0 = bounds.y0;
        box.y1 = bounds.y1;
        
        width = bounds.x1 - bounds.x0;
        height = bounds.y1 - bounds.y0;

    }
   // colorspace= fz_pixmap_colorspace(ctx, NULL);

    image = fz_new_pixmap_with_bbox(ctx,colorspace,box);
    dev = fz_new_draw_device(ctx,image);
    fz_clear_pixmap_with_value(ctx, image, 255);
    fz_try(ctx){
        colorspace= fz_device_rgb;
        cout<<colorspace<<endl;
        fz_run_page(doc,page,dev,fz_identity,NULL);
        ofPixels pixs;
         pixs.setFromExternalPixels(fz_pixmap_samples(ctx, image) , fz_pixmap_width(ctx,image), fz_pixmap_height(ctx,image), OF_PIXELS_RGBA);
        img_texture.setFromPixels(pixs);

        
        fz_free_device(dev);
        dev = fz_new_ofpath_device(ctx, paths);
        fz_run_page(doc,page,dev,fz_identity,NULL);
    //    fz_write_png(ctx, image, (char*)path.c_str(), false);

    }fz_catch(ctx){
        fz_drop_pixmap(ctx,image);
        fz_free_device(dev);
        fz_free_page(doc, page);
        continue;
    }
    

    

    
    
    fz_drop_pixmap(ctx,image);
    fz_free_device(dev);
    fz_free_page(doc, page);
}

fz_close_document(doc);
fz_free_context(ctx);

}

currently it only works with rgbalpha pdfs,

the reason why it doesn’t work with other types is because I can’t tell from the type of pixels what
colorspace is using, if is grayscale rgb cmyk or brg

the newer version of muPdf has a function that returns the fz_colorspace from the ctx before you allocate the image,

not sure if this version of mupdf has a way to tell the type of the pdf…

Any ideas??
is there a smart way to tell from an unsigned char * what type of pixels it has?

maybe divide by with and height? and see if it leaves 1 2 or 3 or 4 etc?

dev_ofpath.cpp (5.9 KB)
ofxPDF.h (606 Bytes)
ofxPDF.cpp (4.8 KB)

http://dropcanvas.com/3ly4z

here is the addon with the mupdf library in it, just drag and drop it to xcode to include it

here is the tiger pdf

http://dropcanvas.com/yhr0l

Here In this example I used the new version of the library - it has different functions so the implementation is a bit different

but it doesn’t compile yet…

http://dropcanvas.com/avj18

I get apple mach-O linker (Id) errors

---- I will have to write wrappers for the C functions… patience…

I can’t figure it out, I can’t link this thing it keeps saying it can’t find the implementation…

maybe another day