void loadJPEG_color( const char *path, cv::Mat &image ) { CGDataProviderRef jpegDataProvider = CGDataProviderCreateWithFilename( path ); CGImageRef imageRef = CGImageCreateWithJPEGDataProvider( jpegDataProvider, NULL, false, kCGRenderingIntentDefault ); copyImageData_color( imageRef, image ); CGImageRelease( imageRef ); CGDataProviderRelease( jpegDataProvider ); }
int main(int argc, char** argv) { if (argc <= 1 || !*(argv[1]) || 0 == strcmp(argv[1], "-")) { fprintf(stderr, "usage:\n\t%s INPUT_PDF_FILE_PATH [OUTPUT_PNG_PATH]\n", argv[0]); return 1; } const char* output = argc > 2 ? argv[2] : nullptr; CGDataProviderRef data = CGDataProviderCreateWithFilename(argv[1]); ASSERT(data); CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data); CGDataProviderRelease(data); ASSERT(pdf); CGPDFPageRef page = CGPDFDocumentGetPage(pdf, PAGE); ASSERT(page); CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); int w = (int)CGRectGetWidth(bounds); int h = (int)CGRectGetHeight(bounds); CGBitmapInfo info = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); ASSERT(cs); std::unique_ptr<uint32_t[]> bitmap(new uint32_t[w * h]); memset(bitmap.get(), 0xFF, 4 * w * h); CGContextRef ctx = CGBitmapContextCreate(bitmap.get(), w, h, 8, w * 4, cs, info); ASSERT(ctx); CGContextDrawPDFPage(ctx, page); CGPDFDocumentRelease(pdf); CGImageRef image = CGBitmapContextCreateImage(ctx); ASSERT(image); CGDataConsumerCallbacks procs; procs.putBytes = [](void* f, const void* buf, size_t s) { return fwrite(buf, 1, s, (FILE*)f); }; procs.releaseConsumer = [](void* info) { fclose((FILE*)info); }; FILE* ofile = (!output || !output[0] || 0 == strcmp(output, "-")) ? stdout : fopen(output, "wb"); ASSERT(ofile); CGDataConsumerRef consumer = CGDataConsumerCreate(ofile, &procs); ASSERT(consumer); CGImageDestinationRef dst = CGImageDestinationCreateWithDataConsumer(consumer, kUTTypePNG, 1, nullptr); CFRelease(consumer); ASSERT(dst); CGImageDestinationAddImage(dst, image, nullptr); ASSERT(CGImageDestinationFinalize(dst)); CFRelease(dst); CGImageRelease(image); CGColorSpaceRelease(cs); CGContextRelease(ctx); return 0; }
UncompressedImage LoadJPEGFile(Path path) { CGDataProviderRef data_provider = CGDataProviderCreateWithFilename(path.c_str()); if (nullptr == data_provider) return UncompressedImage(); CGImageRef image = CGImageCreateWithJPEGDataProvider( data_provider, nullptr, false, kCGRenderingIntentDefault); UncompressedImage result = CGImageToUncompressedImage(image); if (nullptr != image) CFRelease(image); CFRelease(data_provider); return result; }
int main (int argc, const char * argv[]) { if(argc >= 2) { CGPDFDocumentRef doc = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(argv[1])); size_t pages = CGPDFDocumentGetNumberOfPages(doc); printf("%lu pages\n", pages); for(size_t i = 1; i <= pages; i++) { char filename[1024]; snprintf(filename, 1024, "%s.%03lu.png", argv[1], i); printf("writing file: %s\n", filename); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, SIZE, SIZE, 8, 4 * SIZE, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); CGContextDrawPDFDocument(context, CGRectMake(0, 0, SIZE, SIZE), doc, (int)i); CGImageRef image = CGBitmapContextCreateImage(context); CGImageDestinationRef dest = CGImageDestinationCreateWithURL(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingASCII), kCFURLPOSIXPathStyle, false), kUTTypePNG, 1, NULL); CGImageDestinationAddImage(dest, image, NULL); CGImageDestinationFinalize(dest); CGImageRelease(image); CGContextRelease(context); } CGPDFDocumentRelease(doc); } else { printf("pdf2png [filename]\n"); return 1; } return 0; }
void process_1_image (args cli_flags, char *files) { char *out_file_name = get_out_filename (files, cli_flags); if (file_exists (out_file_name)) { printf ("| Output file %s already exists. skipping... ", out_file_name); return; } // Origional Image Properties struct img_prop o = (img_prop) malloc (sizeof (image_properties)); // set all the vales in the imapg properties struct to -1 null_ip (o); // Create a data provider CGDataProviderRef source_image_provider = CGDataProviderCreateWithFilename (files); // Check for a null returned value if (source_image_provider == NULL) { // something went wrong printf ("error: Couldn't create CGDataProvider from URL.\n"); exit (0); } // get the information from the image exif here o->image_rot = get_exif_rot (source_image_provider); // Create the image in memory from the JPEG data CGImageRef source_image = CGImageCreateWithJPEGDataProvider (source_image_provider, NULL, no, kCGRenderingIntentDefault); /********************************************/ /* Getting the colour space **/ o->colorSpace = CGImageGetColorSpace(source_image); /********************************************/ // populate the image info struct pop_img_props (source_image, o); // create a data provider from the decoded JPEG data CGDataProviderRef image_data_provider = CGImageGetDataProvider (source_image); // Create a pointer to the data section of the image in memory CFDataRef source_data_ptr = CGDataProviderCopyData (image_data_provider); // The vImage_Buffers we will use vImage_Buffer *vImage_source = (vImage_Buffer*) malloc (sizeof (vImage_Buffer)); // Check for NULL if (NULL == vImage_source) { printf ("Cannot malloc vImage_source buffer\n"); exit (0); } if (o->bits_ppixel == 24) { // convert from 24bit to 32bit by adding the alpha channel. source_data_ptr = convert24_32bit (source_data_ptr, o); } // Setup the vImage Buffer for the image setupBuffer (vImage_source, o->image_h, o->image_w, o->bytes_row); // Assign the data to the vImage Buffer for the source vImage_source->data = (void *) CFDataGetBytePtr (source_data_ptr); // Check for NULL if (vImage_source->data == NULL) printf ("Unable to get the vimage.data pointer\n"); if (o->image_rot != 1 && o->image_rot != 4 && o->image_rot != 2) // rotate the image rotate_image (vImage_source, o, NULL); // flip the image if (o->image_rot == 2 || o->image_rot == 4 || o->image_rot == 7 || o->image_rot == 5) flip_image (vImage_source, o, NULL); // Resize the images resize_image (vImage_source, o, cli_flags); // Create a colour space to be compared against CGColorSpaceRef rgb = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); if (NULL == rgb) { fprintf(stderr, "Unable to create the reference colourspace.\n"); exit(0); } // Convert the colourspace to RGB if (!CFEqual(rgb, o->colorSpace) && !cli_flags->disableCC) { vImage_source->data = convert_space(vImage_source->data, o->image_w, o->image_h); if (NULL == vImage_source->data) exit(0); } // release the reference colour space CGColorSpaceRelease(rgb); // save the image save_image (vImage_source, o, cli_flags->quality, out_file_name); // Release the source provider CGDataProviderRelease (source_image_provider); source_image_provider = NULL; // Release the source image CGImageRelease (source_image); source_image = NULL; free(source_data_ptr); // Free the filename created by get_out_filename () free (out_file_name); out_file_name = NULL; // free the image properties free (o); o = NULL; // if there is info in the buffer if (vImage_source->data != NULL) { free (vImage_source->data); vImage_source->data = NULL; } // free the buffer free (vImage_source); vImage_source = NULL; } // Process 1 image
/* ================= fs_get_data_provider ================= */ CGDataProviderRef fs_get_data_provider (const char *filename) { return CGDataProviderCreateWithFilename(fs_get_resource_path(filename)); }