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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.