SDL  2.0
SDL_surface.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../SDL_internal.h"
22 
23 #include "SDL_video.h"
24 #include "SDL_sysvideo.h"
25 #include "SDL_blit.h"
26 #include "SDL_RLEaccel_c.h"
27 #include "SDL_pixels_c.h"
28 
29 /* Public routines */
30 
31 /*
32  * Create an empty RGB surface of the appropriate depth using the given
33  * enum SDL_PIXELFORMAT_* format
34  */
37  Uint32 format)
38 {
39  Sint64 pitch;
40  SDL_Surface *surface;
41 
42  /* The flags are no longer used, make the compiler happy */
43  (void)flags;
44 
45  /* Allocate the surface */
46  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
47  if (surface == NULL) {
49  return NULL;
50  }
51 
52  surface->format = SDL_AllocFormat(format);
53  if (!surface->format) {
54  SDL_FreeSurface(surface);
55  return NULL;
56  }
57 
58  surface->w = width;
59  surface->h = height;
60 
61  pitch = SDL_CalculatePitch(surface);
62  if (pitch < 0 || pitch > SDL_MAX_SINT32) {
63  /* Overflow... */
65  return NULL;
66  }
67  surface->pitch = (int)pitch;
68 
69  SDL_SetClipRect(surface, NULL);
70 
71  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
72  SDL_Palette *palette =
73  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
74  if (!palette) {
75  SDL_FreeSurface(surface);
76  return NULL;
77  }
78  if (palette->ncolors == 2) {
79  /* Create a black and white bitmap palette */
80  palette->colors[0].r = 0xFF;
81  palette->colors[0].g = 0xFF;
82  palette->colors[0].b = 0xFF;
83  palette->colors[1].r = 0x00;
84  palette->colors[1].g = 0x00;
85  palette->colors[1].b = 0x00;
86  }
87  SDL_SetSurfacePalette(surface, palette);
88  SDL_FreePalette(palette);
89  }
90 
91  /* Get the pixels */
92  if (surface->w && surface->h) {
93  surface->pixels = SDL_malloc(surface->h * surface->pitch);
94  if (!surface->pixels) {
95  SDL_FreeSurface(surface);
97  return NULL;
98  }
99  /* This is important for bitmaps */
100  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
101  }
102 
103  /* Allocate an empty mapping */
104  surface->map = SDL_AllocBlitMap();
105  if (!surface->map) {
106  SDL_FreeSurface(surface);
107  return NULL;
108  }
109 
110  /* By default surface with an alpha mask are set up for blending */
111  if (surface->format->Amask) {
113  }
114 
115  /* The surface is ready to go */
116  surface->refcount = 1;
117  return surface;
118 }
119 
120 /*
121  * Create an empty RGB surface of the appropriate depth
122  */
123 SDL_Surface *
125  int width, int height, int depth,
126  Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
127 {
128  Uint32 format;
129 
130  /* Get the pixel format */
131  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
132  if (format == SDL_PIXELFORMAT_UNKNOWN) {
133  SDL_SetError("Unknown pixel format");
134  return NULL;
135  }
136 
137  return SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
138 }
139 
140 /*
141  * Create an RGB surface from an existing memory buffer
142  */
143 SDL_Surface *
145  int width, int height, int depth, int pitch,
146  Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
147  Uint32 Amask)
148 {
149  SDL_Surface *surface;
150 
151  surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
152  if (surface != NULL) {
153  surface->flags |= SDL_PREALLOC;
154  surface->pixels = pixels;
155  surface->w = width;
156  surface->h = height;
157  surface->pitch = pitch;
158  SDL_SetClipRect(surface, NULL);
159  }
160  return surface;
161 }
162 
163 /*
164  * Create an RGB surface from an existing memory buffer using the given given
165  * enum SDL_PIXELFORMAT_* format
166  */
167 SDL_Surface *
169  int width, int height, int depth, int pitch,
170  Uint32 format)
171 {
172  SDL_Surface *surface;
173 
174  surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
175  if (surface != NULL) {
176  surface->flags |= SDL_PREALLOC;
177  surface->pixels = pixels;
178  surface->w = width;
179  surface->h = height;
180  surface->pitch = pitch;
181  SDL_SetClipRect(surface, NULL);
182  }
183  return surface;
184 }
185 
186 int
188 {
189  if (!surface) {
190  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
191  }
192  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
193  return -1;
194  }
195  SDL_InvalidateMap(surface->map);
196 
197  return 0;
198 }
199 
200 int
201 SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
202 {
203  int flags;
204 
205  if (!surface) {
206  return -1;
207  }
208 
209  flags = surface->map->info.flags;
210  if (flag) {
211  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
212  } else {
213  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
214  }
215  if (surface->map->info.flags != flags) {
216  SDL_InvalidateMap(surface->map);
217  }
218  return 0;
219 }
220 
221 int
222 SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
223 {
224  int flags;
225 
226  if (!surface) {
227  return SDL_InvalidParamError("surface");
228  }
229 
230  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
231  return SDL_InvalidParamError("key");
232  }
233 
234  if (flag & SDL_RLEACCEL) {
235  SDL_SetSurfaceRLE(surface, 1);
236  }
237 
238  flags = surface->map->info.flags;
239  if (flag) {
240  surface->map->info.flags |= SDL_COPY_COLORKEY;
241  surface->map->info.colorkey = key;
242  if (surface->format->palette) {
243  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
244  ++surface->format->palette->version;
245  if (!surface->format->palette->version) {
246  surface->format->palette->version = 1;
247  }
248  }
249  } else {
250  if (surface->format->palette) {
251  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
252  ++surface->format->palette->version;
253  if (!surface->format->palette->version) {
254  surface->format->palette->version = 1;
255  }
256  }
257  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
258  }
259  if (surface->map->info.flags != flags) {
260  SDL_InvalidateMap(surface->map);
261  }
262 
263  return 0;
264 }
265 
266 int
268 {
269  if (!surface) {
270  return -1;
271  }
272 
273  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
274  return -1;
275  }
276 
277  if (key) {
278  *key = surface->map->info.colorkey;
279  }
280  return 0;
281 }
282 
283 /* This is a fairly slow function to switch from colorkey to alpha */
284 static void
286 {
287  int x, y;
288 
289  if (!surface) {
290  return;
291  }
292 
293  if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
294  !surface->format->Amask) {
295  return;
296  }
297 
298  SDL_LockSurface(surface);
299 
300  switch (surface->format->BytesPerPixel) {
301  case 2:
302  {
303  Uint16 *row, *spot;
304  Uint16 ckey = (Uint16) surface->map->info.colorkey;
305  Uint16 mask = (Uint16) (~surface->format->Amask);
306 
307  /* Ignore alpha in colorkey comparison */
308  ckey &= mask;
309  row = (Uint16 *) surface->pixels;
310  for (y = surface->h; y--;) {
311  spot = row;
312  for (x = surface->w; x--;) {
313  if ((*spot & mask) == ckey) {
314  *spot &= mask;
315  }
316  ++spot;
317  }
318  row += surface->pitch / 2;
319  }
320  }
321  break;
322  case 3:
323  /* FIXME */
324  break;
325  case 4:
326  {
327  Uint32 *row, *spot;
328  Uint32 ckey = surface->map->info.colorkey;
329  Uint32 mask = ~surface->format->Amask;
330 
331  /* Ignore alpha in colorkey comparison */
332  ckey &= mask;
333  row = (Uint32 *) surface->pixels;
334  for (y = surface->h; y--;) {
335  spot = row;
336  for (x = surface->w; x--;) {
337  if ((*spot & mask) == ckey) {
338  *spot &= mask;
339  }
340  ++spot;
341  }
342  row += surface->pitch / 4;
343  }
344  }
345  break;
346  }
347 
348  SDL_UnlockSurface(surface);
349 
350  SDL_SetColorKey(surface, 0, 0);
352 }
353 
354 int
356 {
357  int flags;
358 
359  if (!surface) {
360  return -1;
361  }
362 
363  surface->map->info.r = r;
364  surface->map->info.g = g;
365  surface->map->info.b = b;
366 
367  flags = surface->map->info.flags;
368  if (r != 0xFF || g != 0xFF || b != 0xFF) {
369  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
370  } else {
371  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
372  }
373  if (surface->map->info.flags != flags) {
374  SDL_InvalidateMap(surface->map);
375  }
376  return 0;
377 }
378 
379 
380 int
382 {
383  if (!surface) {
384  return -1;
385  }
386 
387  if (r) {
388  *r = surface->map->info.r;
389  }
390  if (g) {
391  *g = surface->map->info.g;
392  }
393  if (b) {
394  *b = surface->map->info.b;
395  }
396  return 0;
397 }
398 
399 int
401 {
402  int flags;
403 
404  if (!surface) {
405  return -1;
406  }
407 
408  surface->map->info.a = alpha;
409 
410  flags = surface->map->info.flags;
411  if (alpha != 0xFF) {
412  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
413  } else {
414  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
415  }
416  if (surface->map->info.flags != flags) {
417  SDL_InvalidateMap(surface->map);
418  }
419  return 0;
420 }
421 
422 int
424 {
425  if (!surface) {
426  return -1;
427  }
428 
429  if (alpha) {
430  *alpha = surface->map->info.a;
431  }
432  return 0;
433 }
434 
435 int
437 {
438  int flags, status;
439 
440  if (!surface) {
441  return -1;
442  }
443 
444  status = 0;
445  flags = surface->map->info.flags;
446  surface->map->info.flags &=
448  switch (blendMode) {
449  case SDL_BLENDMODE_NONE:
450  break;
451  case SDL_BLENDMODE_BLEND:
452  surface->map->info.flags |= SDL_COPY_BLEND;
453  break;
454  case SDL_BLENDMODE_ADD:
455  surface->map->info.flags |= SDL_COPY_ADD;
456  break;
457  case SDL_BLENDMODE_MOD:
458  surface->map->info.flags |= SDL_COPY_MOD;
459  break;
460  default:
461  status = SDL_Unsupported();
462  break;
463  }
464 
465  if (surface->map->info.flags != flags) {
466  SDL_InvalidateMap(surface->map);
467  }
468 
469  return status;
470 }
471 
472 int
474 {
475  if (!surface) {
476  return -1;
477  }
478 
479  if (!blendMode) {
480  return 0;
481  }
482 
483  switch (surface->map->
484  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
485  case SDL_COPY_BLEND:
486  *blendMode = SDL_BLENDMODE_BLEND;
487  break;
488  case SDL_COPY_ADD:
489  *blendMode = SDL_BLENDMODE_ADD;
490  break;
491  case SDL_COPY_MOD:
492  *blendMode = SDL_BLENDMODE_MOD;
493  break;
494  default:
495  *blendMode = SDL_BLENDMODE_NONE;
496  break;
497  }
498  return 0;
499 }
500 
501 SDL_bool
503 {
504  SDL_Rect full_rect;
505 
506  /* Don't do anything if there's no surface to act on */
507  if (!surface) {
508  return SDL_FALSE;
509  }
510 
511  /* Set up the full surface rectangle */
512  full_rect.x = 0;
513  full_rect.y = 0;
514  full_rect.w = surface->w;
515  full_rect.h = surface->h;
516 
517  /* Set the clipping rectangle */
518  if (!rect) {
519  surface->clip_rect = full_rect;
520  return SDL_TRUE;
521  }
522  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
523 }
524 
525 void
527 {
528  if (surface && rect) {
529  *rect = surface->clip_rect;
530  }
531 }
532 
533 /*
534  * Set up a blit between two surfaces -- split into three parts:
535  * The upper part, SDL_UpperBlit(), performs clipping and rectangle
536  * verification. The lower part is a pointer to a low level
537  * accelerated blitting function.
538  *
539  * These parts are separated out and each used internally by this
540  * library in the optimimum places. They are exported so that if
541  * you know exactly what you are doing, you can optimize your code
542  * by calling the one(s) you need.
543  */
544 int
546  SDL_Surface * dst, SDL_Rect * dstrect)
547 {
548  /* Check to make sure the blit mapping is valid */
549  if ((src->map->dst != dst) ||
550  (dst->format->palette &&
551  src->map->dst_palette_version != dst->format->palette->version) ||
552  (src->format->palette &&
553  src->map->src_palette_version != src->format->palette->version)) {
554  if (SDL_MapSurface(src, dst) < 0) {
555  return (-1);
556  }
557  /* just here for debugging */
558 /* printf */
559 /* ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
560 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
561 /* dst->map->info.flags, src->map->blit); */
562  }
563  return (src->map->blit(src, srcrect, dst, dstrect));
564 }
565 
566 
567 int
569  SDL_Surface * dst, SDL_Rect * dstrect)
570 {
571  SDL_Rect fulldst;
572  int srcx, srcy, w, h;
573 
574  /* Make sure the surfaces aren't locked */
575  if (!src || !dst) {
576  return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
577  }
578  if (src->locked || dst->locked) {
579  return SDL_SetError("Surfaces must not be locked during blit");
580  }
581 
582  /* If the destination rectangle is NULL, use the entire dest surface */
583  if (dstrect == NULL) {
584  fulldst.x = fulldst.y = 0;
585  fulldst.w = dst->w;
586  fulldst.h = dst->h;
587  dstrect = &fulldst;
588  }
589 
590  /* clip the source rectangle to the source surface */
591  if (srcrect) {
592  int maxw, maxh;
593 
594  srcx = srcrect->x;
595  w = srcrect->w;
596  if (srcx < 0) {
597  w += srcx;
598  dstrect->x -= srcx;
599  srcx = 0;
600  }
601  maxw = src->w - srcx;
602  if (maxw < w)
603  w = maxw;
604 
605  srcy = srcrect->y;
606  h = srcrect->h;
607  if (srcy < 0) {
608  h += srcy;
609  dstrect->y -= srcy;
610  srcy = 0;
611  }
612  maxh = src->h - srcy;
613  if (maxh < h)
614  h = maxh;
615 
616  } else {
617  srcx = srcy = 0;
618  w = src->w;
619  h = src->h;
620  }
621 
622  /* clip the destination rectangle against the clip rectangle */
623  {
624  SDL_Rect *clip = &dst->clip_rect;
625  int dx, dy;
626 
627  dx = clip->x - dstrect->x;
628  if (dx > 0) {
629  w -= dx;
630  dstrect->x += dx;
631  srcx += dx;
632  }
633  dx = dstrect->x + w - clip->x - clip->w;
634  if (dx > 0)
635  w -= dx;
636 
637  dy = clip->y - dstrect->y;
638  if (dy > 0) {
639  h -= dy;
640  dstrect->y += dy;
641  srcy += dy;
642  }
643  dy = dstrect->y + h - clip->y - clip->h;
644  if (dy > 0)
645  h -= dy;
646  }
647 
648  /* Switch back to a fast blit if we were previously stretching */
649  if (src->map->info.flags & SDL_COPY_NEAREST) {
650  src->map->info.flags &= ~SDL_COPY_NEAREST;
651  SDL_InvalidateMap(src->map);
652  }
653 
654  if (w > 0 && h > 0) {
655  SDL_Rect sr;
656  sr.x = srcx;
657  sr.y = srcy;
658  sr.w = dstrect->w = w;
659  sr.h = dstrect->h = h;
660  return SDL_LowerBlit(src, &sr, dst, dstrect);
661  }
662  dstrect->w = dstrect->h = 0;
663  return 0;
664 }
665 
666 int
668  SDL_Surface * dst, SDL_Rect * dstrect)
669 {
670  double src_x0, src_y0, src_x1, src_y1;
671  double dst_x0, dst_y0, dst_x1, dst_y1;
672  SDL_Rect final_src, final_dst;
673  double scaling_w, scaling_h;
674  int src_w, src_h;
675  int dst_w, dst_h;
676 
677  /* Make sure the surfaces aren't locked */
678  if (!src || !dst) {
679  return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
680  }
681  if (src->locked || dst->locked) {
682  return SDL_SetError("Surfaces must not be locked during blit");
683  }
684 
685  if (NULL == srcrect) {
686  src_w = src->w;
687  src_h = src->h;
688  } else {
689  src_w = srcrect->w;
690  src_h = srcrect->h;
691  }
692 
693  if (NULL == dstrect) {
694  dst_w = dst->w;
695  dst_h = dst->h;
696  } else {
697  dst_w = dstrect->w;
698  dst_h = dstrect->h;
699  }
700 
701  if (dst_w == src_w && dst_h == src_h) {
702  /* No scaling, defer to regular blit */
703  return SDL_BlitSurface(src, srcrect, dst, dstrect);
704  }
705 
706  scaling_w = (double)dst_w / src_w;
707  scaling_h = (double)dst_h / src_h;
708 
709  if (NULL == dstrect) {
710  dst_x0 = 0;
711  dst_y0 = 0;
712  dst_x1 = dst_w - 1;
713  dst_y1 = dst_h - 1;
714  } else {
715  dst_x0 = dstrect->x;
716  dst_y0 = dstrect->y;
717  dst_x1 = dst_x0 + dst_w - 1;
718  dst_y1 = dst_y0 + dst_h - 1;
719  }
720 
721  if (NULL == srcrect) {
722  src_x0 = 0;
723  src_y0 = 0;
724  src_x1 = src_w - 1;
725  src_y1 = src_h - 1;
726  } else {
727  src_x0 = srcrect->x;
728  src_y0 = srcrect->y;
729  src_x1 = src_x0 + src_w - 1;
730  src_y1 = src_y0 + src_h - 1;
731 
732  /* Clip source rectangle to the source surface */
733 
734  if (src_x0 < 0) {
735  dst_x0 -= src_x0 * scaling_w;
736  src_x0 = 0;
737  }
738 
739  if (src_x1 >= src->w) {
740  dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
741  src_x1 = src->w - 1;
742  }
743 
744  if (src_y0 < 0) {
745  dst_y0 -= src_y0 * scaling_h;
746  src_y0 = 0;
747  }
748 
749  if (src_y1 >= src->h) {
750  dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
751  src_y1 = src->h - 1;
752  }
753  }
754 
755  /* Clip destination rectangle to the clip rectangle */
756 
757  /* Translate to clip space for easier calculations */
758  dst_x0 -= dst->clip_rect.x;
759  dst_x1 -= dst->clip_rect.x;
760  dst_y0 -= dst->clip_rect.y;
761  dst_y1 -= dst->clip_rect.y;
762 
763  if (dst_x0 < 0) {
764  src_x0 -= dst_x0 / scaling_w;
765  dst_x0 = 0;
766  }
767 
768  if (dst_x1 >= dst->clip_rect.w) {
769  src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
770  dst_x1 = dst->clip_rect.w - 1;
771  }
772 
773  if (dst_y0 < 0) {
774  src_y0 -= dst_y0 / scaling_h;
775  dst_y0 = 0;
776  }
777 
778  if (dst_y1 >= dst->clip_rect.h) {
779  src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
780  dst_y1 = dst->clip_rect.h - 1;
781  }
782 
783  /* Translate back to surface coordinates */
784  dst_x0 += dst->clip_rect.x;
785  dst_x1 += dst->clip_rect.x;
786  dst_y0 += dst->clip_rect.y;
787  dst_y1 += dst->clip_rect.y;
788 
789  final_src.x = (int)SDL_floor(src_x0 + 0.5);
790  final_src.y = (int)SDL_floor(src_y0 + 0.5);
791  final_src.w = (int)SDL_floor(src_x1 - src_x0 + 1.5);
792  final_src.h = (int)SDL_floor(src_y1 - src_y0 + 1.5);
793 
794  final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
795  final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
796  final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
797  final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
798 
799  if (final_dst.w < 0)
800  final_dst.w = 0;
801  if (final_dst.h < 0)
802  final_dst.h = 0;
803 
804  if (dstrect)
805  *dstrect = final_dst;
806 
807  if (final_dst.w == 0 || final_dst.h == 0 ||
808  final_src.w <= 0 || final_src.h <= 0) {
809  /* No-op. */
810  return 0;
811  }
812 
813  return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
814 }
815 
816 /**
817  * This is a semi-private blit function and it performs low-level surface
818  * scaled blitting only.
819  */
820 int
822  SDL_Surface * dst, SDL_Rect * dstrect)
823 {
824  static const Uint32 complex_copy_flags = (
828  );
829 
830  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
831  src->map->info.flags |= SDL_COPY_NEAREST;
832  SDL_InvalidateMap(src->map);
833  }
834 
835  if ( !(src->map->info.flags & complex_copy_flags) &&
836  src->format->format == dst->format->format &&
838  return SDL_SoftStretch( src, srcrect, dst, dstrect );
839  } else {
840  return SDL_LowerBlit( src, srcrect, dst, dstrect );
841  }
842 }
843 
844 /*
845  * Lock a surface to directly access the pixels
846  */
847 int
849 {
850  if (!surface->locked) {
851  /* Perform the lock */
852  if (surface->flags & SDL_RLEACCEL) {
853  SDL_UnRLESurface(surface, 1);
854  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
855  }
856  }
857 
858  /* Increment the surface lock count, for recursive locks */
859  ++surface->locked;
860 
861  /* Ready to go.. */
862  return (0);
863 }
864 
865 /*
866  * Unlock a previously locked surface
867  */
868 void
870 {
871  /* Only perform an unlock if we are locked */
872  if (!surface->locked || (--surface->locked > 0)) {
873  return;
874  }
875 
876  /* Update RLE encoded surface with new data */
877  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
878  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
879  SDL_RLESurface(surface);
880  }
881 }
882 
883 /*
884  * Convert a surface into the specified pixel format.
885  */
886 SDL_Surface *
888  Uint32 flags)
889 {
890  SDL_Surface *convert;
891  Uint32 copy_flags;
892  SDL_Color copy_color;
893  SDL_Rect bounds;
894 
895  /* Check for empty destination palette! (results in empty image) */
896  if (format->palette != NULL) {
897  int i;
898  for (i = 0; i < format->palette->ncolors; ++i) {
899  if ((format->palette->colors[i].r != 0xFF) ||
900  (format->palette->colors[i].g != 0xFF) ||
901  (format->palette->colors[i].b != 0xFF))
902  break;
903  }
904  if (i == format->palette->ncolors) {
905  SDL_SetError("Empty destination palette");
906  return (NULL);
907  }
908  }
909 
910  /* Create a new surface with the desired format */
911  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
912  format->BitsPerPixel, format->Rmask,
913  format->Gmask, format->Bmask,
914  format->Amask);
915  if (convert == NULL) {
916  return (NULL);
917  }
918 
919  /* Copy the palette if any */
920  if (format->palette && convert->format->palette) {
921  SDL_memcpy(convert->format->palette->colors,
922  format->palette->colors,
923  format->palette->ncolors * sizeof(SDL_Color));
924  convert->format->palette->ncolors = format->palette->ncolors;
925  }
926 
927  /* Save the original copy flags */
928  copy_flags = surface->map->info.flags;
929  copy_color.r = surface->map->info.r;
930  copy_color.g = surface->map->info.g;
931  copy_color.b = surface->map->info.b;
932  copy_color.a = surface->map->info.a;
933  surface->map->info.r = 0xFF;
934  surface->map->info.g = 0xFF;
935  surface->map->info.b = 0xFF;
936  surface->map->info.a = 0xFF;
937  surface->map->info.flags = 0;
938  SDL_InvalidateMap(surface->map);
939 
940  /* Copy over the image data */
941  bounds.x = 0;
942  bounds.y = 0;
943  bounds.w = surface->w;
944  bounds.h = surface->h;
945  SDL_LowerBlit(surface, &bounds, convert, &bounds);
946 
947  /* Clean up the original surface, and update converted surface */
948  convert->map->info.r = copy_color.r;
949  convert->map->info.g = copy_color.g;
950  convert->map->info.b = copy_color.b;
951  convert->map->info.a = copy_color.a;
952  convert->map->info.flags =
953  (copy_flags &
957  surface->map->info.r = copy_color.r;
958  surface->map->info.g = copy_color.g;
959  surface->map->info.b = copy_color.b;
960  surface->map->info.a = copy_color.a;
961  surface->map->info.flags = copy_flags;
962  SDL_InvalidateMap(surface->map);
963  if (copy_flags & SDL_COPY_COLORKEY) {
964  SDL_bool set_colorkey_by_color = SDL_FALSE;
965 
966  if (surface->format->palette) {
967  if (format->palette &&
968  surface->format->palette->ncolors <= format->palette->ncolors &&
969  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
970  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
971  /* The palette is identical, just set the same colorkey */
972  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
973  } else if (format->Amask) {
974  /* The alpha was set in the destination from the palette */
975  } else {
976  set_colorkey_by_color = SDL_TRUE;
977  }
978  } else {
979  set_colorkey_by_color = SDL_TRUE;
980  }
981 
982  if (set_colorkey_by_color) {
983  /* Set the colorkey by color, which needs to be unique */
984  Uint8 keyR, keyG, keyB, keyA;
985 
986  SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
987  &keyG, &keyB, &keyA);
988  SDL_SetColorKey(convert, 1,
989  SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
990  /* This is needed when converting for 3D texture upload */
992  }
993  }
994  SDL_SetClipRect(convert, &surface->clip_rect);
995 
996  /* Enable alpha blending by default if the new surface has an
997  * alpha channel or alpha modulation */
998  if ((surface->format->Amask && format->Amask) ||
999  (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
1001  }
1002  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
1003  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
1004  }
1005 
1006  /* We're ready to go! */
1007  return (convert);
1008 }
1009 
1010 SDL_Surface *
1012  Uint32 flags)
1013 {
1014  SDL_PixelFormat *fmt;
1015  SDL_Surface *convert = NULL;
1016 
1017  fmt = SDL_AllocFormat(pixel_format);
1018  if (fmt) {
1019  convert = SDL_ConvertSurface(surface, fmt, flags);
1020  SDL_FreeFormat(fmt);
1021  }
1022  return convert;
1023 }
1024 
1025 /*
1026  * Create a surface on the stack for quick blit operations
1027  */
1028 static SDL_INLINE SDL_bool
1030  void * pixels, int pitch, SDL_Surface * surface,
1031  SDL_PixelFormat * format, SDL_BlitMap * blitmap)
1032 {
1033  if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
1034  SDL_SetError("Indexed pixel formats not supported");
1035  return SDL_FALSE;
1036  }
1037  if (SDL_InitFormat(format, pixel_format) < 0) {
1038  return SDL_FALSE;
1039  }
1040 
1041  SDL_zerop(surface);
1042  surface->flags = SDL_PREALLOC;
1043  surface->format = format;
1044  surface->pixels = pixels;
1045  surface->w = width;
1046  surface->h = height;
1047  surface->pitch = pitch;
1048  /* We don't actually need to set up the clip rect for our purposes */
1049  /* SDL_SetClipRect(surface, NULL); */
1050 
1051  /* Allocate an empty mapping */
1052  SDL_zerop(blitmap);
1053  blitmap->info.r = 0xFF;
1054  blitmap->info.g = 0xFF;
1055  blitmap->info.b = 0xFF;
1056  blitmap->info.a = 0xFF;
1057  surface->map = blitmap;
1058 
1059  /* The surface is ready to go */
1060  surface->refcount = 1;
1061  return SDL_TRUE;
1062 }
1063 
1064 /*
1065  * Copy a block of pixels of one format to another format
1066  */
1068  Uint32 src_format, const void * src, int src_pitch,
1069  Uint32 dst_format, void * dst, int dst_pitch)
1070 {
1071  SDL_Surface src_surface, dst_surface;
1072  SDL_PixelFormat src_fmt, dst_fmt;
1073  SDL_BlitMap src_blitmap, dst_blitmap;
1074  SDL_Rect rect;
1075  void *nonconst_src = (void *) src;
1076 
1077  /* Check to make sure we are blitting somewhere, so we don't crash */
1078  if (!dst) {
1079  return SDL_InvalidParamError("dst");
1080  }
1081  if (!dst_pitch) {
1082  return SDL_InvalidParamError("dst_pitch");
1083  }
1084 
1085  /* Fast path for same format copy */
1086  if (src_format == dst_format) {
1087  int bpp, i;
1088 
1089  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1090  switch (src_format) {
1091  case SDL_PIXELFORMAT_YUY2:
1092  case SDL_PIXELFORMAT_UYVY:
1093  case SDL_PIXELFORMAT_YVYU:
1094  bpp = 2;
1095  break;
1096  case SDL_PIXELFORMAT_YV12:
1097  case SDL_PIXELFORMAT_IYUV:
1098  case SDL_PIXELFORMAT_NV12:
1099  case SDL_PIXELFORMAT_NV21:
1100  bpp = 1;
1101  break;
1102  default:
1103  return SDL_SetError("Unknown FOURCC pixel format");
1104  }
1105  } else {
1106  bpp = SDL_BYTESPERPIXEL(src_format);
1107  }
1108  width *= bpp;
1109 
1110  for (i = height; i--;) {
1111  SDL_memcpy(dst, src, width);
1112  src = (Uint8*)src + src_pitch;
1113  dst = (Uint8*)dst + dst_pitch;
1114  }
1115 
1116  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
1117  /* U and V planes are a quarter the size of the Y plane */
1118  width /= 2;
1119  height /= 2;
1120  src_pitch /= 2;
1121  dst_pitch /= 2;
1122  for (i = height * 2; i--;) {
1123  SDL_memcpy(dst, src, width);
1124  src = (Uint8*)src + src_pitch;
1125  dst = (Uint8*)dst + dst_pitch;
1126  }
1127  } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1128  /* U/V plane is half the height of the Y plane */
1129  height /= 2;
1130  for (i = height; i--;) {
1131  SDL_memcpy(dst, src, width);
1132  src = (Uint8*)src + src_pitch;
1133  dst = (Uint8*)dst + dst_pitch;
1134  }
1135  }
1136  return 0;
1137  }
1138 
1139  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1140  src_pitch,
1141  &src_surface, &src_fmt, &src_blitmap)) {
1142  return -1;
1143  }
1144  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1145  &dst_surface, &dst_fmt, &dst_blitmap)) {
1146  return -1;
1147  }
1148 
1149  /* Set up the rect and go! */
1150  rect.x = 0;
1151  rect.y = 0;
1152  rect.w = width;
1153  rect.h = height;
1154  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1155 }
1156 
1157 /*
1158  * Free a surface created by the above function.
1159  */
1160 void
1162 {
1163  if (surface == NULL) {
1164  return;
1165  }
1166  if (surface->flags & SDL_DONTFREE) {
1167  return;
1168  }
1169  if (--surface->refcount > 0) {
1170  return;
1171  }
1172  while (surface->locked > 0) {
1173  SDL_UnlockSurface(surface);
1174  }
1175  if (surface->flags & SDL_RLEACCEL) {
1176  SDL_UnRLESurface(surface, 0);
1177  }
1178  if (surface->format) {
1179  SDL_SetSurfacePalette(surface, NULL);
1180  SDL_FreeFormat(surface->format);
1181  surface->format = NULL;
1182  }
1183  if (surface->map != NULL) {
1184  SDL_FreeBlitMap(surface->map);
1185  surface->map = NULL;
1186  }
1187  if (!(surface->flags & SDL_PREALLOC)) {
1188  SDL_free(surface->pixels);
1189  }
1190  SDL_free(surface);
1191 }
1192 
1193 /* vi: set ts=4 sw=4 expandtab: */
int SDL_GetColorKey(SDL_Surface *surface, Uint32 *key)
Gets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:267
GLenum GLenum dst
Uint8 r
Definition: SDL_blit.h:70
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:869
Uint32 version
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_blit.h:70
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:502
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
SDL_blit blit
Definition: SDL_blit.h:89
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
Sets the RLE acceleration hint for a surface.
Definition: SDL_surface.c:201
Uint8 g
Definition: SDL_pixels.h:296
#define SDL_MapRGBA
#define SDL_SoftStretch
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
int SDL_LockSurface(SDL_Surface *surface)
Sets up a surface for directly accessing the pixels.
Definition: SDL_surface.c:848
SDL_Surface * SDL_ConvertSurfaceFormat(SDL_Surface *surface, Uint32 pixel_format, Uint32 flags)
Definition: SDL_surface.c:1011
SDL_BlendMode
The blend mode used in SDL_RenderCopy() and drawing operations.
Definition: SDL_blendmode.h:40
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
SDL_Rect rect
Definition: testrelative.c:27
Uint8 g
Definition: SDL_blit.h:70
Sint64 SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:748
#define SDL_MasksToPixelFormatEnum
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:887
static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
Definition: SDL_surface.c:1029
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
int SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
Set an additional color value used in blit operations.
Definition: SDL_surface.c:355
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
Set an additional alpha value used in blit operations.
Definition: SDL_surface.c:400
#define SDL_DONTFREE
Definition: SDL_surface.h:55
int SDL_UpperBlitScaled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:667
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:436
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
#define SDL_BlitSurface
Definition: SDL_surface.h:457
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:285
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
Uint32 dst_palette_version
Definition: SDL_blit.h:95
Uint8 b
Definition: SDL_pixels.h:297
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
#define SDL_AllocFormat
#define SDL_COPY_RLE_COLORKEY
Definition: SDL_blit.h:42
#define SDL_MAX_SINT32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:155
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
#define SDL_IntersectRect
#define SDL_floor
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:545
#define SDL_zerop(x)
Definition: SDL_stdinc.h:362
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:222
int SDL_UpperBlit(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:568
GLfloat GLfloat GLfloat alpha
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
Uint32 colorkey
Definition: SDL_blit.h:69
Uint32 flags
Definition: SDL_surface.h:71
void SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect)
Definition: SDL_surface.c:526
Uint32 src_palette_version
Definition: SDL_blit.h:96
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
GLboolean GLboolean g
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:47
SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
Definition: SDL_surface.c:36
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
void * SDL_calloc(size_t nmemb, size_t size)
Uint8 r
Definition: SDL_pixels.h:295
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:993
void * pixels
Definition: SDL_surface.h:75
Uint8 a
Definition: SDL_pixels.h:298
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:187
void SDL_free(void *mem)
int SDL_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode)
Get the blend mode used for blit operations.
Definition: SDL_surface.c:473
int SDL_ConvertPixels(int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
Copy a block of pixels of one format to another format.
Definition: SDL_surface.c:1067
#define SDL_FreeFormat
#define SDL_memcmp
SDL_Surface * SDL_CreateRGBSurfaceWithFormatFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 format)
Definition: SDL_surface.c:168
int SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
Get the additional alpha value used in blit operations.
Definition: SDL_surface.c:423
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
GLenum GLint GLuint mask
#define SDL_GetRGBA
#define SDL_AllocPalette
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:521
SDL_Rect clip_rect
Definition: SDL_surface.h:85
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1161
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
int SDL_RLESurface(SDL_Surface *surface)
SDL_Surface * dst
Definition: SDL_blit.h:87
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:130
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
#define SDL_FreePalette
#define SDL_SetError
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:124
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
int h
Definition: SDL_rect.h:67
#define SDL_COPY_RLE_ALPHAKEY
Definition: SDL_blit.h:43
int SDL_LowerBlitScaled(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:821
SDL_BlitMap * SDL_AllocBlitMap(void)
Definition: SDL_pixels.c:954
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
Uint32 pixel_format
Definition: testoverlay2.c:152
GLbitfield flags
#define SDL_INLINE
Definition: begin_code.h:120
#define SDL_malloc
GLubyte GLubyte GLubyte GLubyte w
SDL_Palette * palette
Definition: SDL_pixels.h:316
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
SDL_Surface * SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:144
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:166
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
GLenum src
GLboolean GLboolean GLboolean b
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1079
int SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
Get the additional color value used in blit operations.
Definition: SDL_surface.c:381
GLenum GLenum void * row
int y
Definition: SDL_rect.h:66
#define SDL_SetPixelFormatPalette
#define SDL_Unsupported()
Definition: SDL_error.h:53
GLfloat GLfloat GLfloat GLfloat h
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:91
#define SDL_memset
#define SDL_PREALLOC
Definition: SDL_surface.h:53
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70