Java ImageIO正在读取RGB数据,而不是ARGB数据



我正在为照片编辑器开发GUI,目前我正忙于从文件加载32位BMP图像。这一切都很顺利,直到我开始尝试多层并将它们成型为一层。(层成型和图像保存由Runtime类使用c++.exe文件完成(

我使用pixelFormer软件制作了一个简单的300x300红色bmp图像,单个像素的值为R:255 G:0 B:0 a:125

我注意到的是,我的方法在下面加载了一个RGB,默认值为255的alpha通道,其中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++可执行文件,并使用Runtime类从Java运行它,但我不想在这里经常使用该类。

更新:由于某些原因System.out.println(img.getType(((;返回TYPE_INT_RGB

更新2:对于那些可能认为我使用了一些c++库的人来说,不,这是我基于本网站代码的代码:https://solarianprogrammer.com/2018/11/19/cpp-reading-writing-bmp-images/Image、Layer和Pixel是我的自定义类。

标题结构:

#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.");
}
}
}

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

最新更新