1

最近、前回の投稿でビットマップの問題を解決しました。今、私は別の画像の問題に戻っています。今回はPNGです。

PNGファイルの読み取りと書き込みにLibPngを使用しています。各ピクセルのBGRA情報を保持する構造体があります。ピクセルが正しくRGBA形式で保存されていることを私は知っています。そこで、BとRのスワッピングを指定しました。これで問題ありません。どういうわけか画像をめくっていると思いますが、よくわかりません。

24ビットPNGを32ビットPNGに、またはその逆に変換しようとすると、問題が発生します。現在、私は24から24と32から32をうまく行うことができます。

皆さんは私のコードを調べて、24から32に変換しようとしたときに私が間違っていることを教えてもらえますか?

以下のコードは、同じPNGをロードしてディスクに書き戻すために機能します。皆さんがコンパイルして必要かどうかを確認できるように、この1つのファイルにすべてを含めるようにしました。

#include <iostream>
#include <vector>
#include <fstream>
#include <stdexcept>
#include "Libraries/LibPng/Include/png.h"

typedef union RGB
{
    uint32_t Color;
    struct
    {
        unsigned char B, G, R, A;
    } RGBA;
} *PRGB;


std::vector<RGB> Pixels;
uint32_t BitsPerPixel, width, height;
int bitdepth, colortype, interlacetype, channels;


void ReadFromStream(png_structp PngPointer, png_bytep Data, png_size_t Length) //For reading using ifstream rather than FILE*
{
    std::ifstream *Stream = (std::ifstream*)png_get_io_ptr(PngPointer);
    Stream->read((char*)Data, Length);
}

void WriteToStream(png_structp PngPointer, png_bytep Data, png_size_t Length)  //For writing using ofstream rather than FILE*
{
    std::ofstream *Stream = (std::ofstream*)png_get_io_ptr(PngPointer);
    Stream->write((char*)Data, Length);
}


void Load(const char* FilePath)
{
    std::fstream hFile(FilePath, std::ios::in | std::ios::binary);
    if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");}

    unsigned char Header[8] = {0};
    hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header));

    if (png_sig_cmp(Header, 0, 8))
    {
        hFile.close();
        throw std::invalid_argument("Error: Invalid File Format. Required: Png.");
    }

    png_structp PngPointer = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if (!PngPointer)
    {
        hFile.close();
        throw std::runtime_error("Error: Cannot Create Read Structure.");
    }

    png_infop InfoPointer = png_create_info_struct(PngPointer);
    if (!InfoPointer)
    {
        hFile.close();
        png_destroy_read_struct(&PngPointer, nullptr, nullptr);
        throw std::runtime_error("Error: Cannot Create InfoPointer Structure.");
    }

    png_infop EndInfo = png_create_info_struct(PngPointer);
    if (!EndInfo)
    {
        hFile.close();
        png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
        throw std::runtime_error("Error: Cannot Create EndInfo Structure.");
    }

    if (setjmp(png_jmpbuf(PngPointer)))
    {
        hFile.close();
        png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
        throw std::runtime_error("Error: Cannot Set Jump Pointer.");
    }

    png_set_sig_bytes(PngPointer, sizeof(Header));
    png_set_read_fn(PngPointer, reinterpret_cast<void*>(&hFile), ReadFromStream);
    png_read_info(PngPointer, InfoPointer);


    //This is where I start getting the info and storing it..

    channels = png_get_channels(PngPointer, InfoPointer);
    png_get_IHDR(PngPointer, InfoPointer, &width, &height, &bitdepth, &colortype, &interlacetype, nullptr, nullptr);
    png_set_strip_16(PngPointer);
    png_set_packing(PngPointer);

    switch (colortype)
    {
        case PNG_COLOR_TYPE_GRAY:
        {
            png_set_expand(PngPointer);
            break;
        }

        case PNG_COLOR_TYPE_GRAY_ALPHA:
        {
            png_set_gray_to_rgb(PngPointer);
            break;
        }

        case PNG_COLOR_TYPE_RGB:
        {
            png_set_bgr(PngPointer);
            BitsPerPixel = 24;
            break;
        }

        case PNG_COLOR_TYPE_RGBA:
        {
            png_set_bgr(PngPointer);
            BitsPerPixel = 32;
            break;
        }

        default: png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr); throw std::runtime_error("Error: Png Type not supported."); break;
    }

    //Store the new data.

    png_read_update_info(PngPointer, InfoPointer);
    channels = png_get_channels(PngPointer, InfoPointer);
    png_get_IHDR(PngPointer, InfoPointer, &width, &height, &bitdepth, &colortype, &interlacetype, nullptr, nullptr);

    Pixels.resize(width * height);
    std::vector<unsigned char*> RowPointers(height);
    unsigned char* BuffPos = reinterpret_cast<unsigned char*>(Pixels.data());

    //Set the row pointers to my Pixels vector. This way, the image is stored upright in my vector<BGRA> Pixels.
    //I think this is flipping it for some reason :S

    for (size_t I = 0; I < height; ++I)
    {
        RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
    }

    png_read_image(PngPointer, RowPointers.data());                 //Get the pixels as BGRA and store it in my struct vector.
    png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
    hFile.close();

    std::cout<<"Loading Parameters..."<<std::endl;
    std::cout<<"Bits:  "<<BitsPerPixel<<std::endl;
    std::cout<<"Depth: "<<bitdepth<<std::endl;
    std::cout<<"CType: "<<colortype<<std::endl;
}

void Save(const char* FilePath)
{
    std::fstream hFile(FilePath, std::ios::out | std::ios::binary);
    if (!hFile.is_open()) {throw std::invalid_argument("Cannot open file for writing.");}

    png_structp PngPointer = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
    if (!PngPointer)
    {
        hFile.close();
        throw std::runtime_error("Error: Cannot Create Write Structure.");
    }

    png_infop InfoPointer = png_create_info_struct(PngPointer);
    if (!InfoPointer)
    {
        hFile.close();
        png_destroy_write_struct(&PngPointer, nullptr);
        throw std::runtime_error("Error: Cannot Create InfoPointer Structure.");
    }

    if (setjmp(png_jmpbuf(PngPointer)))
    {
        hFile.close();
        png_destroy_write_struct(&PngPointer, &InfoPointer);
        throw std::runtime_error("Error: Cannot Set Jump Pointer.");
    }

    std::cout<<"\nSaving Parameters..."<<std::endl;
    std::cout<<"Bits:  "<<BitsPerPixel<<std::endl;
    std::cout<<"Depth: "<<bitdepth<<std::endl;
    std::cout<<"CType: "<<colortype<<std::endl;

    //This is where I set all the Information..

    png_set_IHDR (PngPointer, InfoPointer, width, height, bitdepth, BitsPerPixel == 24 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    std::vector<unsigned char*> RowPointers(height);
    unsigned char* BuffPos = reinterpret_cast<unsigned char*>(Pixels.data());

    //Set the Row pointers to my vector<BGRA> Pixels. It should have been stored upright already.
    //I think this flips it upside down :S?

    for (size_t I = 0; I < height; ++I)
    {
        RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
    }

    png_set_bgr(PngPointer);        //My struct vector holds BGRA and PNG requires RGBA so swap them..
    png_set_write_fn(PngPointer, reinterpret_cast<void*>(&hFile), WriteToStream, nullptr);
    png_set_rows(PngPointer, InfoPointer, RowPointers.data());
    png_write_png(PngPointer, InfoPointer, PNG_TRANSFORM_IDENTITY, NULL);
    png_destroy_write_struct(&PngPointer, &InfoPointer);
    hFile.close();
}

void SetBitsPerPixel(uint32_t BPP)
{
    BitsPerPixel = BPP;
    bitdepth = (BPP > 24 ? 8 : 6);
    channels = (BPP > 24 ? 4 : 3);
    colortype = (BPP > 24 ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB);
}


int main()
{
    Load("C:/Images/Png24.png");
    SetBitsPerPixel(32);
    Save("C:/Images/Output/Png32.png");
}

ロードします(MS-Paintで作成された24ビットPNG):ここに画像の説明を入力してください 24として保存し直すと、問題なく保存されます。32として保存しようとすると、次のようになります。

ここに画像の説明を入力してください

4

1 に答える 1

2

ソースコードに次の変更を加えてみてください。未検証! ロード関数でこれを変更します

    case PNG_COLOR_TYPE_RGB:
    {
        png_set_bgr(PngPointer);
        BitsPerPixel = 24;
        break;
    }

これに:

    case PNG_COLOR_TYPE_RGB:
    {
        png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
        png_set_bgr(PngPointer);
        BitsPerPixel = 32;
        break;
    }


ロードおよび保存機能でこれを変更します 。

for (size_t I = 0; I < height; ++I)
{
    RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
}

これに:

size_t BytesPerLine = width << 2;
unsigned char *ptr = BuffPos;
for (size_t I = 0; I < height; ++I, ptr += BytesPerLine)
    RowPointers[I] = ptr;


保存機能でこれを変更します :

png_write_png(PngPointer, InfoPointer, PNG_TRANSFORM_IDENTITY, NULL);

これに:

png_write_png(PngPointer, InfoPointer, BitsPerPixel == 24 ? PNG_TRANSFORM_STRIP_FILLER : PNG_TRANSFORM_IDENTITY, NULL);


SetBitsPerPixel関数で、次の行を変更 します。

bitdepth = (BPP > 24 ? 8 : 6);

これに:

bitdepth = (BPP >= 24 ? 8 : 6);

実際、私が知る限り、24および32 bppイメージのビット深度は8ビットである必要があるため、なぜこれを行っているのかわかりませ

:この変更は24および32 bpp画像を対象としているため、インデックス付きまたはグレースケール画像を使用する場合は、追加の変更が必要になる場合があります。重要なのは、常にピクセルをBGRAバッファーに保存する必要があるということです(アルファチャネルのないインデックス付き、グレースケール、またはRGB画像をロードするかどうかは関係ありません)。

于 2012-12-24T01:48:02.217 に答える