Browse Source

* src/image.c: SDL_image support.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/pwntcha/trunk@407 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 20 years ago
parent
commit
a967f0584c
3 changed files with 90 additions and 36 deletions
  1. +15
    -2
      configure.ac
  2. +8
    -3
      src/Makefile.am
  3. +67
    -31
      src/image.c

+ 15
- 2
configure.ac View File

@@ -20,6 +20,19 @@ AC_TYPE_SIZE_T

AC_CHECK_FUNCS(getopt_long)

# Use SDL?
ac_cv_my_have_sdl="no"
save_CPPFLAGS="${CPPFLAGS}"
AC_PATH_PROG(SDL_CONFIG, sdl-config, no)
if test "${SDL_CONFIG}" != "no"; then
CPPFLAGS="${CPPFLAGS} `sdl-config --cflags`"
fi
AC_CHECK_HEADERS(SDL_image.h,
[ac_cv_my_have_sdl="yes"],
[ac_cv_my_have_sdl="no"])
CPPFLAGS="${save_CPPFLAGS}"
AM_CONDITIONAL(USE_SDL, test "${ac_cv_my_have_sdl}" = "yes")

# Use OpenCV?
ac_cv_my_have_opencv="no"
save_CPPFLAGS="${CPPFLAGS}"
@@ -46,8 +59,8 @@ AC_CHECK_HEADERS(Imlib2.h,
CPPFLAGS="${save_CPPFLAGS}"
AM_CONDITIONAL(USE_IMLIB2, test "${ac_cv_my_have_imlib2}" = "yes")

if test "${ac_cv_my_have_imlib2}" = "no" -a "${ac_cv_my_have_opencv}" = "no"; then
AC_MSG_ERROR([[cannot find Imlib2 or OpenCV, please install one of them]])
if test "${ac_cv_my_have_sdl}" = "yes" -a "${ac_cv_my_have_imlib2}" = "no" -a "${ac_cv_my_have_opencv}" = "no"; then
AC_MSG_ERROR([[cannot find SDL_Image, Imlib2 or OpenCV, please install one of them]])
fi

AC_OUTPUT([


+ 8
- 3
src/Makefile.am View File

@@ -13,16 +13,21 @@ pwntcha_SOURCES = \
slashdot.c \
test.c

if USE_OPENCV
ADDITIONAL_CFLAGS = `opencv-config --cflags`
ADDITIONAL_LDFLAGS = `opencv-config --libs opencv highgui`
if USE_SDL
ADDITIONAL_CFLAGS = `sdl-config --cflags`
ADDITIONAL_LDFLAGS = `sdl-config --libs` -lSDL_image
else
if USE_IMLIB2
ADDITIONAL_CFLAGS = `imlib2-config --cflags` -DX_DISPLAY_MISSING=1
ADDITIONAL_LDFLAGS = `imlib2-config --libs`
else
if USE_OPENCV
ADDITIONAL_CFLAGS = `opencv-config --cflags`
ADDITIONAL_LDFLAGS = `opencv-config --libs opencv highgui`
else
ADDITIONAL_CFLAGS =
ADDITIONAL_LDFLAGS =
endif
endif
endif


+ 67
- 31
src/image.c View File

@@ -15,11 +15,13 @@
#include "config.h"
#include "common.h"

#if defined(HAVE_CV_H)
# include <cv.h>
# include <highgui.h>
#if defined(HAVE_SDL_IMAGE_H)
# include <SDL_image.h>
#elif defined(HAVE_IMLIB2_H)
# include <Imlib2.h>
#elif defined(HAVE_CV_H)
# include <cv.h>
# include <highgui.h>
#else
# error "No imaging library"
#endif
@@ -27,22 +29,24 @@
struct image *image_load(char *name)
{
struct image *img;
#if defined(HAVE_CV_H)
IplImage *priv = cvLoadImage(name, -1);
#if defined(HAVE_SDL_IMAGE_H)
SDL_Surface *priv = IMG_Load(name);
#elif defined(HAVE_IMLIB2_H)
Imlib_Image priv = imlib_load_image(name);
#elif defined(HAVE_CV_H)
IplImage *priv = cvLoadImage(name, -1);
#endif

if(!priv)
return NULL;

img = malloc(sizeof(struct image));
#if defined(HAVE_CV_H)
img->width = priv->width;
img->height = priv->height;
img->pitch = priv->widthStep;
img->channels = priv->nChannels;
img->pixels = priv->imageData;
#if defined(HAVE_SDL_IMAGE_H)
img->width = priv->w;
img->height = priv->h;
img->pitch = priv->pitch;
img->channels = priv->format->BytesPerPixel;
img->pixels = priv->pixels;
#elif defined(HAVE_IMLIB2_H)
imlib_context_set_image(priv);
img->width = imlib_image_get_width();
@@ -50,6 +54,12 @@ struct image *image_load(char *name)
img->pitch = 4 * imlib_image_get_width();
img->channels = 4;
img->pixels = (char *)imlib_image_get_data();
#elif defined(HAVE_CV_H)
img->width = priv->width;
img->height = priv->height;
img->pitch = priv->widthStep;
img->channels = priv->nChannels;
img->pixels = priv->imageData;
#endif
img->priv = (void *)priv;

@@ -59,22 +69,38 @@ struct image *image_load(char *name)
struct image *image_new(int width, int height)
{
struct image *img;
#if defined(HAVE_CV_H)
IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
#if defined(HAVE_SDL_IMAGE_H)
SDL_Surface *priv;
Uint32 rmask, gmask, bmask, amask;
# if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
# else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
# endif
priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
rmask, gmask, bmask, amask);
#elif defined(HAVE_IMLIB2_H)
Imlib_Image priv = imlib_create_image(width, height);
#elif defined(HAVE_CV_H)
IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
#endif

if(!priv)
return NULL;

img = malloc(sizeof(struct image));
#if defined(HAVE_CV_H)
img->width = priv->width;
img->height = priv->height;
img->pitch = priv->widthStep;
img->channels = priv->nChannels;
img->pixels = priv->imageData;
#if defined(HAVE_SDL_IMAGE_H)
img->width = priv->w;
img->height = priv->h;
img->pitch = priv->pitch;
img->channels = priv->format->BytesPerPixel;
img->pixels = priv->pixels;
#elif defined(HAVE_IMLIB2_H)
imlib_context_set_image(priv);
img->width = imlib_image_get_width();
@@ -82,6 +108,12 @@ struct image *image_new(int width, int height)
img->pitch = 4 * imlib_image_get_width();
img->channels = 4;
img->pixels = (char *)imlib_image_get_data();
#elif defined(HAVE_CV_H)
img->width = priv->width;
img->height = priv->height;
img->pitch = priv->widthStep;
img->channels = priv->nChannels;
img->pixels = priv->imageData;
#endif
img->priv = (void *)priv;

@@ -90,13 +122,15 @@ struct image *image_new(int width, int height)

void image_free(struct image *img)
{
#if defined(HAVE_CV_H)
IplImage *iplimg;
iplimg = (IplImage *)img->priv;
cvReleaseImage(&iplimg);
#if defined(HAVE_SDL_IMAGE_H)
SDL_FreeSurface(img->priv);
#elif defined(HAVE_IMLIB2_H)
imlib_context_set_image(img->priv);
imlib_free_image();
#elif defined(HAVE_CV_H)
IplImage *iplimg;
iplimg = (IplImage *)img->priv;
cvReleaseImage(&iplimg);
#endif

free(img);
@@ -146,14 +180,8 @@ int setpixel(struct image *img, int x, int y, int r, int g, int b)

void image_display(struct image *img)
{
#if defined(HAVE_CV_H)
char name[BUFSIZ];
sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
cvNamedWindow(name, 0);
cvShowImage(name, img->priv);
cvResizeWindow(name, img->width * 2, img->height * 2 + 50);
while((unsigned char)cvWaitKey(0) != 0x1b)
;
#if defined(HAVE_SDL_IMAGE_H)
//Nothing to do here
#elif defined(HAVE_IMLIB2_H)
//char name[BUFSIZ];
//static int i = 0;
@@ -161,6 +189,14 @@ void image_display(struct image *img)
//imlib_context_set_image(img->priv);
//imlib_save_image(name);
//fprintf(stderr, "saved to %s\n", name);
#elif defined(HAVE_CV_H)
char name[BUFSIZ];
sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
cvNamedWindow(name, 0);
cvShowImage(name, img->priv);
cvResizeWindow(name, img->width * 2, img->height * 2 + 50);
while((unsigned char)cvWaitKey(0) != 0x1b)
;
#endif
}


Loading…
Cancel
Save