/*
 * Class:     org_libharu_PdfPage
 * Method:    drawJpegImage
 * Signature: ([BFFFF)V
 */
JNIEXPORT void JNICALL
Java_org_libharu_PdfPage_drawJpegImage(JNIEnv *env, jobject obj, jbyteArray imageData, jfloat x,
        jfloat y, jfloat width, jfloat height) {
    jint page, pdf;
    jbyte* buffer; /* The image data as a jbyte[] (really signed char[]) */
    jsize len; /* The number of elements in the image byte array */

    /* Get mHPDFPagePointer */
    page = (*env)->GetIntField(env, obj, mHPDFPagePointer);
    /* Get mParentHPDFDocPointer */
    pdf = (*env)->GetIntField(env, obj, mParentHPDFDocPointer);

    /* Get the image data as a native byte array */
    buffer = (*env)->GetByteArrayElements(env, imageData, NULL);
    /* Get the number of elements in the image byte array */
    len = (*env)->GetArrayLength(env, imageData);

    /* Load an HPDF_Image from the image byte array */
    HPDF_Image image = HPDF_LoadJpegImageFromMem((HPDF_Doc) pdf, (HPDF_BYTE*) buffer,
            (HPDF_UINT) (len * sizeof(jbyte)));

    /* Actually draw the image */
    HPDF_Page_DrawImage((HPDF_Page) page, image, (HPDF_REAL) x, (HPDF_REAL) y, (HPDF_REAL) width,
            (HPDF_REAL) height);

    /* Release (free) the native byte array */
    (*env)->ReleaseByteArrayElements(env, imageData, buffer, JNI_ABORT);
}
Exemple #2
0
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = 0;

  if (Uri::isDataUri(imgUrl)) {
    Uri::Uri uri = Uri::parseDataUri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)uri.data.c_str(), 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)uri.data.c_str(), 
				      uri.data.size()); 
  } else {
    std::string mimeType = Image::identifyImageFileMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}
Exemple #3
0
HPDF_Image pdf_extract_image(struct bitmap *bitmap)
{
	HPDF_Image image = NULL;
	hlcache_handle *content = NULL;

	/* TODO - get content from bitmap pointer */

	if (content) {
		const char *source_data;
		unsigned long source_size;

		/*Not sure if I don't have to check if downloading has been
		finished.
		Other way - lock pdf plotting while fetching a website
		*/
		source_data = content_get_source_data(content, &source_size);

		switch(content_get_type(content)){
		/*Handle "embeddable" types of images*/
		case CONTENT_JPEG:
 			image = HPDF_LoadJpegImageFromMem(pdf_doc,
 					(const HPDF_BYTE *) source_data,
 					source_size);
 			break;

		/*Disabled until HARU PNG support will be more stable.

		case CONTENT_PNG:
			image = HPDF_LoadPngImageFromMem(pdf_doc,
					(const HPDF_BYTE *)content->source_data,
					content->total_size);
			break;*/
		default:
			break;
		}
	}

	if (!image) {
		HPDF_Image smask;
		unsigned char *img_buffer, *rgb_buffer, *alpha_buffer;
		int img_width, img_height, img_rowstride;
		int i, j;

		/*Handle pixmaps*/
		img_buffer = bitmap_get_buffer(bitmap);
		img_width = bitmap_get_width(bitmap);
		img_height = bitmap_get_height(bitmap);
		img_rowstride = bitmap_get_rowstride(bitmap);

		rgb_buffer = (unsigned char *)malloc(3 * img_width * img_height);
		alpha_buffer = (unsigned char *)malloc(img_width * img_height);
		if (rgb_buffer == NULL || alpha_buffer == NULL) {
			NSLOG(netsurf, INFO,
			      "Not enough memory to create RGB buffer");
			free(rgb_buffer);
			free(alpha_buffer);
			return NULL;
		}

		for (i = 0; i < img_height; i++)
			for (j = 0; j < img_width; j++) {
				rgb_buffer[((i * img_width) + j) * 3] =
				  img_buffer[(i * img_rowstride) + (j * 4)];

				rgb_buffer[(((i * img_width) + j) * 3) + 1] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 1];

				rgb_buffer[(((i * img_width) + j) * 3) + 2] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 2];

				alpha_buffer[(i * img_width)+j] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 3];
			}

		smask = HPDF_LoadRawImageFromMem(pdf_doc, alpha_buffer,
				img_width, img_height,
     				HPDF_CS_DEVICE_GRAY, 8);

		image = HPDF_LoadRawImageFromMem(pdf_doc, rgb_buffer,
				img_width, img_height,
     				HPDF_CS_DEVICE_RGB, 8);

		if (HPDF_Image_AddSMask(image, smask) != HPDF_OK)
			image = NULL;

		free(rgb_buffer);
		free(alpha_buffer);
	}

	return image;
}
Exemple #4
0
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = nullptr;

  if (DataUri::isDataUri(imgUrl)) {
#define HAVE_LOAD_FROM_MEM HPDF_MAJOR_VERSION > 2 || (HPDF_MAJOR_VERSION == 2 && (HPDF_MINOR_VERSION >= 2))

#if HAVE_LOAD_FROM_MEM
    DataUri uri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)&uri.data[0], 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)&uri.data[0], 
				      uri.data.size());
#else
      LOG_ERROR("drawImage: data URI support requires libharu 2.2.0 or later");
#endif
  } else {
    std::string mimeType = ImageUtils::identifyMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}