gpt4 book ai didi

C++ libPng 写一个透明背景的PNG

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:34 24 4
gpt4 key购买 nike

我已经通读了文档和示例,但在我的实现过程中,我无法做到正确。

问题:使用以下代码,写入的.png 将透明区域转换为纯白色。我试图将生成的 .png 覆盖在另一个带有颜色的图像之上,但由于不透明,底层图像不会影响输出。

我错过了什么?我尝试了各种选项,但无法正确输出。

C++代码如下:

function() {
int x, y, w, h;
double xt, yt, wt, ht;

// get image position and size
state->transform(0.0, 0.0, xt, yt);
state->transformDelta(1.0, 1.0, wt, ht);

if (wt > 0) {
x = (int) ceil(xt);
w = (int) ceil(wt);
} else {
x = (int) ceil(xt + wt);
w = (int) ceil(-wt);
}

if (ht > 0) {
y = (int) ceil(yt);
h = (int) ceil(ht);
} else {
y = (int) ceil(yt + ht);
h = (int) ceil(-ht);
}

GString* filePath = GString::format("{0:s}img-{1:d}_{2:d}.", this->outputDir.c_str(),
this->pageNum, this->imageNum);

std::ofstream imgFile;

GBool invert = isColorMapInverted(colorMap);

if (str->getKind() == strDCT && !invert) {
// dump JPEG file

filePath->append("jpg");
// open the image file
imgFile.open(filePath->getCString(), std::ios::out | std::ios::trunc | std::ios::binary);

//LOG4CPLUS_INFO(s_logger, "Writing image " << filePath->getCString());

// initialize stream
str = ((DCTStream*) str)->getRawStream();
str->reset();

// copy the stream
int c;

while ((c = str->getChar()) != EOF) {
imgFile << (Guchar) c;
}
} else {
FILE* f1;

filePath->append("png");

// open the image file
if (!(f1 = fopen(filePath->getCString(), "wb"))) {
throw std::runtime_error("Couldn't open file for writing PNG");
}

//LOG4CPLUS_INFO(s_logger, "Writing image " << filePath->getCString());

// Initialize the PNG stuff
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (!png_ptr) {
throw std::runtime_error("png_create_write_struct failed");
}

png_infop info_ptr = png_create_info_struct(png_ptr);

if (!info_ptr) {
throw std::runtime_error("png_create_info_struct failed");
}

if (setjmp(png_jmpbuf(png_ptr))) {
throw std::runtime_error("Error during init_io");
}

// Write the PNG header
png_init_io(png_ptr, f1);

if (setjmp(png_jmpbuf(png_ptr))) {
throw std::runtime_error("Error while writing png header");
}

// Set up the type of PNG image and the compression level
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
// png_set_expand(png_ptr);
// /* Set the background color to draw transparent and alpha
// images over */
// png_color_16 my_background;
// png_set_background(png_ptr, &my_background,PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
png_set_invert_alpha(png_ptr);

const png_byte bit_depth = colorMap ? 8 : 1;
const png_byte color_type = colorMap ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_GRAY;

png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

// Write the image info bytes
png_write_info(png_ptr, info_ptr);

writeStreamToPng(png_ptr, str, width, height, colorMap, maskColors);

// Write PNG end chunk
if (setjmp(png_jmpbuf(png_ptr))) {
throw std::runtime_error("Error while writing png end chunk");
}

png_write_end(png_ptr, NULL);
fclose(f1);
}

++this->imageNum;
str->close();
imgFile.close();

const int dirLength = this->outputDir.length();
GString* const fileName = new GString(filePath, dirLength, filePath->getLength() - dirLength);
const SimpleBoundingRectangle simpleBound(x, y, x + w, y + h);
const BoundingPolygon* const bounds = new BoundingPolygon(
getEffectiveBoundingPolygon(simpleBound));
SVGImage* const img = new SVGImage(x, y, w, h, fileName, this->currentClipPathIndex, bounds);
this->addNode(img);

// Do no delete fileName - SVGImage's destructor will do that
delete filePath;
}

static void writeStreamToPng(png_structp const png_ptr, Stream* const stream, const int width,
const int height, GfxImageColorMap* const colorMap, int* maskColors) {
const int numBits = colorMap ? colorMap->getBits() : 1;
const int numPixelComponents = colorMap ? colorMap->getNumPixelComps() : 1;
// If color map is specified, assume RGB, i.e. 3 bytes/pixel
const int rowSize = colorMap ? (3 * width) : ((width + 7) / 8);
png_byte* row = (png_byte*) malloc(rowSize);
png_bytep* const row_pointer = &row;

// Initialize the image stream
ImageStream* const imgStream = new ImageStream(stream, width, numPixelComponents, numBits);
imgStream->reset();

// For each line...
for (int y = 0; y < height; y++) {
// Convert stream data of each row to a PNG row
if (colorMap) {
GfxRGB rgb;
Guchar* p = imgStream->getLine();

for (int x = 0; x < width; x++) {
if (p) {
colorMap->getRGB(p, &rgb);
p += numPixelComponents;
} else {
/* No more lines in the stream - assume 0 as done in
ImageOutputDev's drawImage */
rgb.r = rgb.g = rgb.b = 0;
}

// Write the RGB pixels into the row
row[3 * x] = colToByte(rgb.r);
row[3 * x + 1] = colToByte(rgb.g);
row[3 * x + 2] = colToByte(rgb.b);
}
} else {
for (int i = 0; i < rowSize; ++i) {
row[i] = stream->getChar() ^ *maskColors;
}
}

if (setjmp(png_jmpbuf(png_ptr))) {
delete row;
delete imgStream;
throw std::runtime_error("Error in writing png rows");
}

// Write the row to the file
png_write_rows(png_ptr, row_pointer, 1);
}

delete row;
delete imgStream;
}

最佳答案

color_type = colorMap ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_GRAY

那些是不透明的 PNG 颜色类型。

尝试 PNG_COLOR_TYPE_RGBA :PNG_COLOR_TYPE_GRAY_ALPHA

关于C++ libPng 写一个透明背景的PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19945372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com