来自字节数组的 CGImage

发布于 2024-08-22 05:42:27 字数 206 浏览 10 评论 0原文

使用标准图像格式(jpeg、gif、png 等)从文件加载 CGImage 或 NSImage 非常简单。

但是,我现在需要从使用 libfreetype 生成的内存中的字节数组创建 CGImage。从格式化字节数组创建 OpenGL 纹理确实很容易,而且我可以了解如何创建要写入的 CGBitmapContext。但我似乎找不到一种简单的方法来从原始像素数组创建 CGImage。

Loading a CGImage or NSImage from a file using a standard image format (jpeg, gif, png et.) is all very simple.

However, I now need to create a CGImage from an array in bytes in memory generated using libfreetype. Its really easy to create OpenGL textures from an array of formatted bytes, and I can see how to create a CGBitmapContext to write to. But I can't seem to find an easy way to create a CGImage from a raw pixel array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚笙 2024-08-29 05:42:27

使用CFData可以使上面的代码变得更简单。
这是代码。

// raw pixel data memory of 64 * 64 pixel size

UInt8 pixelData[64 * 64 * 3];

// fill the raw pixel buffer with arbitrary gray color for test

for(size_t ui = 0; ui < 64 * 64 * 3; ui++)
  pixelData[ui] = 210;

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

CFDataRef rgbData = CFDataCreate(NULL, pixelData, 64 * 64 * 3);

CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);

CGImageRef rgbImageRef = CGImageCreate(64, 64, 8, 24, 64 * 3, colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault); 

CFRelease(rgbData);

CGDataProviderRelease(provider);

CGColorSpaceRelease(colorspace);

// use the created CGImage

CGImageRelease(rgbImageRef);

Using CFData can make the above code simpler.
Here is the code.

// raw pixel data memory of 64 * 64 pixel size

UInt8 pixelData[64 * 64 * 3];

// fill the raw pixel buffer with arbitrary gray color for test

for(size_t ui = 0; ui < 64 * 64 * 3; ui++)
  pixelData[ui] = 210;

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

CFDataRef rgbData = CFDataCreate(NULL, pixelData, 64 * 64 * 3);

CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);

CGImageRef rgbImageRef = CGImageCreate(64, 64, 8, 24, 64 * 3, colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault); 

CFRelease(rgbData);

CGDataProviderRelease(provider);

CGColorSpaceRelease(colorspace);

// use the created CGImage

CGImageRelease(rgbImageRef);
阳光下慵懒的猫 2024-08-29 05:42:27

老问题,但仍然有用! Swift 4 相当于 chansuk park 的答案:

let height = 64
let width = 64
let numComponents = 3
let numBytes = height * width * numComponents
let pixelData = [UInt8](repeating: 210, count: numBytes)
let colorspace = CGColorSpaceCreateDeviceRGB()
let rgbData = CFDataCreate(nil, pixelData, numBytes)!
let provider = CGDataProvider(data: rgbData)!
let rgbImageRef = CGImage(width: width,
                          height: height,
                          bitsPerComponent: 8,
                          bitsPerPixel: 8 * numComponents,
                          bytesPerRow: width * numComponents,
                          space: colorspace,
                          bitmapInfo: CGBitmapInfo(rawValue: 0),
                          provider: provider,
                          decode: nil,
                          shouldInterpolate: true,
                          intent: CGColorRenderingIntent.defaultIntent)!
// Do something with rgbImageRef, or for UIImage:
let outputImage = UIImage(cgImage: rgbImageRef)

Old question, but still useful! Swift 4 equivalent of chansuk park's answer:

let height = 64
let width = 64
let numComponents = 3
let numBytes = height * width * numComponents
let pixelData = [UInt8](repeating: 210, count: numBytes)
let colorspace = CGColorSpaceCreateDeviceRGB()
let rgbData = CFDataCreate(nil, pixelData, numBytes)!
let provider = CGDataProvider(data: rgbData)!
let rgbImageRef = CGImage(width: width,
                          height: height,
                          bitsPerComponent: 8,
                          bitsPerPixel: 8 * numComponents,
                          bytesPerRow: width * numComponents,
                          space: colorspace,
                          bitmapInfo: CGBitmapInfo(rawValue: 0),
                          provider: provider,
                          decode: nil,
                          shouldInterpolate: true,
                          intent: CGColorRenderingIntent.defaultIntent)!
// Do something with rgbImageRef, or for UIImage:
let outputImage = UIImage(cgImage: rgbImageRef)
横笛休吹塞上声 2024-08-29 05:42:27

您可以创建一个CGDataProvider,并让 CG 从您的提供程序请求必要的数据,而不是写入图像缓冲区。

这是一个非常简单的示例,它生成大小为 64x64 的黑色 CGImage

CGDataProviderSequentialCallbacks callbacks;
callbacks.getBytes = getBytes;

CGDataProviderRef provider = CGDataProviderCreateSequential(NULL, &callbacks);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGImageRef img = CGImageCreate(64,                         // width
                               64,                         // height
                               8,                          // bitsPerComponent
                               24,                         // bitsPerPixel
                               64*3,                       // bytesPerRow
                               space,                      // colorspace
                               kCGBitmapByteOrderDefault,  // bitmapInfo
                               provider,                   // CGDataProvider
                               NULL,                       // decode array
                               NO,                         // shouldInterpolate
                               kCGRenderingIntentDefault); // intent

CGColorSpaceRelease(space);
CGDataProviderRelease(provider);

// use the created CGImage

CGImageRelease(img);

getBytes 的定义如下:

size_t getBytes(void *info, void *buffer, size_t count) {
    memset(buffer, 0x00, count);
    return count;
}

当然,您需要实现其他回调(skipForwardrewindreleaseInfo),并使用info 的正确结构或对象。

有关详细信息,请查看 CGImageCGDataProvider 参考。

You can create a CGDataProvider, and let CG request the necessary data from your provider, instead of writing to an image buffer.

Here's a very simple example that generates a black CGImage of size 64x64.

CGDataProviderSequentialCallbacks callbacks;
callbacks.getBytes = getBytes;

CGDataProviderRef provider = CGDataProviderCreateSequential(NULL, &callbacks);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGImageRef img = CGImageCreate(64,                         // width
                               64,                         // height
                               8,                          // bitsPerComponent
                               24,                         // bitsPerPixel
                               64*3,                       // bytesPerRow
                               space,                      // colorspace
                               kCGBitmapByteOrderDefault,  // bitmapInfo
                               provider,                   // CGDataProvider
                               NULL,                       // decode array
                               NO,                         // shouldInterpolate
                               kCGRenderingIntentDefault); // intent

CGColorSpaceRelease(space);
CGDataProviderRelease(provider);

// use the created CGImage

CGImageRelease(img);

and getBytes is defined like this:

size_t getBytes(void *info, void *buffer, size_t count) {
    memset(buffer, 0x00, count);
    return count;
}

of course, you will want to implement the other callbacks (skipForward, rewind, releaseInfo), and use a proper structure or object for info.

For more information, check out the CGImage and CGDataProvider references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文