Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
You can specify the format for pixel data in SDL_RenderReadPixels() a…
Browse files Browse the repository at this point in the history
…nd SDL_RenderWritePixels()

This code still doesn't quite work yet. :)
  • Loading branch information
slouken committed Nov 16, 2009
1 parent 8e34778 commit aded421
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 50 deletions.
9 changes: 7 additions & 2 deletions include/SDL_video.h
Expand Up @@ -1173,22 +1173,26 @@ extern DECLSPEC int SDLCALL SDL_RenderCopy(SDL_TextureID textureID,
*
* \param rect A pointer to the rectangle to read, or NULL for the entire
* render target.
* \param pixels A pointer to be filled in with the pixel data in the rendering
* target format.
* \param format The desired format of the pixel data, or 0 to use the format
* of the rendering target
* \param pixels A pointer to be filled in with the pixel data
* \param pitch The pitch of the pixels parameter.
*
* \return 0 on success, or -1 if pixel reading is not supported.
*
* \warning This is a very slow operation, and should not be used frequently.
*/
extern DECLSPEC int SDLCALL SDL_RenderReadPixels(const SDL_Rect * rect,
Uint32 format,
void *pixels, int pitch);

/**
* \brief Write pixels to the current rendering target.
*
* \param rect A pointer to the rectangle to write, or NULL for the entire
* render target.
* \param format The format of the pixel data, or 0 to use the format
* of the rendering target
* \param pixels A pointer to the pixel data to write.
* \param pitch The pitch of the pixels parameter.
*
Expand All @@ -1197,6 +1201,7 @@ extern DECLSPEC int SDLCALL SDL_RenderReadPixels(const SDL_Rect * rect,
* \warning This is a very slow operation, and should not be used frequently.
*/
extern DECLSPEC int SDLCALL SDL_RenderWritePixels(const SDL_Rect * rect,
Uint32 format,
const void *pixels,
int pitch);

Expand Down
54 changes: 45 additions & 9 deletions src/video/SDL_renderer_gl.c
Expand Up @@ -103,9 +103,9 @@ static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
void * pixels, int pitch);
Uint32 pixel_format, void * pixels, int pitch);
static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
const void * pixels, int pitch);
Uint32 pixel_format, const void * pixels, int pitch);
static void GL_RenderPresent(SDL_Renderer * renderer);
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GL_DestroyRenderer(SDL_Renderer * renderer);
Expand Down Expand Up @@ -1254,16 +1254,16 @@ GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,

static int
GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
void * pixels, int pitch)
Uint32 pixel_format, void * pixels, int pitch)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
Uint32 pixel_format = display->current_mode.format;
GLint internalFormat;
GLenum format, type;
Uint8 *src, *dst, *tmp;
int length, rows;

if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
/* FIXME: Do a temp copy to a format that is supported */
SDL_SetError("Unsupported pixel format");
return -1;
}
Expand All @@ -1275,16 +1275,52 @@ GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
}
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
data->glPixelStorei(GL_PACK_ROW_LENGTH,
-2 * (pitch / bytes_per_pixel(pixel_format)));
(pitch / bytes_per_pixel(pixel_format)));

data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h,
format, type, pixels + (rect->h-1)*pitch);
format, type, pixels);

/* Flip the rows to be top-down */
length = rect->w * bytes_per_pixel(pixel_format);
src = (Uint8*)pixels + (rect->h-1)*pitch;
dst = (Uint8*)pixels;
tmp = SDL_stack_alloc(Uint8, length);
rows = rect->h / 2;
while (rows--) {
SDL_memcpy(tmp, dst, length);
SDL_memcpy(dst, src, length);
SDL_memcpy(src, tmp, length);
}
SDL_stack_free(tmp);
}

static int
GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
const void * pixels, int pitch)
Uint32 pixel_format, const void * pixels, int pitch)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
GLint internalFormat;
GLenum format, type;

if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
/* FIXME: Do a temp copy to a format that is supported */
SDL_SetError("Unsupported pixel format");
return -1;
}

/* FIXME: We need to copy the data and flip it */

if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) {
data->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
} else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) {
data->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
}
data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
data->glPixelStorei(GL_UNPACK_ROW_LENGTH,
(pitch / bytes_per_pixel(pixel_format)));

data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h,
format, type, pixels);
}

static void
Expand Down
41 changes: 9 additions & 32 deletions src/video/SDL_renderer_sw.c
Expand Up @@ -66,9 +66,9 @@ static int SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
void * pixels, int pitch);
Uint32 format, void * pixels, int pitch);
static int SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
const void * pixels, int pitch);
Uint32 format, const void * pixels, int pitch);
static void SW_RenderPresent(SDL_Renderer * renderer);
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void SW_DestroyRenderer(SDL_Renderer * renderer);
Expand Down Expand Up @@ -736,12 +736,9 @@ SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,

static int
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
void * pixels, int pitch)
Uint32 format, void * pixels, int pitch)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
const Uint8 *src;
Uint8 *dst;
int src_pitch, dst_pitch, w, h;

if (data->renderer->LockTexture(data->renderer,
data->texture[data->current_texture],
Expand All @@ -750,17 +747,9 @@ SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return -1;
}

src = data->surface.pixels;
src_pitch = data->surface.pitch;
dst = pixels;
dst_pitch = pitch;
h = rect->h;
w = rect->w * data->surface.format->BytesPerPixel;
while (h--) {
SDL_memcpy(dst, src, w);
src += src_pitch;
dst += dst_pitch;
}
SDL_ConvertPixels(rect->w, rect->h,
data->format, data->surface.pixels, data->surface.pitch,
format, pixels, pitch);

data->renderer->UnlockTexture(data->renderer,
data->texture[data->current_texture]);
Expand All @@ -769,12 +758,9 @@ SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,

static int
SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
const void * pixels, int pitch)
Uint32 format, const void * pixels, int pitch)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
const Uint8 *src;
Uint8 *dst;
int src_pitch, dst_pitch, w, h;

if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
SDL_AddDirtyRect(&data->dirty, rect);
Expand All @@ -787,17 +773,8 @@ SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
return -1;
}

src = pixels;
src_pitch = pitch;
dst = data->surface.pixels;
dst_pitch = data->surface.pitch;
h = rect->h;
w = rect->w * data->surface.format->BytesPerPixel;
while (h--) {
SDL_memcpy(dst, src, w);
src += src_pitch;
dst += dst_pitch;
}
SDL_ConvertPixels(rect->w, rect->h, format, pixels, pitch,
data->format, data->surface.pixels, data->surface.pitch);

data->renderer->UnlockTexture(data->renderer,
data->texture[data->current_texture]);
Expand Down
4 changes: 2 additions & 2 deletions src/video/SDL_sysvideo.h
Expand Up @@ -97,9 +97,9 @@ struct SDL_Renderer
int (*RenderCopy) (SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
void * pixels, int pitch);
Uint32 format, void * pixels, int pitch);
int (*RenderWritePixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
const void * pixels, int pitch);
Uint32 format, const void * pixels, int pitch);
void (*RenderPresent) (SDL_Renderer * renderer);
void (*DestroyTexture) (SDL_Renderer * renderer, SDL_Texture * texture);

Expand Down
20 changes: 16 additions & 4 deletions src/video/SDL_video.c
Expand Up @@ -2487,7 +2487,8 @@ SDL_RenderCopy(SDL_TextureID textureID, const SDL_Rect * srcrect,
}

int
SDL_RenderReadPixels(const SDL_Rect * rect, void * pixels, int pitch)
SDL_RenderReadPixels(const SDL_Rect * rect, Uint32 format,
void * pixels, int pitch)
{
SDL_Renderer *renderer;
SDL_Window *window;
Expand All @@ -2503,6 +2504,10 @@ SDL_RenderReadPixels(const SDL_Rect * rect, void * pixels, int pitch)
}
window = SDL_GetWindowFromID(renderer->window);

if (!format) {
format = SDL_GetDisplayFromWindow(window)->current_mode.format;
}

real_rect.x = 0;
real_rect.y = 0;
real_rect.w = window->w;
Expand All @@ -2521,11 +2526,13 @@ SDL_RenderReadPixels(const SDL_Rect * rect, void * pixels, int pitch)
}
}

return renderer->RenderReadPixels(renderer, &real_rect, pixels, pitch);
return renderer->RenderReadPixels(renderer, &real_rect,
format, pixels, pitch);
}

int
SDL_RenderWritePixels(const SDL_Rect * rect, const void * pixels, int pitch)
SDL_RenderWritePixels(const SDL_Rect * rect, Uint32 format,
const void * pixels, int pitch)
{
SDL_Renderer *renderer;
SDL_Window *window;
Expand All @@ -2541,6 +2548,10 @@ SDL_RenderWritePixels(const SDL_Rect * rect, const void * pixels, int pitch)
}
window = SDL_GetWindowFromID(renderer->window);

if (!format) {
format = SDL_GetDisplayFromWindow(window)->current_mode.format;
}

real_rect.x = 0;
real_rect.y = 0;
real_rect.w = window->w;
Expand All @@ -2559,7 +2570,8 @@ SDL_RenderWritePixels(const SDL_Rect * rect, const void * pixels, int pitch)
}
}

return renderer->RenderWritePixels(renderer, &real_rect, pixels, pitch);
return renderer->RenderWritePixels(renderer, &real_rect,
format, pixels, pitch);
}

void
Expand Down
2 changes: 2 additions & 0 deletions test/automated/common/common.h
Expand Up @@ -12,11 +12,13 @@


#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
# define FORMAT SDL_PIXELFORMAT_RGBA8888
# define RMASK 0xff000000 /**< Red bit mask. */
# define GMASK 0x00ff0000 /**< Green bit mask. */
# define BMASK 0x0000ff00 /**< Blue bit mask. */
# define AMASK 0x000000ff /**< Alpha bit mask. */
#else
# define FORMAT SDL_PIXELFORMAT_ABGR8888
# define RMASK 0x000000ff /**< Red bit mask. */
# define GMASK 0x0000ff00 /**< Green bit mask. */
# define BMASK 0x00ff0000 /**< Blue bit mask. */
Expand Down
2 changes: 1 addition & 1 deletion test/automated/render/render.c
Expand Up @@ -67,7 +67,7 @@ static int render_compare( const char *msg, const SurfaceImage_t *s )
return 1;

/* Read pixels. */
ret = SDL_RenderReadPixels( NULL, pix, 80*4 );
ret = SDL_RenderReadPixels( NULL, FORMAT, pix, 80*4 );
if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
return 1;

Expand Down

0 comments on commit aded421

Please sign in to comment.