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.
 
 
 
 
 
 

103 lines
2.3 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. pipi_image_t *pipi_load_sdl(const char *name)
  26. {
  27. pipi_image_t *img;
  28. SDL_Surface *priv = IMG_Load(name);
  29. if(!priv)
  30. return NULL;
  31. if(priv->format->BytesPerPixel == 1)
  32. {
  33. img = pipi_new(priv->w, priv->h);
  34. SDL_BlitSurface(priv, NULL, img->priv, NULL);
  35. SDL_FreeSurface(priv);
  36. return img;
  37. }
  38. img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
  39. img->width = priv->w;
  40. img->height = priv->h;
  41. img->pitch = priv->pitch;
  42. img->channels = priv->format->BytesPerPixel;
  43. img->pixels = priv->pixels;
  44. img->priv = (void *)priv;
  45. return img;
  46. }
  47. pipi_image_t *pipi_new_sdl(int width, int height)
  48. {
  49. pipi_image_t *img;
  50. SDL_Surface *priv;
  51. Uint32 rmask, gmask, bmask, amask;
  52. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  53. rmask = 0xff000000;
  54. gmask = 0x00ff0000;
  55. bmask = 0x0000ff00;
  56. amask = 0x00000000;
  57. #else
  58. rmask = 0x000000ff;
  59. gmask = 0x0000ff00;
  60. bmask = 0x00ff0000;
  61. amask = 0x00000000;
  62. #endif
  63. priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
  64. rmask, gmask, bmask, amask);
  65. if(!priv)
  66. return NULL;
  67. img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
  68. img->width = priv->w;
  69. img->height = priv->h;
  70. img->pitch = priv->pitch;
  71. img->channels = priv->format->BytesPerPixel;
  72. img->pixels = priv->pixels;
  73. img->priv = (void *)priv;
  74. return img;
  75. }
  76. void pipi_free_sdl(pipi_image_t *img)
  77. {
  78. SDL_FreeSurface(img->priv);
  79. free(img);
  80. }
  81. void pipi_save_sdl(pipi_image_t *img, const char *name)
  82. {
  83. SDL_SaveBMP(img->priv, name);
  84. }