I need c code using stb_image to decode a jpg
- User
I need c code using stb_image to decode a jpg
- Assistant
Sure! Here's an example of C code that uses the stb_image library to decode a JPEG file:
#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include <stdio.h> int main() { char* filename = "image.jpg"; int width, height, channels; // Load the image unsigned char* image = stbi_load(filename, &width, &height, &channels, 0); if (!image) { printf("Failed to load image: %s\n", stbi_failure_reason()); return 1; } // Print image information printf("Image width: %d\n", width); printf("Image height: %d\n", height); printf("Number of channels: %d\n", channels); // Free the image data memory stbi_image_free(image); return 0; }Make sure you have the
stb_image.hfile available in the same directory as your code. You can obtain the header file from the stb_image library repository: https://github.com/nothings/stb/blob/master/stb_image.h.To compile and run the code, you'll also need to link against the dynamic library for your system. For example, in Linux, you can compile with the following command:
gcc example.c -o example -lm -ljpegIn this case, the code simply loads the JPEG file specified by
filename, prints some information about the image, and then frees the allocated image data memory usingstbi_image_free(). You can modify the code to perform other operations on the image as needed.Remember to replace
"image.jpg"with the path to your own JPEG file. - User
Can you expand the example to include loading, modfying and saving a jpg with stb_image
- Assistant
Certainly! Here's an expanded example that includes loading, modifying, and saving a JPEG image using the stb_image library:
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image.h" #include "stb_image_write.h" #include <stdio.h> #include <stdlib.h> int main() { char* filename = "image.jpg"; int width, height, channels; // Load the image unsigned char* image = stbi_load(filename, &width, &height, &channels, 0); if (!image) { printf("Failed to load image: %s\n", stbi_failure_reason()); return 1; } // Modify the image // For example, let's change the color of the top-left pixel to red int pixelIndex = 0; image[pixelIndex * channels] = 255; // Red channel image[pixelIndex * channels + 1] = 0; // Green channel image[pixelIndex * channels + 2] = 0; // Blue channel // Save the modified image as a new file char* outputFilename = "modified_image.jpg"; int success = stbi_write_jpg(outputFilename, width, height, channels, image, 100); if (!success) { printf("Failed to save image: %s\n", stbi_failure_reason()); return 1; } // Free the image data memory stbi_image_free(image); printf("Image modified and saved successfully as %s\n", outputFilename); return 0; }This expanded example shows how to modify the loaded image data. In this case, the color of the top-left pixel is changed to red by directly manipulating the pixel values.
After modifying the image, the code saves the modified image as a new JPEG file using
stbi_write_jpg(). TheoutputFilenamevariable specifies the name of the output file.Remember to replace
"image.jpg"with the path to your own JPEG file.