- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 CGImageDestination 编写带有索引颜色和 alpha 值 (RGBA) 的 PNG 文件。就PNG specification而言这是一个颜色类型为 3 和 tRNS block 的图像。我从程序的单独部分获取索引矩阵和附加调色板。
我已经成功地编写了没有 alpha 值的索引图像(即只有不透明颜色)。如果我按照文档添加 alpha 值,生成的图像将不再使用颜色类型 3(调色板),而是变成完整的 RGBA 图像(颜色类型 6)。在实际的 PNG 文件中,alpha 值本质上附加到调色板条目。似乎没有办法创建显式 RGBA 色彩空间。
我可以直接使用libpng或其他PNG库来实现所需的编码。但是,如果可能的话,我宁愿只使用 CGImage 和 friend 。
TL;DR:如何使用 CGImage/CGImageDestination 生成带有 alpha 值的索引 PNG 文件?
两年前就已经提出了一个非常类似的问题( Create and write paletted RGBA PNG using NSImage ),但它谈到了 Cocoa 并且没有收到解决方案。
我的测试代码如下。如果在编译时定义了 USE_ALPHA ,则生成的图像将使用 alpha 值。否则就不会。
/* Compilation command (tested with Xcode 4.6.3 on Mac OS X 10.8.4):
*
* clang++ -std=c++11 -stdlib=libc++ -framework ApplicationServices -DUSE_ALPHA -o demo demo.cpp
*/
#include <utility>
#include <vector>
#include <array>
#include <ApplicationServices/ApplicationServices.h>
/* R, G, B */
const std::vector<std::array<std::uint8_t, 3>> colortable = {
{ 0xF0, 0x10, 0x10 },
{ 0x20, 0xF0, 0x20 },
{ 0x30, 0x30, 0xF0 },
{ 0xF0, 0xF0, 0x40 },
{ 0x50, 0xF0, 0xF0 },
};
/* Colour index, alpha */
const std::vector<std::uint8_t> data = {
3, 0x30,
2, 0x20,
1, 0x10,
0, 0x00,
4, 0x40,
3, 0x30,
2, 0x20,
1, 0x10,
};
void safeCfRelease(const ::CFTypeRef ref) {
if (ref != nullptr) {
::CFRelease(ref);
}
}
int main() {
const std::shared_ptr<const ::__CFURL>
url(::CFURLCreateWithFileSystemPath(nullptr, CFSTR("out.png"),
::kCFURLPOSIXPathStyle, false),
safeCfRelease);
const std::shared_ptr<::CGDataProvider>
prov_data(::CGDataProviderCreateWithData(nullptr, &data[0], data.size(), nullptr),
safeCfRelease);
const std::shared_ptr<::CGColorSpace>
gray(::CGColorSpaceCreateDeviceGray(), safeCfRelease),
rgb(::CGColorSpaceCreateDeviceRGB(), safeCfRelease),
indexed(::CGColorSpaceCreateIndexed(rgb.get(), colortable.size(),
reinterpret_cast<const unsigned char *>(&colortable[0])),
safeCfRelease);
const auto bitmap_info =
#ifdef USE_ALPHA
::kCGImageAlphaLast |
#else
::kCGImageAlphaNoneSkipLast |
#endif
::kCGBitmapByteOrderDefault;
const std::shared_ptr<::CGImage>
image(::CGImageCreate(/* width */ 4,
/* height */ 2,
/* bitsPerComponent */ 8,
/* bitsPerPixel */ 16,
/* bytesPerRow */ 8,
indexed.get(),
bitmap_info,
prov_data.get(), nullptr, false,
::kCGRenderingIntentDefault),
safeCfRelease);
const std::shared_ptr<::CGImageDestination>
dest(::CGImageDestinationCreateWithURL(url.get(), ::kUTTypePNG, 1, nullptr),
safeCfRelease);
::CGImageDestinationAddImage(dest.get(), image.get(), nullptr);
::CGImageDestinationFinalize(dest.get());
}
file
给出的不透明颜色描述(这也适用于使用 ImageMagick 生成的具有 tRNS block 的图像):
PNG image data, 4 x 2, 8-bit colormap, non-interlaced
使用 CGImageDestination 的 alpha 值描述:
PNG image data, 4 x 2, 8-bit/color RGBA, non-interlaced
最佳答案
不知道这是否有效,但是您尝试过 UIImagePNGRepresentation() 吗?
CGImage cgimg; //initialize this with the CGImage that you want to be a PNG
UIImage* image = [UIImage imageWithCGImage:cgimg];
NSData* pngData = UIImagePNGRepresentation(image);
CGImageRelease(cgimg);
希望这有帮助!
关于macos - Mac OS X/iOS : How to write indexed PNG image using RGBA colours through CGImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18158135/
在 golang 颜色包中,有一种方法可以从 RGBA 中获取 r,g,b,a 值对象: func (c RGBA) RGBA() (r, g, b, a uint32) { r = uint
我有一个 OpenGL RGBA 纹理,并使用帧缓冲区对象将另一个 RGBA 纹理传输到它上面。问题是,如果我使用通常的混合函数 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MI
假设我有: 和一些CSS: .dark {background:rgba(10,10,10,0.8);} .lite {background:rgba(200,200,200,0.5)
我正在寻找一种更有效的方法,将在预乘色彩空间中存储为 double 的 RGBA 转换为 8 位整数/ channel RGBA 非预乘色彩空间。这对我的图像处理来说是一项巨大的成本。 对于一个 ch
我需要将 PNG H*W*4 rgba 图像转换为形状为 H*W*3 的 rgb 图像. 我可以做到这一点,但是当我保存它时,图像会再次保存为H*W*4这是代码片段: for idx, image i
我有一个 SCSS 文件,这里是它的一部分。SCSS: $red: 200; $green: 200; $blue: 200; $opa: 1; .th-layout-class{ backg
我尝试使用 libpng 库,但遇到了问题。 我现在正在使用 CLion,并尝试在我的项目中包含这个 libpng 库。问题在于配置该库以在我的项目中使用它而不是使用该库。我认为我可以将 libpng
csslint 警告回退背景(十六进制或 RGB)应该在 RGBA 背景之前。"evidence="background: rgba(0, 0, 0, 0.8);/* FF3+,Saf3+,Opera
你应该使用 rgba(0, 0, 0, 0) 还是 rgba(255, 255, 255, 0) 在 CSS 中实现透明度? 各自的优缺点是什么? 最佳答案 rgba() 函数的最后一个参数是“alp
掩码 CSS #mask { background-color: rgba(0, 0, 0, 0.8); position: fixed; left: 0; top:
我现在有类型不清楚的图片,他可能是jpeg gif,我想将其转换为image.Image,我该怎么办? 下面是我的代码 func imageToPng(img image.Image) (image.
我正在尝试创建导航栏,当用户不在页面顶部时,该导航栏会淡出至 80% 的不透明度。我已经设法让滚动和相应的 CSS 更改正常工作,但我不确定是否会褪色。我尝试过 .fadeTo() ,它似乎将不透明度
我必须自定义一个 iOS 应用,指南说: Please don’t use RGBA values in 0 to 255 decimal notation, but use 0.0 to 1.0 a
HTML 代码: Hello CSS 代码: .item img:hover { background: rgba(0, 0, 0, 0.8) } 当我将鼠标悬停在图像上
我在深色背景上为字母添加了 rgba 颜色,并且在 Chrome (67) 中它周围有一个非常小的白色轮廓。我强加了一个 outline: none 但没有任何改变。 body { backgrou
我想通过 CCS3 创建简单的悬停效果,例如默认图像是黑白的,但是当我悬停到该图像时,将显示该图像的实际颜色。 请帮帮我 最佳答案 您可以使用新属性 filter 但浏览器支持不是很深(参见 http
我正在尝试创建一个与图像大小完全匹配的背景颜色叠加层,并在该叠加层上显示文本。但是背景颜色也覆盖了文字,希望有人帮我解决这个问题。 HTML
我想创建像这张图片这样的输入 enter image description here 我从 stackoverflow 看到了这个链接 Skew Input Border Without Skewi
问题是 rgba 透明度。我应该改变它的背景并使其透明。但是,它只是改变了背景颜色,并没有使其透明。 我经常遇到这个问题,有时有效,有时无效。 我试图弄清楚为什么有时它不起作用,即使我的编码方式与其他
我只是想为图像添加背景颜色 (rgba),但不起作用。 我的 CSS 是: section{ width:100%; height:400px; background: url(../img/back
我是一名优秀的程序员,十分优秀!