Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

I need c code using stb_image to decode a jpg

  1. User

    I need c code using stb_image to decode a jpg

  2. 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.h file 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 -ljpeg
    

    In 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 using stbi_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.

  3. User

    Can you expand the example to include loading, modfying and saving a jpg with stb_image

  4. 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(). The outputFilename variable specifies the name of the output file.

    Remember to replace "image.jpg" with the path to your own JPEG file.