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.
 
 
 
 
 
 

128 lines
3.2 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_RGBA_C].pixels = priv->pixels;
  41. img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
  42. img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
  43. img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
  44. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  45. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  46. img->last_modified = PIPI_PIXELS_RGBA_C;
  47. img->codec_priv = (void *)priv;
  48. img->codec_format = PIPI_PIXELS_RGBA_C;
  49. img->wrap = 0;
  50. img->u8 = 1;
  51. return img;
  52. }
  53. void pipi_free_sdl(pipi_image_t *img)
  54. {
  55. SDL_FreeSurface(img->codec_priv);
  56. }
  57. void pipi_save_sdl(pipi_image_t *img, const char *name)
  58. {
  59. if(!img->codec_priv)
  60. {
  61. SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
  62. /* FIXME: check pitch differences here */
  63. if(img->last_modified == PIPI_PIXELS_RGBA_C)
  64. {
  65. memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA_C].pixels,
  66. priv->pitch * priv->h);
  67. free(img->p[PIPI_PIXELS_RGBA_C].pixels);
  68. }
  69. img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels;
  70. img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
  71. img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
  72. img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
  73. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  74. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  75. img->codec_priv = (void *)priv;
  76. img->codec_format = PIPI_PIXELS_RGBA_C;
  77. img->wrap = 0;
  78. img->u8 = 1;
  79. }
  80. pipi_getpixels(img, img->codec_format);
  81. SDL_SaveBMP(img->codec_priv, name);
  82. }
  83. /*
  84. * The following functions are local.
  85. */
  86. static SDL_Surface *create_32bpp_surface(int w, int h)
  87. {
  88. Uint32 rmask, gmask, bmask, amask;
  89. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  90. rmask = 0xff000000;
  91. gmask = 0x00ff0000;
  92. bmask = 0x0000ff00;
  93. amask = 0x00000000;
  94. #else
  95. rmask = 0x000000ff;
  96. gmask = 0x0000ff00;
  97. bmask = 0x00ff0000;
  98. amask = 0x00000000;
  99. #endif
  100. return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
  101. rmask, gmask, bmask, amask);
  102. }