Skip to content

Commit

Permalink
channel formats python script
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 22, 2024
1 parent 386ca17 commit eb43a0e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
32 changes: 20 additions & 12 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,29 @@ bool Model::loadTexture() {

// Determine the correct format
GLenum format;
if (num_channels == 1)
format = GL_RED;
else if (num_channels == 3)
format = GL_RGB;
else if (num_channels == 4)
format = GL_RGBA;
else {
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
stbi_image_free(image_data);
return false;
switch (num_channels) {
case 1:
format = GL_RED;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
default: {
// Free image data before returning
stbi_image_free(image_data);
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
return false;
}
}

std::cout << "Format is: " << format << " (" << num_channels << ")" << std::endl;

// Upload data to GPU
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image_data);
glGenerateMipmap(GL_TEXTURE_2D);

Expand Down
40 changes: 40 additions & 0 deletions scripts/channels_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from PIL import Image
import sys

def check_image_format(image_path):
try:
with Image.open(image_path) as img:
mode = img.mode
format = img.format
size = img.size

if mode == "L":
num_channels = 1 # Grayscale
opengl_format = "GL_RED"
elif mode == "LA":
num_channels = 2 # Grayscale + Alpha
opengl_format = "GL_RG"
elif mode == "RGB":
num_channels = 3 # RGB
opengl_format = "GL_RGB"
elif mode == "RGBA":
num_channels = 4 # RGBA
opengl_format = "GL_RGBA"
else:
num_channels = len(mode) # Other modes like "CMYK"
opengl_format = "Unknown or not directly supported"

print(f"Image Format: {format}")
print(f"Image Size: {size}")
print(f"Number of Channels: {num_channels}")
print(f"OpenGL Format: {opengl_format}")

except IOError:
print(f"Cannot open image file: {image_path}")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python check_image_format.py <image_path>")
else:
image_path = sys.argv[1]
check_image_format(image_path)

0 comments on commit eb43a0e

Please sign in to comment.