tags:

views:

131

answers:

3

Hi all! I need to convert a GIF image to Jpeg image using C programming language. I searched the web, but I didn't find an example which could help me. Any suggestion are appreciated!

EDIT: I want to do this using an cross-platform open-source library like SDL.

+5  A: 

Try the GD or ImageMagick libraries

Paul Dixon
+! ImageMagick!
kenny
Could you please suggest an example for ImageMagick (gif to jpg)? I can't find anything on the web.
Levo
Or GraphicsMagick.
Amigable Clark Kant
+1  A: 

I found libafterimage to be incredibly simple to use. In this snippet I also scale the image to at most width or at most height, while preserving aspect:

#include <libAfterImage/afterimage.h>



int convert_image_to_jpeg_of_size(const char* infile, const char* outfile, const double max_width, const double max_height)
{
    ASImage*  im;
    ASVisual* asv;
    ASImage*  scaled_im;
    double    height;
    double    width;
    double    pixelzoom;
    double    proportion;

    im = file2ASImage(infile, 0xFFFFFFFF, SCREEN_GAMMA, 0, ".", NULL);

    if (!im) {

        return 1;
    }

    proportion = (double)im->width / (double)im->height;

    asv = create_asvisual(NULL, 0, 0, NULL);

    if (proportion > 1) {
            /* Oblong. */
        width = max_width;
        pixelzoom = max_width / im->width;
        height = (double)im->height * pixelzoom;
    } else {
        height = max_height;
        pixelzoom = max_height / im->height;
        width = (double)im->width * pixelzoom;
    }

    scaled_im = scale_asimage(asv, im, width, height, ASA_ASImage, 0, ASIMAGE_QUALITY_DEFAULT);

        /* writing result into the file */
    ASImage2file(scaled_im, NULL, outfile, ASIT_Jpeg, NULL);
    destroy_asimage(&scaled_im);
    destroy_asimage(&im);

    return 0;
}
Amigable Clark Kant
Under what licence is it released? I need an open source lib.
Levo
Amigable Clark Kant
Thanks! It works perfectly! One more question, is it possible to compile it with gif and jpg support only? (to reduce output size)
Levo
@Levo? Output size of what? The binary you compile. Typically, you link dynamically against a library and other programs on the computer need the library too. In that case it won't matter much. If your program is the only program on the computer to use the library, try linking statically. This will often reduce the total footprint of your program + library. Tell us more about your needs and we can help you. If you are talking about *scavenging* the library for source files, keep in mind it is LGPL, your code has effectively to be too, in that case.
Amigable Clark Kant
A: 

Not the easiest to use, but the fastest way is almost surely using libavcodec/libavformat from ffmpeg.

R..
What, to convert a GIF into a JPEG? Both are not video formats
Pekka
Yes. JPEG is definitely a *frame* format within video (MJPEG), and GIF might be too in some obscure video, but probably the reason it's there is that GIF is a "video format" in itself (animated GIFs).
R..