zathura_error_t djvu_page_render_cairo(zathura_page_t* page, void* UNUSED(data), cairo_t* cairo, bool GIRARA_UNUSED(printing)) { if (page == NULL || cairo == NULL) { return ZATHURA_ERROR_INVALID_ARGUMENTS; } zathura_document_t* document = zathura_page_get_document(page); if (document == NULL) { return ZATHURA_ERROR_UNKNOWN; } /* init ddjvu render data */ djvu_document_t* djvu_document = zathura_document_get_data(document); ddjvu_page_t* djvu_page = ddjvu_page_create_by_pageno(djvu_document->document, zathura_page_get_index(page)); if (djvu_page == NULL) { return ZATHURA_ERROR_UNKNOWN; } while (!ddjvu_page_decoding_done(djvu_page)) { handle_messages(djvu_document, true); } cairo_surface_t* surface = cairo_get_target(cairo); if (surface == NULL || cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS || cairo_surface_get_type(surface) != CAIRO_SURFACE_TYPE_IMAGE) { ddjvu_page_release(djvu_page); return ZATHURA_ERROR_UNKNOWN; } unsigned int page_width = cairo_image_surface_get_width(surface); unsigned int page_height = cairo_image_surface_get_height(surface);; ddjvu_rect_t rrect = { 0, 0, page_width, page_height }; ddjvu_rect_t prect = { 0, 0, page_width, page_height }; char* surface_data = (char*) cairo_image_surface_get_data(surface); if (surface_data == NULL) { ddjvu_page_release(djvu_page); return ZATHURA_ERROR_UNKNOWN; } /* render page */ ddjvu_page_render(djvu_page, DDJVU_RENDER_COLOR, &prect, &rrect, djvu_document->format, cairo_image_surface_get_stride(surface), surface_data); ddjvu_page_release(djvu_page); return ZATHURA_ERROR_OK; }
extern "C" void Java_org_vudroid_djvudroid_codec_DjvuPage_free(JNIEnv *env, jclass cls, jlong pageHangle) { ddjvu_page_release((ddjvu_page_t*)pageHangle); }
static int getPageInfo(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); int pageno = luaL_checkint(L, 2); ddjvu_page_t *djvu_page; int page_width, page_height, page_dpi; double page_gamma; ddjvu_page_type_t page_type; char *page_type_str; djvu_page = ddjvu_page_create_by_pageno(doc->doc_ref, pageno - 1); if (! djvu_page) return luaL_error(L, "cannot create djvu_page #%d", pageno); while (! ddjvu_page_decoding_done(djvu_page)) handle(L, doc->context, TRUE); page_width = ddjvu_page_get_width(djvu_page); lua_pushnumber(L, page_width); page_height = ddjvu_page_get_height(djvu_page); lua_pushnumber(L, page_height); page_dpi = ddjvu_page_get_resolution(djvu_page); lua_pushnumber(L, page_dpi); page_gamma = ddjvu_page_get_gamma(djvu_page); lua_pushnumber(L, page_gamma); page_type = ddjvu_page_get_type(djvu_page); switch (page_type) { case DDJVU_PAGETYPE_UNKNOWN: page_type_str = "UNKNOWN"; break; case DDJVU_PAGETYPE_BITONAL: page_type_str = "BITONAL"; break; case DDJVU_PAGETYPE_PHOTO: page_type_str = "PHOTO"; break; case DDJVU_PAGETYPE_COMPOUND: page_type_str = "COMPOUND"; break; default: page_type_str = "INVALID"; break; } lua_pushstring(L, page_type_str); ddjvu_page_release(djvu_page); return 5; }
JNIEXPORT void JNICALL Java_universe_constellation_orion_viewer_djvu_DjvuDocument_gotoPageInternal(JNIEnv *env, jobject thiz, int pageNum) { LOGI("Opening page: %d", pageNum); if (page != NULL) { ddjvu_page_release(page); page = NULL; } page = ddjvu_page_create_by_pageno(doc, pageNum); }
static int closePage(lua_State *L) { DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); // should be safe if called twice if (page->page_ref != NULL) { ddjvu_page_release(page->page_ref); page->page_ref = NULL; } return 0; }
JNIEXPORT void JNICALL Java_universe_constellation_orion_viewer_djvu_DjvuDocument_destroying(JNIEnv * env, jobject thiz) { LOGI("Closing doc..."); if (page != NULL) { ddjvu_page_release(page); page = NULL; } if (doc != NULL) { ddjvu_document_release(doc); doc = NULL; } if (context != NULL) { ddjvu_context_release(context); context = context; } }
QImage Model::DjVuPage::render(qreal horizontalResolution, qreal verticalResolution, Rotation rotation, const QRect& boundingRect) const { QMutexLocker mutexLocker(&m_parent->m_mutex); ddjvu_status_t status; ddjvu_page_t* page = ddjvu_page_create_by_pageno(m_parent->m_document, m_index); if(page == 0) { return QImage(); } while(true) { status = ddjvu_page_decoding_status(page); if(status < DDJVU_JOB_OK) { clearMessageQueue(m_parent->m_context, true); } else { break; } } if(status >= DDJVU_JOB_FAILED) { ddjvu_page_release(page); return QImage(); } switch(rotation) { default: case RotateBy0: ddjvu_page_set_rotation(page, DDJVU_ROTATE_0); break; case RotateBy90: ddjvu_page_set_rotation(page, DDJVU_ROTATE_270); break; case RotateBy180: ddjvu_page_set_rotation(page, DDJVU_ROTATE_180); break; case RotateBy270: ddjvu_page_set_rotation(page, DDJVU_ROTATE_90); break; } ddjvu_rect_t pagerect; pagerect.x = 0; pagerect.y = 0; switch(rotation) { default: case RotateBy0: case RotateBy180: pagerect.w = qRound(horizontalResolution / m_resolution * m_size.width()); pagerect.h = qRound(verticalResolution / m_resolution * m_size.height()); break; case RotateBy90: case RotateBy270: pagerect.w = qRound(horizontalResolution / m_resolution * m_size.height()); pagerect.h = qRound(verticalResolution / m_resolution * m_size.width()); break; } ddjvu_rect_t renderrect; if(boundingRect.isNull()) { renderrect.x = pagerect.x; renderrect.y = pagerect.y; renderrect.w = pagerect.w; renderrect.h = pagerect.h; } else { renderrect.x = boundingRect.x(); renderrect.y = boundingRect.y(); renderrect.w = boundingRect.width(); renderrect.h = boundingRect.height(); } QImage image(renderrect.w, renderrect.h, QImage::Format_RGB32); if(!ddjvu_page_render(page, DDJVU_RENDER_COLOR, &pagerect, &renderrect, m_parent->m_format, image.bytesPerLine(), reinterpret_cast< char* >(image.bits()))) { image = QImage(); } clearMessageQueue(m_parent->m_context, false); ddjvu_page_release(page); return image; }
gint32 load_djvu (const gchar * filename, GimpRunMode runmode, gboolean preview) { GimpPixelRgn rgn_in; GimpDrawable *drawable; gint32 volatile image_ID; gint32 layer_ID; int x1, y1, x2, y2, width, height; unsigned char *src = NULL; int file_length; ctx = ddjvu_context_create("gimp"); // doc = ddjvu_document_create_by_filename(ctx,filename, TRUE); while (! ddjvu_document_decoding_done(doc)) handle(TRUE); i = ddjvu_document_get_pagenum(doc); dialog( &pageno ,i); //processing the page page = ddjvu_page_create_by_pageno(doc, pageno-1); while (! ddjvu_page_decoding_done(page)) handle(TRUE); if (ddjvu_page_decoding_error(page)) { fprintf(stderr,"unexpected error "); exit(10); } // ddjvu variables ddjvu_rect_t prect; ddjvu_rect_t rrect; ddjvu_format_style_t style; ddjvu_render_mode_t mode; ddjvu_format_t *fmt; int iw = ddjvu_page_get_width(page); int ih = ddjvu_page_get_height(page); int dpi = ddjvu_page_get_resolution(page); ddjvu_page_type_t type = ddjvu_page_get_type(page); char *image = 0; int rowsize; //end of ddjvu variables style= DDJVU_FORMAT_RGB24; mode=DDJVU_RENDER_COLOR; fmt = ddjvu_format_create(style, 0, 0); ddjvu_format_set_row_order(fmt, 1); prect.w = iw; prect.h = ih; prect.x = 0; prect.y = 0; flag_scale=150; prect.w = (unsigned int) (iw * flag_scale) / dpi; prect.h = (unsigned int) (ih * flag_scale) / dpi; rrect = prect; rowsize = (rrect.w *3); image = (char*)malloc(rowsize * rrect.h); //generating page ddjvu_page_render(page, mode, &prect, &rrect, fmt, rowsize, image); ddjvu_page_release(page); char *s =image; /* create output image */ width = prect.w; height = prect.h; x1 = 0; y1 = 0; x2 = prect.w; y2 = prect.h; image_ID = gimp_image_new (width, height, GIMP_RGB); layer_ID = gimp_layer_new (image_ID, _("Background"), width, height, GIMP_RGB, 100, GIMP_NORMAL_MODE); gimp_image_add_layer (image_ID, layer_ID, 0); drawable = gimp_drawable_get (layer_ID); // initializes the drawable gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); gimp_pixel_rgn_init (&rgn_in, drawable, x1, y1, x2 - x1, y2 - y1, TRUE, TRUE); gimp_pixel_rgn_set_rect (&rgn_in, image, 0, 0, width, height); //g_free (buf); gimp_drawable_flush (drawable); gimp_drawable_merge_shadow (drawable->drawable_id, TRUE); gimp_drawable_update (drawable->drawable_id, x1, y1, x2 - x1, y2 - y1); gimp_image_set_filename (image_ID, filename); return image_ID; image =NULL; //not handling any error if (doc) ddjvu_document_release(doc); if (ctx) ddjvu_context_release(ctx); }
/* ** Returns 0 for success, negative number for error code. ** bpp can be 24 or 8. ** */ int bmpdjvu_djvufile_to_bmp(WILLUSBITMAP *bmp,char *infile,int pageno, int dpi,int bpp,FILE *out) { ddjvu_context_t *ctx; ddjvu_document_t *doc; ddjvu_page_t *page; /* ddjvu_page_type_t type; */ ddjvu_rect_t prect; ddjvu_rect_t rrect; ddjvu_format_style_t style; ddjvu_render_mode_t mode; ddjvu_format_t *fmt; int i,iw,ih,idpi,status; ctx=ddjvu_context_create("bmpdjvu_djvufile_to_bmp"); if (ctx==NULL) { nprintf(out,"Cannot create djvu context.\n"); return(-1); } doc=ddjvu_document_create_by_filename(ctx,infile,1); if (doc==NULL) { ddjvu_context_release(ctx); nprintf(out,"Cannot create djvu document context from djvu file %s.\n", infile); return(-2); } i=ddjvu_document_get_pagenum(doc); if (pageno<0 || pageno>i) { ddjvu_document_release(doc); ddjvu_context_release(ctx); nprintf(out,"Page number %d is out of range for djvu file %s.\n",pageno,infile); return(-3); } page=ddjvu_page_create_by_pageno(doc,pageno-1); if (page==NULL) { ddjvu_document_release(doc); ddjvu_context_release(ctx); nprintf(out,"Cannot parse page %d of djvu file %s.\n",pageno,infile); return(-4); } while (!ddjvu_page_decoding_done(page)) handle(1,ctx); if (ddjvu_page_decoding_error(page)) { ddjvu_page_release(page); ddjvu_document_release(doc); ddjvu_context_release(ctx); nprintf(out,"Error decoding page %d of djvu file %s.\n",pageno,infile); return(-5); } /* type= */ ddjvu_page_get_type(page); /* printf("type=%d\n",type); description=ddjvu_page_get_long_description(page); printf("Description='%s'\n",description); */ iw = ddjvu_page_get_width(page); ih = ddjvu_page_get_height(page); idpi = ddjvu_page_get_resolution(page); prect.x=prect.y=0; bmp->width=prect.w=iw*dpi/idpi; bmp->height=prect.h=ih*dpi/idpi; bmp->bpp=(bpp==8) ? 8 : 24; rrect=prect; bmp_alloc(bmp); if (bmp->bpp==8) { int ii; for (ii=0;ii<256;ii++) bmp->red[ii]=bmp->blue[ii]=bmp->green[ii]=ii; } mode=DDJVU_RENDER_COLOR; style=bpp==8 ? DDJVU_FORMAT_GREY8 : DDJVU_FORMAT_RGB24; fmt=ddjvu_format_create(style,0,0); if (fmt==NULL) { ddjvu_page_release(page); ddjvu_document_release(doc); ddjvu_context_release(ctx); nprintf(out,"Error setting DJVU format for djvu file %s (page %d).\n",infile,pageno); return(-6); } ddjvu_format_set_row_order(fmt,1); status=ddjvu_page_render(page,mode,&prect,&rrect,fmt,bmp_bytewidth(bmp),(char *)bmp->data); ddjvu_format_release(fmt); ddjvu_page_release(page); ddjvu_document_release(doc); ddjvu_context_release(ctx); if (!status) { nprintf(out,"Error rendering page %d of djvu file %s.\n",pageno,infile); return(-7); } return(0); }
zathura_image_buffer_t* djvu_page_render(zathura_page_t* page, void* UNUSED(data), zathura_error_t* error) { if (page == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } zathura_document_t* document = zathura_page_get_document(page); if (document == NULL) { return NULL; } /* calculate sizes */ unsigned int page_width = zathura_document_get_scale(document) * zathura_page_get_width(page); unsigned int page_height = zathura_document_get_scale(document) * zathura_page_get_height(page); if (page_width == 0 || page_height == 0) { if (error != NULL) { *error = ZATHURA_ERROR_UNKNOWN; } goto error_out; } /* init ddjvu render data */ djvu_document_t* djvu_document = zathura_document_get_data(document); ddjvu_page_t* djvu_page = ddjvu_page_create_by_pageno( djvu_document->document, zathura_page_get_index(page)); if (djvu_page == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_UNKNOWN; } goto error_out; } while (!ddjvu_page_decoding_done(djvu_page)) { handle_messages(djvu_document, true); } ddjvu_rect_t rrect = { 0, 0, page_width, page_height }; ddjvu_rect_t prect = { 0, 0, page_width, page_height }; zathura_image_buffer_t* image_buffer = zathura_image_buffer_create(page_width, page_height); if (image_buffer == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_OUT_OF_MEMORY; } goto error_free; } /* set rotation */ ddjvu_page_set_rotation(djvu_page, DDJVU_ROTATE_0); /* render page */ ddjvu_page_render(djvu_page, DDJVU_RENDER_COLOR, &prect, &rrect, djvu_document->format, 3 * page_width, (char*) image_buffer->data); return image_buffer; error_free: ddjvu_page_release(djvu_page); zathura_image_buffer_free(image_buffer); error_out: return NULL; }