#include "utils.h" #include "libpng/png.h" #define PDF_PRINT_BUFSIZE 1024 #define PDF_PRINTF(f) do { \ sprintf(pdf_print_buf, f); \ mv2pdfbuf(); \ } while (0) #define PNG_PTR image_ptr->png_ptr #define INFO_PTR image_ptr->info_ptr typedef struct _image_info { int ref_count; png_structp png_ptr; png_infop info_ptr; } image_info; char pdf_print_buf[PDF_PRINT_BUFSIZE]; void mv2pdfbuf() { char *p = pdf_print_buf; if (sizeof(pdfbuf) - pdfptr <= PDF_PRINT_BUFSIZE) pdfflush(); while (*p) pdfbuf[pdfptr++] = *p++; } integer readimageinfo(FILE *infile) { image_info *image_ptr = MALLOC(image_info, 1); char buf[255]; image_ptr->ref_count = 1; getstr(buf, bmakenamestring(infile)); flushstring(); filename = buf; if ((PNG_PTR = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) FAIL("png_create_read_struct() failed"); if ((INFO_PTR = png_create_info_struct(PNG_PTR)) == NULL) FAIL("png_create_info_struct() failed"); if (setjmp (PNG_PTR->jmpbuf)) FAIL("setjmp() failed"); png_init_io(PNG_PTR, infile); png_read_info(PNG_PTR, INFO_PTR); if (INFO_PTR->color_type & PNG_COLOR_MASK_ALPHA) FAIL("alpha channel not supported"); if (INFO_PTR->interlace_type != 0) FAIL("interlace type not supported"); if (INFO_PTR->bit_depth == 16) { png_set_strip_16(PNG_PTR); WARN("can't handle 16 bits per channel, strip down to 8 bits"); } png_read_update_info(PNG_PTR, INFO_PTR); return (integer)image_ptr; } void writeimage(image_info *image_ptr) { int i, j; png_bytep row = MALLOC(png_byte, INFO_PTR->rowbytes); PDF_PRINTF("/Type /XObject\n/Subtype /Image\n/Width %u\n/Height %u\n/BitsPerComponent %u\n" AND INFO_PTR->width AND INFO_PTR->height AND INFO_PTR->bit_depth); PDF_PRINTF("/ColorSpace "); switch (INFO_PTR->color_type) { case PNG_COLOR_TYPE_PALETTE: PDF_PRINTF("[/Indexed /DeviceRGB %u %u 0 R]\n" AND INFO_PTR->num_palette - 1 AND objcount + 1); break; case PNG_COLOR_TYPE_GRAY: PDF_PRINTF("/DeviceGray\n"); break; default: PDF_PRINTF("/DeviceRGB\n"); } PDF_PRINTF("/Length %u\n" AND INFO_PTR->rowbytes*INFO_PTR->height); if (getpdfcompresslevel() > 0) PDF_PRINTF("/Filter /FlateDecode\n"); PDF_PRINTF(">>\nstream\n"); if (getpdfcompresslevel() > 0) startcompress(); for (i = 0; i < INFO_PTR->height; i++) { png_read_row(PNG_PTR, row, NULL); for (j = 0; j < INFO_PTR->rowbytes; j++) { pdfbuf[pdfptr++] = row[j]; if (pdfptr == pdfbufsize) pdfflush(); } } if (getpdfcompresslevel() > 0) finishcompress(); PDF_PRINTF("endstream\nendobj\n"); if (INFO_PTR->color_type == PNG_COLOR_TYPE_PALETTE) { pdfnewdict(0, 0); PDF_PRINTF("/Length %u\n" AND INFO_PTR->num_palette*3); if (getpdfcompresslevel() > 0) PDF_PRINTF("/Filter /FlateDecode\n"); PDF_PRINTF(">>\nstream\n"); if (3*INFO_PTR->num_palette + pdfptr >= pdfbufsize) pdfflush(); if (getpdfcompresslevel() > 0) startcompress(); for (i = 0; i < INFO_PTR->num_palette; i++) { pdfbuf[pdfptr++] = INFO_PTR->palette[i].red; pdfbuf[pdfptr++] = INFO_PTR->palette[i].green; pdfbuf[pdfptr++] = INFO_PTR->palette[i].blue; } if (getpdfcompresslevel() > 0) finishcompress(); PDF_PRINTF("endstream\nendobj\n"); } if (INFO_PTR->color_type == PNG_COLOR_TYPE_GRAY) pdfimageb = true; else pdfimagec = true; free(row); pdfflush(); } void addimageref(image_info *image_ptr) { image_ptr->ref_count++; } void deleteimageref(image_info *image_ptr) { if (--image_ptr->ref_count == 0) { fclose(PNG_PTR->io_ptr); png_destroy_read_struct(&(PNG_PTR), &(INFO_PTR), NULL); free(image_ptr); } } integer imagewidth(image_info *image_ptr) { return INFO_PTR->width; } integer imageheight(image_info *image_ptr) { return INFO_PTR->height; } integer imagexres(image_info *image_ptr) { if (INFO_PTR->valid & PNG_INFO_pHYs) return INFO_PTR->x_pixels_per_unit; return 0; } integer imageyres(image_info *image_ptr) { if (INFO_PTR->valid & PNG_INFO_pHYs) return INFO_PTR->y_pixels_per_unit; return 0; }