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.
 
 
 
 
 
 

135 lines
3.3 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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 int pipi_free_sdl(pipi_image_t *);
  26. static SDL_Surface *create_32bpp_surface(int w, int h);
  27. pipi_image_t *pipi_load_sdl(const char *name)
  28. {
  29. pipi_image_t *img;
  30. SDL_Surface *priv = IMG_Load(name);
  31. if(!priv)
  32. return NULL;
  33. if(priv->format->BytesPerPixel != 4)
  34. {
  35. SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
  36. SDL_BlitSurface(priv, NULL, tmp, NULL);
  37. SDL_FreeSurface(priv);
  38. priv = tmp;
  39. }
  40. img = pipi_new(priv->w, priv->h);
  41. img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels;
  42. img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
  43. img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
  44. img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
  45. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  46. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  47. img->last_modified = PIPI_PIXELS_RGBA_C;
  48. img->codec_priv = (void *)priv;
  49. img->codec_format = PIPI_PIXELS_RGBA_C;
  50. img->codec_free = pipi_free_sdl;
  51. img->wrap = 0;
  52. img->u8 = 1;
  53. return img;
  54. }
  55. int pipi_save_sdl(pipi_image_t *img, const char *name)
  56. {
  57. if(!img->codec_priv)
  58. {
  59. SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
  60. /* FIXME: check pitch differences here */
  61. if(img->last_modified == PIPI_PIXELS_RGBA_C)
  62. {
  63. memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA_C].pixels,
  64. priv->pitch * priv->h);
  65. free(img->p[PIPI_PIXELS_RGBA_C].pixels);
  66. }
  67. img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels;
  68. img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
  69. img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
  70. img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
  71. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  72. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  73. img->codec_priv = (void *)priv;
  74. img->codec_format = PIPI_PIXELS_RGBA_C;
  75. img->codec_free = pipi_free_sdl;
  76. img->wrap = 0;
  77. img->u8 = 1;
  78. }
  79. pipi_getpixels(img, img->codec_format);
  80. SDL_SaveBMP(img->codec_priv, name);
  81. return 0;
  82. }
  83. /*
  84. * XXX: The following functions are local.
  85. */
  86. static int pipi_free_sdl(pipi_image_t *img)
  87. {
  88. SDL_FreeSurface(img->codec_priv);
  89. return 0;
  90. }
  91. static SDL_Surface *create_32bpp_surface(int w, int h)
  92. {
  93. Uint32 rmask, gmask, bmask, amask;
  94. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  95. rmask = 0xff000000;
  96. gmask = 0x00ff0000;
  97. bmask = 0x0000ff00;
  98. amask = 0x00000000;
  99. #else
  100. rmask = 0x000000ff;
  101. gmask = 0x0000ff00;
  102. bmask = 0x00ff0000;
  103. amask = 0x00000000;
  104. #endif
  105. return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
  106. rmask, gmask, bmask, amask);
  107. }