java imageio正在读取rgb而不是argb数据

11dmarpk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(458)

我正在为一个照片编辑器的图形用户界面工作,目前我被困在从一个文件加载32位bmp图像。一切都很顺利,直到我开始尝试多个层次,并把它们塑造成一个单一的(层成型和图像保存由运行时类使用c++.exe文件完成)
我用pixelformer软件制作了一个简单的300x300红色bmp图像,每个像素的值为r:255g:0b:0a:125
我注意到我下面的方法为alpha通道加载了一个默认值为255的rgb,其中125应该是。在这里:

public void readFile(String sourceFileName, Image image) throws IOException {

    ArrayList<Short> pixels = new ArrayList<>();
    try {

       BufferedImage img = ImageIO.read(getClass().getResource(sourceFileName));
       System.out.println(img.getType());

        for (int i =0;i<img.getHeight();i++)
            for (int j =0;j<img.getWidth();j++)
            {
                Color c = new Color(img.getRGB(j,i),true);
                pixels.add((short)(c.getRed()));
                pixels.add((short)(c.getGreen()));
                pixels.add((short)(c.getBlue()));
                pixels.add((short)(c.getAlpha()));
                System.out.println("argb: " + c.getAlpha() + ", " + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());

            }

      image.loadLayer(new Layer(img.getHeight(),img.getWidth(),pixels));

    } catch (IOException e)
    {
        //
    }

然而,当我用我之前编写的c读取器加载这个bmp文件并将读取的数据保存到json文件中时,我可以清楚地看到alpha值125位于java方法返回255的位置。
json部分片段:您可以清楚地看到像素实际上是它们应该的样子。
我可以创建一个用于加载文件的c
可执行文件,并使用运行时类从java运行它,但我不想在这里经常使用该类。
更新:出于某种原因system.out.println(img.gettype());返回类型\ int \ rgb
更新2:对于那些可能认为我在使用某个c++库的人,这里是我基于此网站代码编写的代码:https://solarianprogrammer.com/2018/11/19/cpp-reading-writing-bmp-images/ 图像,图层和像素是我的自定义类。
标题结构:


# define BMPID 0x4D42

# pragma pack(push, 1)

struct BMPFileHeader {
    uint16_t fileType = BMPID; 
    uint32_t fileSize = 0; //in bytes
    uint16_t unused1 = 0;
    uint16_t unused2 = 0;
    uint32_t data_offset = 0;           // Start position of pixel data (bytes from the beginning of the file)
};

struct BMPInfoHeader {
    uint32_t headerSize = 0; 
    int32_t width = 0; 
    int32_t height = 0; 

    uint16_t planes = 1; //
    uint16_t bitsPerPixel = 0;
    uint32_t compression = 0;
    uint32_t image_size = 0;
    int32_t x_pixels_per_meter = 0;
    int32_t y_pixels_per_meter = 0;
    uint32_t colors_used = 0;
    uint32_t colors_important = 0;    // No. of colors used for displaying the bitmap. If 0 all colors are required
};

struct BMPColorHeader {
    uint32_t red_mask = 0x00ff0000;         
    uint32_t green_mask = 0x0000ff00;      
    uint32_t blue_mask = 0x000000ff;        
    uint32_t alpha_mask = 0xff000000;       
    uint32_t color_space_type = 0x73524742;
    uint32_t unused[12] = { 0 };       // Unused data for sRGB color space
};

# pragma pack(pop)

实际读卡器:

void BMPFormater::read(std::string sourceFileName)
{
    {
        std::ifstream input{ sourceFileName, std::ios_base::binary };
        if (input)
        {

            BMPFileHeader fileHeader; // struct instance
            BMPInfoHeader infoHeader; // struct instance
            BMPColorHeader colorHeader; // struct instance

            uint32_t row_stride{ 0 };

            input.read((char*)&fileHeader, sizeof(fileHeader));
            if (fileHeader.fileType != BMPID) {
                throw std::runtime_error("Error! Unrecognized file format.");
            }

            input.read((char*)&infoHeader, sizeof(infoHeader));

            this->bitsPerPixel= infoHeader.bitsPerPixel;
           this->height=infoHeader.height;
           this->width=infoHeader.width;

            if (infoHeader.height < 0)
                throw std::runtime_error("The program can treat only BMP images with the origin in the bottom left corner!");

            // Jump to the pixel data location
            input.seekg(fileHeader.data_offset, input.beg);

           this->pixels.resize(this->width * this->height * this->bitsPerPixel / 8);

            // Here we check if there is a need to take into account row padding
            if (infoHeader.width % 4 == 0)
                input.read((char*)this->pixels.data(), this->pixels.size());

            else {
                row_stride = infoHeader.width * infoHeader.bitsPerPixel / 8;

               // uint32_t new_stride = 0;
                uint32_t new_stride = row_stride;
                    while (new_stride % 4 != 0)
                        new_stride++;

                std::vector<uint8_t> padding_row(new_stride - row_stride);

                for (int y = 0; y < infoHeader.height; ++y) { //cita red po red i svaki red peduje
                    input.read((char*)(this->pixels.data() + row_stride * y), row_stride);
                    input.read((char*)padding_row.data(), padding_row.size());
                }

            }

            //zamena crvenog i plavog piksela BGR(A) -> RGB(A)
            if (infoHeader.bitsPerPixel == 32)
            {

                for (int i = 0; i < infoHeader.width * infoHeader.height * infoHeader.bitsPerPixel / 8; i += 4)
                {
                    uint8_t temp = this->pixels[i];
                    this->pixels[i] = this->pixels[i + 2];
                    this->pixels[i + 2] = temp;
                }

            }
            else if (infoHeader.bitsPerPixel == 24)
            {

                for (int i = 0; i < infoHeader.width * infoHeader.height * infoHeader.bitsPerPixel / 8; i += 3)
                {
                    uint8_t temp = this->pixels[i];
                    this->pixels[i] = this->pixels[i + 2];
                    this->pixels[i + 2] = temp;
                }

            }

            Layer newLayer(this->height,this->width,this->bitsPerPixel,this->pixels);

            if (image->numberOfLayers != 0)

                image->resizeLayers(newLayer);

            image->layers.push_back(newLayer);
            image->numberOfLayers++;

            input.close();
        }
        else {
            throw std::runtime_error("Unable to open the input image file.");
        }
    }
}
jtw3ybtb

jtw3ybtb1#

感谢用户@camickr,我发现了这个问题。api不理解这个bmp格式(很可能是头数据),所以我使用这个网站完成了bmp到bmp的转换https://www.media.io/image-converter.html 结果很好。

相关问题