extern "C" jboolean Java_org_ebookdroid_droids_djvu_codec_DjvuPage_renderPage(JNIEnv *env, jclass cls, jlong pageHangle, jint targetWidth, jint targetHeight, jfloat pageSliceX, jfloat pageSliceY, jfloat pageSliceWidth, jfloat pageSliceHeight, jintArray buffer, jint rendermode) { DEBUG_WRITE("Rendering page"); ddjvu_page_t* page = (ddjvu_page_t*) ((pageHangle)); ddjvu_rect_t pageRect; pageRect.x = 0; pageRect.y = 0; pageRect.w = targetWidth / pageSliceWidth; pageRect.h = targetHeight / pageSliceHeight; ddjvu_rect_t targetRect; targetRect.x = pageSliceX * targetWidth / pageSliceWidth; targetRect.y = pageSliceY * targetHeight / pageSliceHeight; targetRect.w = targetWidth; targetRect.h = targetHeight; unsigned int masks[] = { 0xFF0000, 0x00FF00, 0x0000FF }; ddjvu_format_t* pixelFormat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 3, masks); ddjvu_format_set_row_order(pixelFormat, TRUE); ddjvu_format_set_y_direction(pixelFormat, TRUE); char *pBuffer = (char *) env->GetPrimitiveArrayCritical(buffer, 0); jboolean result = ddjvu_page_render(page, (ddjvu_render_mode_t) rendermode, &pageRect, &targetRect, pixelFormat, targetWidth * 4, pBuffer); env->ReleasePrimitiveArrayCritical(buffer, pBuffer, 0); ddjvu_format_release(pixelFormat); return result; }
extern "C" jboolean Java_org_ebookdroid_droids_djvu_codec_DjvuPage_renderPageDirect(JNIEnv *env, jclass cls, jlong pageHangle, jlong contextHandle, jint targetWidth, jint targetHeight, jfloat pageSliceX, jfloat pageSliceY, jfloat pageSliceWidth, jfloat pageSliceHeight, jobject byteBuffer, jint rendermode) { DEBUG("Rendering page bitmap"); void *pixels; int ret; pixels = env->GetDirectBufferAddress(byteBuffer); if (!pixels) { ERROR("GetDirectBufferAddress failed!"); return JNI_FALSE; } ddjvu_page_t* page = (ddjvu_page_t*) ((pageHangle)); ddjvu_rect_t pageRect; pageRect.x = 0; pageRect.y = 0; pageRect.w = targetWidth / pageSliceWidth; pageRect.h = targetHeight / pageSliceHeight; ddjvu_rect_t targetRect; targetRect.x = pageSliceX * targetWidth / pageSliceWidth; targetRect.y = pageSliceY * targetHeight / pageSliceHeight; targetRect.w = targetWidth; targetRect.h = targetHeight; unsigned int masks[] = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}; ddjvu_format_t* pixelFormat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 4, masks); ddjvu_format_set_row_order(pixelFormat, TRUE); ddjvu_format_set_y_direction(pixelFormat, TRUE); while (!ddjvu_page_decoding_done(page)) { waitAndHandleMessages(env, contextHandle); } jboolean result = ddjvu_page_render(page, (ddjvu_render_mode_t) rendermode, &pageRect, &targetRect, pixelFormat, targetWidth * 4, (char*) pixels); ddjvu_format_release(pixelFormat); return result; }
zathura_error_t djvu_document_free(zathura_document_t* document, djvu_document_t* djvu_document) { if (document == NULL) { return ZATHURA_ERROR_INVALID_ARGUMENTS; } if (djvu_document != NULL) { ddjvu_context_release(djvu_document->context); ddjvu_document_release(djvu_document->document); ddjvu_format_release(djvu_document->format); free(djvu_document); } return ZATHURA_ERROR_OK; }
static int closeDocument(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); // should be safe if called twice if (doc->doc_ref != NULL) { ddjvu_document_release(doc->doc_ref); doc->doc_ref = NULL; } if (doc->context != NULL) { ddjvu_context_release(doc->context); doc->context = NULL; } if (doc->pixelformat != NULL) { ddjvu_format_release(doc->pixelformat); doc->pixelformat = NULL; } return 0; }
Model::DjVuDocument::~DjVuDocument() { ddjvu_document_release(m_document); ddjvu_context_release(m_context); ddjvu_format_release(m_format); }
/* * DjVu advertised readiness to provide bitmap: So get it! * we use the RGB format! */ static void get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, const ImageInfo *image_info ) { ddjvu_format_t *format; ddjvu_page_type_t type; Image *image; int ret, stride; unsigned char *q; ddjvu_rect_t rect; rect.x = x; rect.y = y; rect.w = (unsigned int) w; /* /10 */ rect.h = (unsigned int) h; /* /10 */ image = lc->image; type = ddjvu_page_get_type(lc->page); /* stride of this temporary buffer: */ stride = (type == DDJVU_PAGETYPE_BITONAL)? (image->columns + 7)/8 : image->columns *3; q = (unsigned char *) AcquireQuantumMemory(image->rows,stride); if (q == (unsigned char *) NULL) return; format = ddjvu_format_create( (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24, /* DDJVU_FORMAT_RGB24 * DDJVU_FORMAT_RGBMASK32*/ /* DDJVU_FORMAT_RGBMASK32 */ 0, NULL); #if 0 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */ if (format == NULL) { abort(); /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */ } #endif ddjvu_format_set_row_order(format, 1); ddjvu_format_set_y_direction(format, 1); ret = ddjvu_page_render(page, DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */ &rect, &rect, /* mmc: ?? */ format, stride, /* ?? */ (char*)q); (void) ret; ddjvu_format_release(format); if (type == DDJVU_PAGETYPE_BITONAL) { /* */ #if DEBUG printf("%s: expanding BITONAL page/image\n", __FUNCTION__); #endif register IndexPacket *indexes; size_t bit, byte; for (y=0; y < (ssize_t) image->rows; y++) { PixelPacket * o = QueueAuthenticPixels(image,0,y,image->columns,1,&image->exception); if (o == (PixelPacket *) NULL) break; indexes=GetAuthenticIndexQueue(image); bit=0; byte=0; /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/ for (x= 0; x < (ssize_t) image->columns; x++) { if (bit == 0) byte= (size_t) q[(y * stride) + (x / 8)]; if (indexes != (IndexPacket *) NULL) SetPixelIndex(indexes+x,(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01)); bit++; if (bit == 8) bit=0; byte>>=1; } if (SyncAuthenticPixels(image,&image->exception) == MagickFalse) break; } if (!image->ping) SyncImage(image); } else {
extern "C" jboolean Java_org_ebookdroid_droids_djvu_codec_DjvuPage_renderPageBitmap(JNIEnv *env, jclass cls, jlong pageHangle, jint targetWidth, jint targetHeight, jfloat pageSliceX, jfloat pageSliceY, jfloat pageSliceWidth, jfloat pageSliceHeight, jobject bitmap, jint rendermode) { //#ifdef USE_JNI_BITMAP_API DEBUG_WRITE("Rendering page bitmap"); AndroidBitmapInfo info; void *pixels; int ret; if ((ret = NativeBitmap_getInfo(env, bitmap, &info)) < 0) { DEBUG_PRINT("AndroidBitmap_getInfo() failed ! error=%d", ret); return 0; } DEBUG_WRITE("Checking format"); // if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { // DEBUG_WRITE("Bitmap format is not RGBA_8888 !"); // return 0; // } if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) { DEBUG_WRITE("Bitmap format is not RGB_565 !"); return 0; } DEBUG_WRITE("locking pixels"); if ((ret = NativeBitmap_lockPixels(env, bitmap, &pixels)) < 0) { DEBUG_PRINT("AndroidBitmap_lockPixels() failed ! error=%d", ret); return 0; } ddjvu_page_t* page = (ddjvu_page_t*) ((pageHangle)); ddjvu_rect_t pageRect; pageRect.x = 0; pageRect.y = 0; pageRect.w = targetWidth / pageSliceWidth; pageRect.h = targetHeight / pageSliceHeight; ddjvu_rect_t targetRect; targetRect.x = pageSliceX * targetWidth / pageSliceWidth; targetRect.y = pageSliceY * targetHeight / pageSliceHeight; targetRect.w = targetWidth; targetRect.h = targetHeight; unsigned int masks[] = { 0xF800, 0x07E0, 0x001F }; ddjvu_format_t* pixelFormat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK16, 3, masks); // unsigned int masks[] = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}; // ddjvu_format_t* pixelFormat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 4, masks); ddjvu_format_set_row_order(pixelFormat, TRUE); ddjvu_format_set_y_direction(pixelFormat, TRUE); // jboolean result = ddjvu_page_render(page, DDJVU_RENDER_COLOR, &pageRect, &targetRect, pixelFormat, targetWidth * 4, (char*)pixels); jboolean result = ddjvu_page_render(page, (ddjvu_render_mode_t) rendermode, &pageRect, &targetRect, pixelFormat, targetWidth * 2, (char*) pixels); ddjvu_format_release(pixelFormat); NativeBitmap_unlockPixels(env, bitmap); return result; //#else // DEBUG_WRITE("Rendering page bitmap not implemented"); // return 0; //#endif }
JNIEXPORT jboolean JNICALL Java_universe_constellation_orion_viewer_djvu_DjvuDocument_drawPage(JNIEnv *env, jobject thiz, jobject bitmap, float zoom, int pageW, int pageH, int patchX, int patchY, int patchW, int patchH) { LOGI("==================Start Rendering=============="); int ret; AndroidBitmapInfo info; void *pixels; int num_pixels = pageW * pageH; LOGI("Rendering page=%dx%d patch=[%d,%d,%d,%d]", pageW, pageH, patchX, patchY, patchW, patchH); LOGI("page: %x", page); LOGI("In native method\n"); if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) { LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); return 0; } LOGI("Checking format\n"); if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { LOGE("Bitmap format is not RGBA_8888 !"); return 0; } LOGI("locking pixels\n"); if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) { LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); return 0; } //float zoom = 0.0001f * zoom10000; int pageWidth = ddjvu_page_get_width(page); int pageHeight = ddjvu_page_get_height(page); ddjvu_rect_t pageRect; pageRect.x = 0; pageRect.y = 0; pageRect.w = round(zoom * pageWidth); pageRect.h = round(zoom * pageHeight); LOGI("Original page=%dx%d patch=[%d,%d,%d,%d]", pageWidth, pageHeight, pageRect.x, pageRect.y, pageRect.w, pageRect.h); ddjvu_rect_t targetRect; targetRect.x = patchX; targetRect.y = patchY; targetRect.w = patchW; targetRect.h = patchH; int shift = 0; if (targetRect.x < 0) { shift = -targetRect.x; targetRect.w += targetRect.x; targetRect.x = 0; } if (targetRect.y < 0) { shift += -targetRect.y * pageW; targetRect.h += targetRect.y; targetRect.y = 0; } if (pageRect.w < targetRect.x + targetRect.w) { targetRect.w = targetRect.w - (targetRect.x + targetRect.w - pageRect.w); } if (pageRect.h < targetRect.y + targetRect.h) { targetRect.h = targetRect.h - (targetRect.y + targetRect.h - pageRect.h); } memset(pixels, 255, num_pixels * 4); LOGI("Rendering page=%dx%d patch=[%d,%d,%d,%d]", patchW, patchH, targetRect.x, targetRect.y, targetRect.w, targetRect.h); unsigned int masks[4] = { 0xff, 0xff00, 0xff0000, 0xff000000 }; ddjvu_format_t* pixelFormat = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 4, masks); LOGI("zoom=%f ", zoom); ddjvu_format_set_row_order(pixelFormat, TRUE); ddjvu_format_set_y_direction(pixelFormat, TRUE); char * buffer = &(((unsigned char *)pixels)[shift*4]); jboolean result = ddjvu_page_render(page, DDJVU_RENDER_COLOR, &pageRect, &targetRect, pixelFormat, pageW * 4, buffer); ddjvu_format_release(pixelFormat); orion_updateContrast((unsigned char *) pixels, num_pixels*4); AndroidBitmap_unlockPixels(env, bitmap); LOGI("...Rendered"); return 1; }
/* ** 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_error_t djvu_document_open(zathura_document_t* document) { zathura_error_t error = ZATHURA_ERROR_OK; if (document == NULL) { error = ZATHURA_ERROR_INVALID_ARGUMENTS; goto error_out; } djvu_document_t* djvu_document = calloc(1, sizeof(djvu_document_t)); if (djvu_document == NULL) { error = ZATHURA_ERROR_OUT_OF_MEMORY; goto error_out; } /* setup format */ static unsigned int masks[4] = {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000}; djvu_document->format = ddjvu_format_create(DDJVU_FORMAT_RGBMASK32, 4, masks); if (djvu_document->format == NULL) { error = ZATHURA_ERROR_UNKNOWN; goto error_free; } ddjvu_format_set_row_order(djvu_document->format, TRUE); /* setup context */ djvu_document->context = ddjvu_context_create("zathura"); if (djvu_document->context == NULL) { error = ZATHURA_ERROR_UNKNOWN; goto error_free; } /* setup document */ djvu_document->document = ddjvu_document_create_by_filename( djvu_document->context, zathura_document_get_path(document), FALSE ); if (djvu_document->document == NULL) { error = ZATHURA_ERROR_UNKNOWN; goto error_free; } /* load document info */ ddjvu_message_t* msg; ddjvu_message_wait(djvu_document->context); while ((msg = ddjvu_message_peek(djvu_document->context)) && (msg->m_any.tag != DDJVU_DOCINFO)) { if (msg->m_any.tag == DDJVU_ERROR) { error = ZATHURA_ERROR_UNKNOWN; goto error_free; } ddjvu_message_pop(djvu_document->context); } /* decoding error */ if (ddjvu_document_decoding_error(djvu_document->document)) { handle_messages(djvu_document, true); error = ZATHURA_ERROR_UNKNOWN; goto error_free; } zathura_document_set_data(document, djvu_document); zathura_document_set_number_of_pages(document, ddjvu_document_get_pagenum(djvu_document->document)); return error; error_free: if (djvu_document->format != NULL) { ddjvu_format_release(djvu_document->format); } if (djvu_document->context != NULL) { ddjvu_context_release(djvu_document->context); } free(djvu_document); error_out: return error; }