bug-guile-sdl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (Guile-SDL) Feature request - Access pixels of a surface


From: David Thompson
Subject: Re: (Guile-SDL) Feature request - Access pixels of a surface
Date: Sat, 01 Jun 2013 13:49:48 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130514 Thunderbird/17.0.6

On 06/01/2013 11:29 AM, Thien-Thi Nguyen wrote:
OK, sounds like a good way to start.  Here is a sketch that you can copy
into src/sdlsurface.c somewhere:

  PRIMPROC
  (surface_pixels, "surface-pixels", 1, 0, 0,
   (SCM surface),
   doc: /***********
  Return the pixel data of @var{surface} as a new SRFI 4 u8vector.
  If there are problems, return @code{#f}.  */)
  {
  #define FUNC_NAME s_surface_pixels
    SDL_Surface *src;
    size_t len;
    void *pixels;
ASSERT_SURFACE (surface, 1);
    src = UNPACK_SURFACE (surface);
    len = src->w * src->h * src->format->BytesPerPixel;
    if (! (pixels = malloc (len)))
      RETURN_FALSE;
    if (! memcpy (pixels, src->pixels, len))
      {
        /* Should never get here, since ‘memcpy’ returns ‘pixels’,
           but who knows?  */
        free (pixels);
        RETURN_FALSE;
      }
    return scm_take_u8vector (pixels, len);
  #undef FUNC_NAME
  }

You can modify test/image.scm to test it like so:

  (let* ((gnu-head (SDL:load-image (datafile "gnu-goatee.jpg")))
         (v (SDL:surface-pixels gnu-head)))
    (fso "gnu-head => ~S (pixels: ~A)~%"
         gnu-head
         (if (u8vector? v)
             (fs "u8vector length ~A" (u8vector-length v))
             'unavailable))
    (SDL:blit-surface gnu-head gnu-rect (SDL:get-video-surface) gnu-rect))

If it works for you, i'll include it in the next release.  On the other
hand, if there are problems, please let me know soon so that we can fix
those first, and THEN i'll include it in the next release.  :-D

Works great for me. I had to add a line `src/sdl.tsar` in order for the procedure to be exported. Could you explain a bit more the format of this file? I'm not sure what the numbers mean before the procedure name.

Thanks!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]