static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream,
                                                        CFStringRef type) {
    CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream);
    if (NULL == consumer) {
        return NULL;
    }
    SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer);

    return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL);
}
err_t MacCGEncoder::writeImage(const Image& image)
{
  Image tmp;
  if (image.isEmpty())
    return ERR_IMAGE_INVALID_SIZE;

  err_t err = ERR_OK;

  CGDataConsumerRef cgConsumer = NULL;
  CGImageDestinationRef cgDestination = NULL;
  CGImageRef cgImage = NULL;

  cgConsumer = CGDataConsumerCreate(this, &_consumerCallbacks);
  if (FOG_IS_NULL(cgConsumer))
  {
    err = ERR_RT_OUT_OF_MEMORY;
    goto _End;
  }

  cgDestination = CGImageDestinationCreateWithDataConsumer(cgConsumer,
    static_cast<MacCGCodecProvider*>(_provider)->getUTType(), 1, NULL);
  CFRelease(cgConsumer);

  if (FOG_IS_NULL(cgDestination))
  {
    err = ERR_RT_OUT_OF_MEMORY;
    goto _End;
  }

  if (image.getType() == IMAGE_TYPE_MAC_CG)
    cgImage = static_cast<CGImageRef>(const_cast<Image&>(image).getHandle());

  if (FOG_IS_NULL(cgImage))
  {
    err = image.toCGImage(&cgImage);
    if (FOG_IS_ERROR(err))
      goto _End;
  }

  CGImageDestinationAddImage(cgDestination, cgImage, NULL);
  if (!CGImageDestinationFinalize(cgDestination))
  {
    err = ERR_IO_CANT_WRITE;
  }

_End:
  CFRelease(cgDestination);

  if (err == ERR_OK)
    updateProgress(1.0f);
  return err;
}
Beispiel #3
0
static void quartzgen_end_job(GVJ_t *job)
{
	CGContextRef context = (CGContextRef)job->context;
	if (!job->external_context) {
		switch (job->device.id) {
		
		case FORMAT_PDF:
			/* save the PDF */
			CGPDFContextClose(context);
			break;
				
		case FORMAT_CGIMAGE:
			break;
			
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1040
		default:	/* bitmap formats */
			{
				/* create an image destination */
				CGDataConsumerRef data_consumer = CGDataConsumerCreate(job, &device_data_consumer_callbacks);
				CGImageDestinationRef image_destination = CGImageDestinationCreateWithDataConsumer(data_consumer, format_uti[job->device.id], 1, NULL);
				
				/* add the bitmap image to the destination and save it */
				CGImageRef image = CGBitmapContextCreateImage(context);
				CGImageDestinationAddImage(image_destination, image, NULL);
				CGImageDestinationFinalize(image_destination);
				
				/* clean up */
				if (image_destination)
					CFRelease(image_destination);
				CGImageRelease(image);
				CGDataConsumerRelease(data_consumer);
			}
			break;
#endif
		}
		CGContextRelease(context);
	}
	else if (job->device.id == FORMAT_CGIMAGE)
	{
		/* create an image and save it where the window field is, which was set to the passed-in context at begin job */
		*((CGImageRef*)job->window) = CGBitmapContextCreateImage(context);
#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 20000
		void* context_data = CGBitmapContextGetData(context);
		size_t context_datalen = CGBitmapContextGetBytesPerRow(context) * CGBitmapContextGetHeight(context);
#endif
		CGContextRelease(context);
#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 20000
		munmap(context_data, context_datalen);
#endif
	}
}
Beispiel #4
0
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;
}