Changeset 2629


Ignore:
Timestamp:
Aug 2, 2008, 2:00:51 AM (14 years ago)
Author:
Sam Hocevar
Message:
  • pipi.c: reimplement pipi_new() without relying on the underlying codec library, so we can have several or none of them at some time.
  • sdl.c imlib.c: allow to save images that weren't created using the current codec.
Location:
libpipi/trunk/pipi
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • libpipi/trunk/pipi/codec.c

    r2607 r2629  
    3939}
    4040
    41 pipi_image_t *pipi_new(int width, int height)
    42 {
    43 #if USE_SDL
    44     return pipi_new_sdl(width, height);
    45 #elif USE_IMLIB2
    46     return pipi_new_imlib2(width, height);
    47 #elif USE_OPENCV
    48     return pipi_new_opencv(width, height);
    49 #endif
    50 }
    51 
    5241void pipi_free(pipi_image_t *img)
    5342{
    54     unsigned int i;
     43    int i;
    5544
    5645    for(i = 0; i < PIPI_PIXELS_MAX; i++)
     
    5948
    6049#if USE_SDL
    61     return pipi_free_sdl(img);
     50    pipi_free_sdl(img);
    6251#elif USE_IMLIB2
    63     return pipi_free_imlib2(img);
     52    pipi_free_imlib2(img);
    6453#elif USE_OPENCV
    65     return pipi_free_opencv(img);
     54    pipi_free_opencv(img);
    6655#endif
     56
     57    free(img);
    6758}
    6859
  • libpipi/trunk/pipi/codec/imlib.c

    r2605 r2629  
    3737        return NULL;
    3838
    39     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    40     memset(img, 0, sizeof(pipi_image_t));
    41 
    4239    imlib_context_set_image(priv);
    43     img->w = imlib_image_get_width();
    44     img->h = imlib_image_get_height();
     40    img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
    4541
    4642    img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data();
    47     img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
    48     img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
    49     img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
    50     img->last_modified = PIPI_PIXELS_RGBA32;
    51 
    52     img->codec_priv = (void *)priv;
    53     img->codec_format = PIPI_PIXELS_RGBA32;
    54 
    55     return img;
    56 }
    57 
    58 pipi_image_t *pipi_new_imlib2(int width, int height)
    59 {
    60     pipi_image_t *img;
    61     Imlib_Image priv = imlib_create_image(width, height);
    62 
    63     if(!priv)
    64         return NULL;
    65 
    66     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    67     memset(img, 0, sizeof(pipi_image_t));
    68 
    69     imlib_context_set_image(priv);
    70     img->w = imlib_image_get_width();
    71     img->h = imlib_image_get_height();
    72 
    73     img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data();
    74     img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
    75     img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
    76     img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
     43    img->p[PIPI_PIXELS_RGBA32].w = img->w;
     44    img->p[PIPI_PIXELS_RGBA32].h = img->h;
     45    img->p[PIPI_PIXELS_RGBA32].pitch = 4 * img->w;
    7746    img->last_modified = PIPI_PIXELS_RGBA32;
    7847
     
    8756    imlib_context_set_image(img->codec_priv);
    8857    imlib_free_image();
    89 
    90     free(img);
    9158}
    9259
    9360void pipi_save_imlib2(pipi_image_t *img, const char *name)
    9461{
     62    if(!img->codec_priv)
     63    {
     64        Imlib_Image priv = imlib_create_image(img->w, img->h);
     65        void *data;
     66
     67        imlib_context_set_image(priv);
     68        data = imlib_image_get_data();
     69
     70        /* FIXME: check pitch differences here */
     71        if(img->last_modified == PIPI_PIXELS_RGBA32)
     72        {
     73            memcpy(data, img->p[PIPI_PIXELS_RGBA32].pixels,
     74                   4 * img->w * img->h);
     75            free(img->p[PIPI_PIXELS_RGBA32].pixels);
     76        }
     77
     78        img->p[PIPI_PIXELS_RGBA32].pixels = data;
     79        img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
     80        img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
     81        img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
     82
     83        img->codec_priv = (void *)priv;
     84        img->codec_format = PIPI_PIXELS_RGBA32;
     85    }
     86
    9587    pipi_getpixels(img, img->codec_format);
    9688    imlib_context_set_image(img->codec_priv);
  • libpipi/trunk/pipi/codec/opencv.c

    r2605 r2629  
    3030#include "pipi_internals.h"
    3131
     32/* FIXME: this whole file is broken until we support BGR24 images */
     33
    3234pipi_image_t *pipi_load_opencv(const char *name)
    3335{
     
    5961{
    6062    pipi_image_t *img;
    61     IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
     63    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 4);
    6264
    6365    if(!priv)
  • libpipi/trunk/pipi/codec/sdl.c

    r2605 r2629  
    2929#include "pipi_internals.h"
    3030
     31static SDL_Surface *create_32bpp_surface(int w, int h);
     32
    3133pipi_image_t *pipi_load_sdl(const char *name)
    3234{
     
    3941    if(priv->format->BytesPerPixel != 4)
    4042    {
    41         img = pipi_new(priv->w, priv->h);
    42         SDL_BlitSurface(priv, NULL, img->codec_priv, NULL);
     43        SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
     44        SDL_BlitSurface(priv, NULL, tmp, NULL);
    4345        SDL_FreeSurface(priv);
    44         return img;
     46        priv = tmp;
    4547    }
    4648
    47     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    48     memset(img, 0, sizeof(pipi_image_t));
    49 
    50     img->w = priv->w;
    51     img->h = priv->h;
    52 
    53     img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
    54     img->p[PIPI_PIXELS_RGBA32].w = priv->w;
    55     img->p[PIPI_PIXELS_RGBA32].h = priv->h;
    56     img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
    57     img->last_modified = PIPI_PIXELS_RGBA32;
    58 
    59     img->codec_priv = (void *)priv;
    60     img->codec_format = PIPI_PIXELS_RGBA32;
    61 
    62     return img;
    63 }
    64 
    65 pipi_image_t *pipi_new_sdl(int width, int height)
    66 {
    67     pipi_image_t *img;
    68     SDL_Surface *priv;
    69     Uint32 rmask, gmask, bmask, amask;
    70 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    71     rmask = 0xff000000;
    72     gmask = 0x00ff0000;
    73     bmask = 0x0000ff00;
    74     amask = 0x00000000;
    75 #else
    76     rmask = 0x000000ff;
    77     gmask = 0x0000ff00;
    78     bmask = 0x00ff0000;
    79     amask = 0x00000000;
    80 #endif
    81     priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
    82                                 rmask, gmask, bmask, amask);
    83 
    84     if(!priv)
    85         return NULL;
    86 
    87     img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
    88     memset(img, 0, sizeof(pipi_image_t));
    89 
    90     img->w = priv->w;
    91     img->h = priv->h;
     49    img = pipi_new(priv->w, priv->h);
    9250
    9351    img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
     
    10664{
    10765    SDL_FreeSurface(img->codec_priv);
    108 
    109     free(img);
    11066}
    11167
    11268void pipi_save_sdl(pipi_image_t *img, const char *name)
    11369{
     70    if(!img->codec_priv)
     71    {
     72        SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
     73
     74        /* FIXME: check pitch differences here */
     75        if(img->last_modified == PIPI_PIXELS_RGBA32)
     76        {
     77            memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA32].pixels,
     78                   priv->pitch * priv->h);
     79            free(img->p[PIPI_PIXELS_RGBA32].pixels);
     80        }
     81
     82        img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
     83        img->p[PIPI_PIXELS_RGBA32].w = priv->w;
     84        img->p[PIPI_PIXELS_RGBA32].h = priv->h;
     85        img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
     86
     87        img->codec_priv = (void *)priv;
     88        img->codec_format = PIPI_PIXELS_RGBA32;
     89    }
     90
    11491    pipi_getpixels(img, img->codec_format);
    11592    SDL_SaveBMP(img->codec_priv, name);
    11693}
    11794
     95/*
     96 * The following functions are local.
     97 */
     98
     99static SDL_Surface *create_32bpp_surface(int w, int h)
     100{
     101    Uint32 rmask, gmask, bmask, amask;
     102#if SDL_BYTEORDER == SDL_BIG_ENDIAN
     103    rmask = 0xff000000;
     104    gmask = 0x00ff0000;
     105    bmask = 0x0000ff00;
     106    amask = 0x00000000;
     107#else
     108    rmask = 0x000000ff;
     109    gmask = 0x0000ff00;
     110    bmask = 0x00ff0000;
     111    amask = 0x00000000;
     112#endif
     113
     114    return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
     115                                rmask, gmask, bmask, amask);
     116}
     117
  • libpipi/trunk/pipi/pipi.c

    r2605 r2629  
    3939*/
    4040
     41pipi_image_t *pipi_new(int w, int h)
     42{
     43    pipi_image_t *img;
     44
     45    img = malloc(sizeof(pipi_image_t));
     46    memset(img, 0, sizeof(pipi_image_t));
     47
     48    img->w = w;
     49    img->h = h;
     50    img->last_modified = PIPI_PIXELS_UNINITIALISED;
     51
     52    return img;
     53}
     54
  • libpipi/trunk/pipi/pipi.h

    r2617 r2629  
    3030typedef enum
    3131{
     32    PIPI_PIXELS_UNINITIALISED = -1,
     33
    3234    PIPI_PIXELS_RGBA32 = 0,
    3335    PIPI_PIXELS_RGBA_F = 1,
Note: See TracChangeset for help on using the changeset viewer.