简体   繁体   English

C ++ libPng用透明背景编写PNG

[英]C++ libPng write a PNG with transparent background

I have read through the documentation and examples but with my implementation, I couldn't get it right. 我已经阅读了文档和示例,但是在实现过程中,我做得不好。

Problem: With the following code, the .png written has the transparent region converted into plain white. 问题:使用以下代码,编写的.png会将透明区域转换为纯白色。 I am trying to overlay the resultant .png on top of another image with colors, but because of opaqueness, the underlying image doesn't contribute to output. 我正在尝试将生成的.png用颜色叠加在另一张图像的顶部,但是由于不透明,基础图像对输出没有帮助。

What am I missing? 我想念什么? I have tried various options but couldn't get the output right. 我尝试了各种选项,但无法获得正确的输出。

C++ Code below: 下面的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 ? color_type = colorMap? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_GRAY PNG_COLOR_TYPE_RGB:PNG_COLOR_TYPE_GRAY

Those are opaque PNG color types. 这些是不透明的PNG颜色类型。

Try PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_GRAY_ALPHA 尝试PNG_COLOR_TYPE_RGBA:PNG_COLOR_TYPE_GRAY_ALPHA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM