I'm working on a GUI for a photo editor and currently I'm stuck on loading a 32bit BMP image from a file. It was all going smoothly until I've started experimenting with multiple layers and molding them into a single one. (Layer molding and image saving is beeing done by Runtime class using c++ .exe file)
I've made a simple 300x300 red bmp image using pixelFormer software with individual pixels having values of R:255 G:0 B:0 A:125
What I've noticed is that my method which is bellow loads a RGB with default 255 value for alpha channel where 125 should be. Here it is:
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)
{
//
}
However, when I load this same BMP file with a c++ reader which I have previously written and save the read data in to a json file I can clearly see that alpha value of 125 is located where Java method returns a 255.
Json partial snippet: You can clearly see that pixels are in fact as they should be.
I could create a c++ executable for loading the file and run it from Java using Runtime class but I don't want to use that class too often here.
Update: For some reason System.out.println(img.getType()); returns TYPE_INT_RGB
Update 2: For those of you who might thing that I was using some c++ library, no here is my code based on code from this website: https://solarianprogrammer.com/2018/11/19/cpp-reading-writing-bmp-images/ Image, Layer and Pixel are my custom classes.
Header structs:
#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)
Actual reader:
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.");
}
}
}
Thanks to the user @camickr I've found the issue. The API didn't understand this BMP Format (header data most likely) so I've done a conversion from BMP to BMP using this website https://www.media.io/image-converter.html and it worked out just fine.
User contributions licensed under CC BY-SA 3.0