You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
2.9 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * sdl.c: SDL_image I/O functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <SDL_image.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. static SDL_Surface *create_32bpp_surface(int w, int h);
  26. pipi_image_t *pipi_load_sdl(const char *name)
  27. {
  28. pipi_image_t *img;
  29. SDL_Surface *priv = IMG_Load(name);
  30. if(!priv)
  31. return NULL;
  32. if(priv->format->BytesPerPixel != 4)
  33. {
  34. SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
  35. SDL_BlitSurface(priv, NULL, tmp, NULL);
  36. SDL_FreeSurface(priv);
  37. priv = tmp;
  38. }
  39. img = pipi_new(priv->w, priv->h);
  40. img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
  41. img->p[PIPI_PIXELS_RGBA32].w = priv->w;
  42. img->p[PIPI_PIXELS_RGBA32].h = priv->h;
  43. img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
  44. img->last_modified = PIPI_PIXELS_RGBA32;
  45. img->codec_priv = (void *)priv;
  46. img->codec_format = PIPI_PIXELS_RGBA32;
  47. return img;
  48. }
  49. void pipi_free_sdl(pipi_image_t *img)
  50. {
  51. SDL_FreeSurface(img->codec_priv);
  52. }
  53. void pipi_save_sdl(pipi_image_t *img, const char *name)
  54. {
  55. if(!img->codec_priv)
  56. {
  57. SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
  58. /* FIXME: check pitch differences here */
  59. if(img->last_modified == PIPI_PIXELS_RGBA32)
  60. {
  61. memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA32].pixels,
  62. priv->pitch * priv->h);
  63. free(img->p[PIPI_PIXELS_RGBA32].pixels);
  64. }
  65. img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
  66. img->p[PIPI_PIXELS_RGBA32].w = priv->w;
  67. img->p[PIPI_PIXELS_RGBA32].h = priv->h;
  68. img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
  69. img->codec_priv = (void *)priv;
  70. img->codec_format = PIPI_PIXELS_RGBA32;
  71. }
  72. pipi_getpixels(img, img->codec_format);
  73. SDL_SaveBMP(img->codec_priv, name);
  74. }
  75. /*
  76. * The following functions are local.
  77. */
  78. static SDL_Surface *create_32bpp_surface(int w, int h)
  79. {
  80. Uint32 rmask, gmask, bmask, amask;
  81. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  82. rmask = 0xff000000;
  83. gmask = 0x00ff0000;
  84. bmask = 0x0000ff00;
  85. amask = 0x00000000;
  86. #else
  87. rmask = 0x000000ff;
  88. gmask = 0x0000ff00;
  89. bmask = 0x00ff0000;
  90. amask = 0x00000000;
  91. #endif
  92. return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
  93. rmask, gmask, bmask, amask);
  94. }