resize image in opengl using std library at time of std_load
- User
resize image in opengl using std library at time of std_load
- Assistant
- User
stbir_resize_uint8 function of stb library
- Assistant
resize image in opengl using std library at time of std_load
stbir_resize_uint8 function of stb library
To resize an image using the standard library and OpenGL, you can use the stb_image library for loading images and stb_image_resize for resizing them. Below is an example demonstrating how to load and resize an image using these libraries (assuming you already have them included in your project) and then create an OpenGL texture from the resized image.
Please note that you need to have stb_image.h and stb_image_resize.h headers available in your project.
Here's a complete example in C++:
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include <GL/gl.h>
#include <iostream>
GLuint loadAndResizeTexture(const char* filepath, int newWidth, int newHeight) {
// Load image using stb_image
int width, height, channels;
unsigned char *imageData = stbi_load(filepath, &width, &height, &channels, 0);
if (!imageData) {
std::cerr << "Failed to load image: " << filepath << std::endl;
return 0;
}
// Allocate memory for the resized image
unsigned char* resizedImageData = new unsigned char[newWidth * newHeight * channels];
// Resize image using stb_image_resize
if (!stbir_resize_uint8(imageData, width, height, 0, resizedImageData, newWidth, newHeight, 0, channels)) {
std::cerr << "Failed to resize image." << std::endl;
stbi_image_free(imageData);
delete[] resizedImageData;
return 0;
}
// Free the original image data
stbi_image_free(imageData);
// Generate OpenGL texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload the texture to the GPU
glTexImage2D(GL_TEXTURE_2D, 0, (channels == 4) ? GL_RGBA : GL_RGB, newWidth, newHeight, 0, (channels == 4) ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, resizedImageData);
// Free the resized image data
delete[] resizedImageData;
return texture;
}
int main() {
// Initialize your OpenGL context here
// Example usage
GLuint texture = loadAndResizeTexture("your_image.png", 512, 512);
if (texture == 0) {
std::cerr << "Texture loading failed." << std::endl;
return -1;
}
// Your rendering code goes here
// Cleanup OpenGL resources
glDeleteTextures(1, &texture);
return 0;
}
This code will read an image from a file, resize it to the desired dimensions, and create an OpenGL texture with it. Make sure your OpenGL context is properly initialized before using OpenGL functions. This code does not handle context creation, which is typically done with a windowing library such as GLFW or SDL.
The stbir_resize_uint8 function from the stb_image_resize library is used for resizing images. This function resamples an input image to a different width and height, and it operates on unsigned 8-bit per channel pixel data.
int stbir_resize_uint8(
const void *input_pixels, int input_w, int input_h, int input_stride_in_bytes,
void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
int num_channels);
0 if rows are tightly packed.0 if rows are tightly packed.It returns 1 on success and 0 on failure.
Let's demonstrate how to use the stbir_resize_uint8 function by resizing an image.
// Necessary includes
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include <iostream>
int main() {
// Load input image using stb_image
int width, height, channels;
unsigned char *input_image = stbi_load("your_image.png", &width, &height, &channels, 0);
if (!input_image) {
std::cerr << "Failed to load image.\n";
return -1;
}
// Define new dimensions for the resized image
int new_width = 512;
int new_height = 512;
// Allocate memory for the resized image
unsigned char* output_image = new unsigned char[new_width * new_height * channels];
// Perform the resize operation
if (!stbir_resize_uint8(input_image, width, height, 0, output_image, new_width, new_height, 0, channels)) {
std::cerr << "Failed to resize image.\n";
stbi_image_free(input_image);
delete[] output_image;
return -1;
}
// Save or use the resized image (insert your code here)
// For example, you could save the output image using some save function you have.
// Cleanup
stbi_image_free(input_image);
delete[] output_image;
std::cout << "Image resized successfully!\n";
return 0;
}
stbi_load function from stb_image.h.new_width and new_height) for the resized image.stbir_resize_uint8 to resize the image data.This example demonstrates how to resize an image using the stbir_resize_uint8 function from the stb_image_resize library. Ensure that your build process includes the necessary stb_image.h and stb_image_resize.h headers with their implementations defined as shown above. This will allow you to leverage the powerful image loading and resizing capabilities of the stb libraries.
Not yet included in a published release.