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; }
int main (int argc, char **argv) { HPDF_Doc pdf; HPDF_Font font; HPDF_Page page; char fname[256]; HPDF_Image image; HPDF_REAL x, y; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } /* error-handler */ if (setjmp(env)) { HPDF_Free (pdf); return 1; } HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); /* create default-font */ font = HPDF_GetFont (pdf, "Helvetica", NULL); /* add a new page object. */ page = HPDF_AddPage (pdf); HPDF_Page_SetWidth (page, 172); HPDF_Page_SetHeight (page, 80); HPDF_Page_BeginText (page); HPDF_Page_SetFontAndSize (page, font, 20); HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70); HPDF_Page_ShowText (page, "RawImageDemo"); HPDF_Page_EndText (page); /* load RGB raw-image file. */ #ifndef __WIN32__ image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_rgb.dat", 32, 32, HPDF_CS_DEVICE_RGB); #else image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_rgb.dat", 32, 32, HPDF_CS_DEVICE_RGB); #endif x = 20; y = 20; /* Draw image to the canvas. (normal-mode with actual size.)*/ HPDF_Page_DrawImage (page, image, x, y, 32, 32); /* load GrayScale raw-image file. */ #ifndef __WIN32__ image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_gray.dat", 32, 32, HPDF_CS_DEVICE_GRAY); #else image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_gray.dat", 32, 32, HPDF_CS_DEVICE_GRAY); #endif x = 70; y = 20; /* Draw image to the canvas. (normal-mode with actual size.)*/ HPDF_Page_DrawImage (page, image, x, y, 32, 32); /* load GrayScale raw-image (1bit) file from memory. */ image = HPDF_LoadRawImageFromMem (pdf, RAW_IMAGE_DATA, 32, 32, HPDF_CS_DEVICE_GRAY, 1); x = 120; y = 20; /* Draw image to the canvas. (normal-mode with actual size.)*/ HPDF_Page_DrawImage (page, image, x, y, 32, 32); /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }