#include #include "SDL.h" #include "SDL_image.h" #include "Imlib2.h" enum { IMG_LEFT = 0, IMG_RIGHT = 1, IMG_LAST // should be last }; typedef struct RGB { Uint8 r,g,b; } RGB; typedef struct ARGB { Uint8 a,r,g,b; } ARGB; typedef struct BGR { Uint8 b,g,r; } BGR; typedef struct BGRA { Uint8 b,g,r,a; } BGRA; int main(int argc, char* argv[]) { SDL_Surface* pImages[IMG_LAST]; Imlib_Image pOut = NULL; DATA32* pBuf = NULL; Uint32 width = 0; Uint32 height = 0; Uint32 bpp = 0; Uint8 r, g, b; RGB* p = NULL; Uint32 offset = 0; if (argc < 3) { printf("Usage: interlace LEFT RIGHT [OUTPUT]\n"); return -1; } for (int i = 0; i < IMG_LAST; i++) { pImages[i] = IMG_Load(argv[i+1]); if (!pImages[i]) { printf("IMG_Load: %s\n", IMG_GetError()); return 1; } printf("Loaded %dx%dx%d '%s'\n", pImages[i]->w, pImages[i]->h, pImages[i]->format->BitsPerPixel, argv[i+1]); } if ( (pImages[IMG_LEFT]->w != pImages[IMG_RIGHT]->w) || (pImages[IMG_LEFT]->h != pImages[IMG_RIGHT]->h) || (pImages[IMG_LEFT]->format->BitsPerPixel != pImages[IMG_RIGHT]->format->BitsPerPixel) || (pImages[IMG_LEFT]->format->BitsPerPixel != 24) ) { printf("ERROR: Specified images aren't the same size|bpp or not 24 bpp\n"); goto cleanup; } width = pImages[IMG_RIGHT]->w; height = pImages[IMG_RIGHT]->h; bpp = pImages[IMG_RIGHT]->format->BitsPerPixel; pOut = imlib_create_image(width, height); imlib_context_set_image(pOut); pBuf = imlib_image_get_data(); printf("%dx%d\n", imlib_image_get_width(), imlib_image_get_height()); p = (RGB*)pImages[IMG_LEFT]->pixels; for (int y = 0; y < height; y+=2) for (int x = 0; x < width; x++) { offset = y*width + x; r = p[offset].r; g = p[offset].g; b = p[offset].b; pBuf[offset] = r << 16 | g << 8 | b; } p = (RGB*)pImages[IMG_RIGHT]->pixels; for (int y = 1; y < height; y+=2) for (int x = 0; x < width; x++) { offset = y*width + x; r = p[offset].r; g = p[offset].g; b = p[offset].b; pBuf[offset] = r << 16 | g << 8 | b; } imlib_image_put_back_data(pBuf); if (argc > 3) imlib_save_image(argv[3]); else imlib_save_image("out.jpg"); imlib_free_image(); cleanup: for (int i = 0; i < IMG_LAST; i++) SDL_FreeSurface(pImages[i]); return 0; }