選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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