Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. JNIEXPORT void JNICALL
  2. JNI_FN(MuPDFCore_saveImageInternal)(JNIEnv * env, jobject thiz, jobject bitmap, int imgW, int imgH, int posX, int posY)
  3. {
  4.  
  5.     AndroidBitmapInfo info;
  6.     void *pixels;
  7.     int ret;
  8.     fz_irect bbox;
  9.     fz_rect rect;
  10.     fz_pixmap *pix = NULL;
  11.     fz_image *img = NULL;
  12.     fz_point pos;
  13.     fz_matrix matrix;
  14.  
  15.     image_struct *img_s = NULL;
  16.  
  17.     globals *glo = get_globals(env, thiz);
  18.     fz_context *ctx = glo->ctx;
  19.     pdf_document *doc = glo->doc;
  20.     pdf_page *page = &glo->pages[glo->current];
  21.  
  22.  
  23.     fz_var(pix);
  24.     fz_var(img);
  25.     fz_var(pos);
  26.  
  27.     LOGI("In native method\n");
  28.     if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
  29.         LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
  30.         return 0;
  31.     }
  32.  
  33.     LOGI("Checking format\n");
  34.     if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
  35.         LOGE("Bitmap format is not RGBA_8888 !");
  36.         return 0;
  37.     }
  38.  
  39.     LOGI("locking pixels\n");
  40.     if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
  41.         LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
  42.         return 0;
  43.     }
  44.  
  45.     bbox.x0 = 0;
  46.     bbox.y0 = 0;
  47.     bbox.x1 = imgW;
  48.     bbox.y1 = imgH;
  49.     pix = fz_new_pixmap_with_bbox_and_data(ctx, glo->colorspace, &bbox, pixels);
  50.     img = fz_new_image_from_pixmap(ctx, pix, NULL);
  51.  
  52.     pos.x = posX;
  53.     pos.y = posY;
  54.  
  55.     fz_matrix ctm = fz_identity;
  56.     float zoom = glo->resolution / 72;
  57.     zoom = 1.0 / zoom;
  58.     fz_scale(&ctm, zoom, zoom);
  59.  
  60.     fz_transform_point(&pos, &ctm);
  61.     img_s->image = *img;
  62.     img_s->pos = pos;
  63.     matrix.a = imgW;
  64.     matrix.d = imgH;
  65.     matrix.e = posX;
  66.     matrix.f = posY;
  67.     img_s->matrix = matrix;
  68.     img_s->doc = doc;
  69.  
  70.  
  71.     pdf_clean_page_contents(doc, page, NULL, my_process_fn, &img_s);
  72.  
  73. }
  74.  
  75. static void my_process_fn(void *arg, fz_buffer *buffer, pdf_obj *res)
  76. {
  77.     image_struct *img_s = arg;
  78.     fz_image *img = &img_s->image;
  79.     fz_matrix *matrix = &img_s->matrix;
  80.     pdf_document *doc = img_s->doc;
  81.     fz_matrix ctm = fz_identity;
  82.     fz_device *pdf_dev = NULL;
  83.  
  84.     fz_var(pdf_dev);
  85.  
  86.     pdf_dev = pdf_new_pdf_device(doc, NULL, res, &ctm, buffer);
  87.  
  88.     pdf_dev_fill_image(pdf_dev, img, matrix, 1);
  89.  
  90.     fz_free_device(pdf_dev);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement