annotate src/video/SDL_bmp.c @ 3024:1a08749aebce

Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
author Couriersud <couriersud@arcor.de>
date Sun, 11 Jan 2009 23:56:19 +0000
parents 502adab079a4
children 69ab1117dd3b
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
2859
99210400e8b9 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 2851
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
6 modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
13 Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
16 License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1012
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
20 slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
22 #include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 Code to load and save surfaces in Windows BMP format.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 Why support BMP format? Well, it's a native format for Windows, and
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 most image processing programs can read and write it. It would be nice
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 to be able to have at least one image format that we can natively load
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30 and save, and since PNG is so complex that it would bloat the library,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 BMP is a good alternative.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33 This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 #include "SDL_video.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37 #include "SDL_endian.h"
2823
15e1dd1fff78 Added missing header
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
38 #include "SDL_pixels_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
39
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
40 /* Compression encodings for BMP files */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
41 #ifndef BI_RGB
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
42 #define BI_RGB 0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
43 #define BI_RLE8 1
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
44 #define BI_RLE4 2
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
45 #define BI_BITFIELDS 3
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
46 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
47
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48
3024
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
49 static Uint8
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
50 SDL_Read8(SDL_RWops * src)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
51 {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
52 Uint8 value;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
53
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
54 SDL_RWread(src, &value, 1, 1);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
55 return (value);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
56 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
57
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
58 SDL_Surface *
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
59 SDL_LoadICO_RW(SDL_RWops * src, int freesrc)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
60 {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
61 int was_error;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
62 long fp_offset;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
63 int bmpPitch;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
64 int i, pad;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
65 SDL_Surface *surface;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
66 Uint32 Rmask;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
67 Uint32 Gmask;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
68 Uint32 Bmask;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
69 Uint8 *bits;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
70 int ExpandBMP;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
71 int maxCol = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
72 int icoOfs = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
73 Uint32 palette[256];
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
74
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
75 /* The Win32 ICO file header (14 bytes) */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
76 Uint16 bfReserved;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
77 Uint16 bfType;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
78 Uint16 bfCount;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
79
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
80 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
81 Uint32 biSize;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
82 Sint32 biWidth;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
83 Sint32 biHeight;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
84 Uint16 biPlanes;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
85 Uint16 biBitCount;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
86 Uint32 biCompression;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
87 Uint32 biSizeImage;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
88 Sint32 biXPelsPerMeter;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
89 Sint32 biYPelsPerMeter;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
90 Uint32 biClrUsed;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
91 Uint32 biClrImportant;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
92
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
93 /* Make sure we are passed a valid data source */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
94 surface = NULL;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
95 was_error = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
96 if (src == NULL) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
97 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
98 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
99 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
100
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
101 /* Read in the ICO file header */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
102 fp_offset = SDL_RWtell(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
103 SDL_ClearError();
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
104
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
105 bfReserved = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
106 bfType = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
107 bfCount = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
108 if ((bfType != 1 && bfType != 2) || (bfCount == 0)) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
109 SDL_SetError("File is not a Windows ICO file");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
110 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
111 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
112 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
113
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
114 /* Read the Win32 Icon Directory */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
115 for (i = 0; i < bfCount; i++) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
116 /* Icon Directory Entries */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
117 int bWidth = SDL_Read8(src); /* Uint8, but 0 = 256 ! */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
118 int bHeight = SDL_Read8(src); /* Uint8, but 0 = 256 ! */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
119 int bColorCount = SDL_Read8(src); /* Uint8, but 0 = 256 ! */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
120 Uint8 bReserved = SDL_Read8(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
121 Uint16 wPlanes = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
122 Uint16 wBitCount = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
123 Uint32 dwBytesInRes = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
124 Uint32 dwImageOffset = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
125
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
126 if (!bWidth)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
127 bWidth = 256;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
128 if (!bHeight)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
129 bHeight = 256;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
130 if (!bColorCount)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
131 bColorCount = 256;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
132
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
133 //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
134 if (bColorCount > maxCol) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
135 maxCol = bColorCount;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
136 icoOfs = dwImageOffset;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
137 //printf("marked\n");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
138 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
139 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
140
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
141 /* Advance to the DIB Data */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
142 if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
143 SDL_Error(SDL_EFSEEK);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
144 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
145 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
146 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
147
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
148 /* Read the Win32 BITMAPINFOHEADER */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
149 biSize = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
150 if (biSize == 40) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
151 biWidth = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
152 biHeight = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
153 biPlanes = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
154 biBitCount = SDL_ReadLE16(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
155 biCompression = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
156 biSizeImage = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
157 biXPelsPerMeter = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
158 biYPelsPerMeter = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
159 biClrUsed = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
160 biClrImportant = SDL_ReadLE32(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
161 } else {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
162 SDL_SetError("Unsupported ICO bitmap format");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
163 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
164 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
165 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
166
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
167 /* Check for read error */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
168 if (SDL_strcmp(SDL_GetError(), "") != 0) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
169 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
170 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
171 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
172
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
173 /* We don't support any BMP compression right now */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
174 switch (biCompression) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
175 case BI_RGB:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
176 /* Default values for the BMP format */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
177 switch (biBitCount) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
178 case 1:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
179 case 4:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
180 ExpandBMP = biBitCount;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
181 biBitCount = 8;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
182 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
183 case 8:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
184 ExpandBMP = 8;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
185 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
186 case 32:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
187 Rmask = 0x00FF0000;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
188 Gmask = 0x0000FF00;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
189 Bmask = 0x000000FF;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
190 ExpandBMP = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
191 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
192 default:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
193 SDL_SetError("ICO file with unsupported bit count");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
194 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
195 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
196 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
197 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
198 default:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
199 SDL_SetError("Compressed ICO files not supported");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
200 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
201 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
202 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
203
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
204 /* Create a RGBA surface */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
205 biHeight = biHeight >> 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
206 //printf("%d x %d\n", biWidth, biHeight);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
207 surface =
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
208 SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
209 0x0000FF00, 0x000000FF, 0xFF000000);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
210 if (surface == NULL) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
211 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
212 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
213 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
214
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
215 /* Load the palette, if any */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
216 //printf("bc %d bused %d\n", biBitCount, biClrUsed);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
217 if (biBitCount <= 8) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
218 if (biClrUsed == 0) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
219 biClrUsed = 1 << biBitCount;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
220 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
221 for (i = 0; i < (int) biClrUsed; ++i) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
222 SDL_RWread(src, &palette[i], 4, 1);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
223 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
224 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
225
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
226 /* Read the surface pixels. Note that the bmp image is upside down */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
227 bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
228 switch (ExpandBMP) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
229 case 1:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
230 bmpPitch = (biWidth + 7) >> 3;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
231 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
232 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
233 case 4:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
234 bmpPitch = (biWidth + 1) >> 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
235 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
236 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
237 case 8:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
238 bmpPitch = biWidth;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
239 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
240 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
241 default:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
242 bmpPitch = biWidth * 4;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
243 pad = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
244 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
245 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
246 while (bits > (Uint8 *) surface->pixels) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
247 bits -= surface->pitch;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
248 switch (ExpandBMP) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
249 case 1:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
250 case 4:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
251 case 8:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
252 {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
253 Uint8 pixel = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
254 int shift = (8 - ExpandBMP);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
255 for (i = 0; i < surface->w; ++i) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
256 if (i % (8 / ExpandBMP) == 0) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
257 if (!SDL_RWread(src, &pixel, 1, 1)) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
258 SDL_SetError("Error reading from ICO");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
259 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
260 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
261 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
262 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
263 *((Uint32 *) bits + i) = (palette[pixel >> shift]);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
264 pixel <<= ExpandBMP;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
265 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
266 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
267 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
268
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
269 default:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
270 if (SDL_RWread(src, bits, 1, surface->pitch)
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
271 != surface->pitch) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
272 SDL_Error(SDL_EFREAD);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
273 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
274 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
275 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
276 break;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
277 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
278 /* Skip padding bytes, ugh */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
279 if (pad) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
280 Uint8 padbyte;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
281 for (i = 0; i < pad; ++i) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
282 SDL_RWread(src, &padbyte, 1, 1);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
283 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
284 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
285 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
286 /* Read the mask pixels. Note that the bmp image is upside down */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
287 bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
288 ExpandBMP = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
289 bmpPitch = (biWidth + 7) >> 3;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
290 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
291 while (bits > (Uint8 *) surface->pixels) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
292 Uint8 pixel = 0;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
293 int shift = (8 - ExpandBMP);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
294
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
295 bits -= surface->pitch;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
296 for (i = 0; i < surface->w; ++i) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
297 if (i % (8 / ExpandBMP) == 0) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
298 if (!SDL_RWread(src, &pixel, 1, 1)) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
299 SDL_SetError("Error reading from ICO");
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
300 was_error = 1;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
301 goto done;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
302 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
303 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
304 *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
305 pixel <<= ExpandBMP;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
306 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
307 /* Skip padding bytes, ugh */
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
308 if (pad) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
309 Uint8 padbyte;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
310 for (i = 0; i < pad; ++i) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
311 SDL_RWread(src, &padbyte, 1, 1);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
312 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
313 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
314 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
315 done:
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
316 if (was_error) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
317 if (src) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
318 SDL_RWseek(src, fp_offset, RW_SEEK_SET);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
319 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
320 if (surface) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
321 SDL_FreeSurface(surface);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
322 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
323 surface = NULL;
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
324 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
325 if (freesrc && src) {
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
326 SDL_RWclose(src);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
327 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
328 return (surface);
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
329 }
1a08749aebce Add SDL_LoadICO_RW to SDL. Loads best quality icon from *.ico file.
Couriersud <couriersud@arcor.de>
parents: 2990
diff changeset
330
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
331 SDL_Surface *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
332 SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
333 {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
334 int was_error;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
335 long fp_offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
336 int bmpPitch;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
337 int i, pad;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
338 SDL_Surface *surface;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
339 Uint32 Rmask;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
340 Uint32 Gmask;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
341 Uint32 Bmask;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
342 SDL_Palette *palette;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
343 Uint8 *bits;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
344 int ExpandBMP;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
345
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
346 /* The Win32 BMP file header (14 bytes) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
347 char magic[2];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
348 Uint32 bfSize;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
349 Uint16 bfReserved1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
350 Uint16 bfReserved2;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
351 Uint32 bfOffBits;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
352
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
353 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
354 Uint32 biSize;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
355 Sint32 biWidth;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
356 Sint32 biHeight;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
357 Uint16 biPlanes;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
358 Uint16 biBitCount;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
359 Uint32 biCompression;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
360 Uint32 biSizeImage;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
361 Sint32 biXPelsPerMeter;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
362 Sint32 biYPelsPerMeter;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
363 Uint32 biClrUsed;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
364 Uint32 biClrImportant;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
365
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
366 /* Make sure we are passed a valid data source */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
367 surface = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
368 was_error = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
369 if (src == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
370 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
371 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
372 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
373
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
374 /* Read in the BMP file header */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
375 fp_offset = SDL_RWtell(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
376 SDL_ClearError();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
377 if (SDL_RWread(src, magic, 1, 2) != 2) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
378 SDL_Error(SDL_EFREAD);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
379 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
380 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
381 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
382 if (SDL_strncmp(magic, "BM", 2) != 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
383 SDL_SetError("File is not a Windows BMP file");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
384 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
385 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
386 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
387 bfSize = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
388 bfReserved1 = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
389 bfReserved2 = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
390 bfOffBits = SDL_ReadLE32(src);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
391
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
392 /* Read the Win32 BITMAPINFOHEADER */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
393 biSize = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
394 if (biSize == 12) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
395 biWidth = (Uint32) SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
396 biHeight = (Uint32) SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
397 biPlanes = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
398 biBitCount = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
399 biCompression = BI_RGB;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
400 biSizeImage = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
401 biXPelsPerMeter = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
402 biYPelsPerMeter = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
403 biClrUsed = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
404 biClrImportant = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
405 } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
406 biWidth = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
407 biHeight = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
408 biPlanes = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
409 biBitCount = SDL_ReadLE16(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
410 biCompression = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
411 biSizeImage = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
412 biXPelsPerMeter = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
413 biYPelsPerMeter = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
414 biClrUsed = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
415 biClrImportant = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
416 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
417
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
418 /* Check for read error */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
419 if (SDL_strcmp(SDL_GetError(), "") != 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
420 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
421 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
422 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
423
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
424 /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
425 switch (biBitCount) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
426 case 1:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
427 case 4:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
428 ExpandBMP = biBitCount;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
429 biBitCount = 8;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
430 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
431 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
432 ExpandBMP = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
433 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
434 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
435
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
436 /* We don't support any BMP compression right now */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
437 Rmask = Gmask = Bmask = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
438 switch (biCompression) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
439 case BI_RGB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
440 /* If there are no masks, use the defaults */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
441 if (bfOffBits == (14 + biSize)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
442 /* Default values for the BMP format */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
443 switch (biBitCount) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
444 case 15:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
445 case 16:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
446 Rmask = 0x7C00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
447 Gmask = 0x03E0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
448 Bmask = 0x001F;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
449 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
450 case 24:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
451 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
452 Rmask = 0x000000FF;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
453 Gmask = 0x0000FF00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
454 Bmask = 0x00FF0000;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
455 break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
456 #endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
457 case 32:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
458 Rmask = 0x00FF0000;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
459 Gmask = 0x0000FF00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
460 Bmask = 0x000000FF;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
461 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
462 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
463 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
464 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
465 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
466 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
467 /* Fall through -- read the RGB masks */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
468
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
469 case BI_BITFIELDS:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
470 switch (biBitCount) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
471 case 15:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
472 case 16:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
473 case 32:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
474 Rmask = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
475 Gmask = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
476 Bmask = SDL_ReadLE32(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
477 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
478 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
479 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
480 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
481 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
482 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
483 SDL_SetError("Compressed BMP files not supported");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
484 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
485 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
486 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
487
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
488 /* Create a compatible surface, note that the colors are RGB ordered */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
489 surface =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
490 SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
491 Bmask, 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
492 if (surface == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
493 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
494 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
495 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
496
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
497 /* Load the palette, if any */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
498 palette = (surface->format)->palette;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
499 if (palette) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
500 if (biClrUsed == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
501 biClrUsed = 1 << biBitCount;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
502 }
2920
Sam Lantinga <slouken@libsdl.org>
parents: 2918
diff changeset
503 if ((int) biClrUsed > palette->ncolors) {
2851
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
504 palette->ncolors = biClrUsed;
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
505 palette->colors =
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
506 (SDL_Color *) SDL_realloc(palette->colors,
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
507 palette->ncolors *
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
508 sizeof(*palette->colors));
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
509 if (!palette->colors) {
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
510 SDL_OutOfMemory();
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
511 was_error = 1;
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
512 goto done;
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
513 }
2920
Sam Lantinga <slouken@libsdl.org>
parents: 2918
diff changeset
514 } else if ((int) biClrUsed < palette->ncolors) {
2851
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
515 palette->ncolors = biClrUsed;
6c3fbeb04eca Fixed crash in testpalette and potential crash in SDL_LoadBMP_RW()
Sam Lantinga <slouken@libsdl.org>
parents: 2823
diff changeset
516 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
517 if (biSize == 12) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
518 for (i = 0; i < (int) biClrUsed; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
519 SDL_RWread(src, &palette->colors[i].b, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
520 SDL_RWread(src, &palette->colors[i].g, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
521 SDL_RWread(src, &palette->colors[i].r, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
522 palette->colors[i].unused = SDL_ALPHA_OPAQUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
523 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
524 } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
525 for (i = 0; i < (int) biClrUsed; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
526 SDL_RWread(src, &palette->colors[i].b, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
527 SDL_RWread(src, &palette->colors[i].g, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
528 SDL_RWread(src, &palette->colors[i].r, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
529 SDL_RWread(src, &palette->colors[i].unused, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
530 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
531 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
532 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
533
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
534 /* Read the surface pixels. Note that the bmp image is upside down */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
535 if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
536 SDL_Error(SDL_EFSEEK);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
537 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
538 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
539 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
540 bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
541 switch (ExpandBMP) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
542 case 1:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
543 bmpPitch = (biWidth + 7) >> 3;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
544 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
545 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
546 case 4:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
547 bmpPitch = (biWidth + 1) >> 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
548 pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
549 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
550 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
551 pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
552 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
553 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
554 while (bits > (Uint8 *) surface->pixels) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
555 bits -= surface->pitch;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
556 switch (ExpandBMP) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
557 case 1:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
558 case 4:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
559 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
560 Uint8 pixel = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
561 int shift = (8 - ExpandBMP);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
562 for (i = 0; i < surface->w; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
563 if (i % (8 / ExpandBMP) == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
564 if (!SDL_RWread(src, &pixel, 1, 1)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
565 SDL_SetError("Error reading from BMP");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
566 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
567 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
568 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
569 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
570 *(bits + i) = (pixel >> shift);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
571 pixel <<= ExpandBMP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
572 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
573 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
574 break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
575
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
576 default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
577 if (SDL_RWread(src, bits, 1, surface->pitch)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
578 != surface->pitch) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
579 SDL_Error(SDL_EFREAD);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
580 was_error = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
581 goto done;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
582 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
583 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
584 /* Byte-swap the pixels if needed. Note that the 24bpp
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
585 case has already been taken care of above. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
586 switch (biBitCount) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
587 case 15:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
588 case 16:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
589 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
590 Uint16 *pix = (Uint16 *) bits;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
591 for (i = 0; i < surface->w; i++)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
592 pix[i] = SDL_Swap16(pix[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
593 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
594 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
595
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
596 case 32:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
597 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
598 Uint32 *pix = (Uint32 *) bits;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
599 for (i = 0; i < surface->w; i++)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
600 pix[i] = SDL_Swap32(pix[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
601 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
602 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
603 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
604 #endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
605 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
606 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
607 /* Skip padding bytes, ugh */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
608 if (pad) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
609 Uint8 padbyte;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
610 for (i = 0; i < pad; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
611 SDL_RWread(src, &padbyte, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
612 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
613 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
614 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
615 done:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
616 if (was_error) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
617 if (src) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
618 SDL_RWseek(src, fp_offset, RW_SEEK_SET);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
619 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
620 if (surface) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
621 SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
622 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
623 surface = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
624 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
625 if (freesrc && src) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
626 SDL_RWclose(src);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
627 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
628 return (surface);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
629 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
630
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
631 int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
632 SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
633 {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
634 long fp_offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
635 int i, pad;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
636 SDL_Surface *surface;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
637 Uint8 *bits;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
638
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
639 /* The Win32 BMP file header (14 bytes) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
640 char magic[2] = { 'B', 'M' };
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
641 Uint32 bfSize;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
642 Uint16 bfReserved1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
643 Uint16 bfReserved2;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
644 Uint32 bfOffBits;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
645
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
646 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
647 Uint32 biSize;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
648 Sint32 biWidth;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
649 Sint32 biHeight;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
650 Uint16 biPlanes;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
651 Uint16 biBitCount;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
652 Uint32 biCompression;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
653 Uint32 biSizeImage;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
654 Sint32 biXPelsPerMeter;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
655 Sint32 biYPelsPerMeter;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
656 Uint32 biClrUsed;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
657 Uint32 biClrImportant;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
658
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
659 /* Make sure we have somewhere to save */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
660 surface = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
661 if (dst) {
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
662 SDL_bool save32bit = SDL_FALSE;
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
663 #ifdef SAVE_32BIT_BMP
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
664 /* We can save alpha information in a 32-bit BMP */
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
665 if (saveme->map->info.flags & SDL_COPY_COLORKEY ||
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
666 saveme->format->Amask) {
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
667 save32bit = SDL_TRUE;
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
668 }
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
669 #endif /* SAVE_32BIT_BMP */
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
670
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
671 if (saveme->format->palette && !save32bit) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
672 if (saveme->format->BitsPerPixel == 8) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
673 surface = saveme;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
674 } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
675 SDL_SetError("%d bpp BMP files not supported",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
676 saveme->format->BitsPerPixel);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
677 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
678 } else if ((saveme->format->BitsPerPixel == 24) &&
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
679 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
680 (saveme->format->Rmask == 0x00FF0000) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
681 (saveme->format->Gmask == 0x0000FF00) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
682 (saveme->format->Bmask == 0x000000FF)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
683 #else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
684 (saveme->format->Rmask == 0x000000FF) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
685 (saveme->format->Gmask == 0x0000FF00) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
686 (saveme->format->Bmask == 0x00FF0000)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
687 #endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
688 ) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
689 surface = saveme;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
690 } else {
2967
e4a469d6ddab Implemented SDL_SetWindowIcon(), with translucent icon support under X11.
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
691 SDL_PixelFormat format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
692
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
693 /* If the surface has a colorkey or alpha channel we'll save a
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
694 32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
695 if (save32bit) {
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
696 SDL_InitFormat(&format, 32,
2990
Sam Lantinga <slouken@libsdl.org>
parents: 2969
diff changeset
697 0x00FF0000, 0x0000FF00, 0x000000FF,
Sam Lantinga <slouken@libsdl.org>
parents: 2969
diff changeset
698 0xFF000000);
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
699 } else {
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
700 SDL_InitFormat(&format, 24,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
701 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
702 0x00FF0000, 0x0000FF00, 0x000000FF,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
703 #else
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
704 0x000000FF, 0x0000FF00, 0x00FF0000,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
705 #endif
2969
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
706 0);
1ee69e7e7cea Added support for saving 32-bit BMP with alpha channel (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 2967
diff changeset
707 }
2967
e4a469d6ddab Implemented SDL_SetWindowIcon(), with translucent icon support under X11.
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
708 surface = SDL_ConvertSurface(saveme, &format, 0);
e4a469d6ddab Implemented SDL_SetWindowIcon(), with translucent icon support under X11.
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
709 if (!surface) {
2990
Sam Lantinga <slouken@libsdl.org>
parents: 2969
diff changeset
710 SDL_SetError("Couldn't convert image to %d bpp",
Sam Lantinga <slouken@libsdl.org>
parents: 2969
diff changeset
711 format.BitsPerPixel);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
712 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
713 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
714 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
715
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
716 if (surface && (SDL_LockSurface(surface) == 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
717 const int bw = surface->w * surface->format->BytesPerPixel;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
718
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
719 /* Set the BMP file header values */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
720 bfSize = 0; /* We'll write this when we're done */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
721 bfReserved1 = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
722 bfReserved2 = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
723 bfOffBits = 0; /* We'll write this when we're done */
1012
f14e3059e138 Date: Mon, 13 Dec 2004 21:28:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
724
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
725 /* Write the BMP file header values */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
726 fp_offset = SDL_RWtell(dst);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
727 SDL_ClearError();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
728 SDL_RWwrite(dst, magic, 2, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
729 SDL_WriteLE32(dst, bfSize);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
730 SDL_WriteLE16(dst, bfReserved1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
731 SDL_WriteLE16(dst, bfReserved2);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
732 SDL_WriteLE32(dst, bfOffBits);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
733
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
734 /* Set the BMP info values */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
735 biSize = 40;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
736 biWidth = surface->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
737 biHeight = surface->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
738 biPlanes = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
739 biBitCount = surface->format->BitsPerPixel;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
740 biCompression = BI_RGB;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
741 biSizeImage = surface->h * surface->pitch;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
742 biXPelsPerMeter = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
743 biYPelsPerMeter = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
744 if (surface->format->palette) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
745 biClrUsed = surface->format->palette->ncolors;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
746 } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
747 biClrUsed = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
748 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
749 biClrImportant = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
750
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
751 /* Write the BMP info values */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
752 SDL_WriteLE32(dst, biSize);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
753 SDL_WriteLE32(dst, biWidth);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
754 SDL_WriteLE32(dst, biHeight);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
755 SDL_WriteLE16(dst, biPlanes);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
756 SDL_WriteLE16(dst, biBitCount);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
757 SDL_WriteLE32(dst, biCompression);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
758 SDL_WriteLE32(dst, biSizeImage);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
759 SDL_WriteLE32(dst, biXPelsPerMeter);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
760 SDL_WriteLE32(dst, biYPelsPerMeter);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
761 SDL_WriteLE32(dst, biClrUsed);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
762 SDL_WriteLE32(dst, biClrImportant);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
763
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
764 /* Write the palette (in BGR color order) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
765 if (surface->format->palette) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
766 SDL_Color *colors;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
767 int ncolors;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
768
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
769 colors = surface->format->palette->colors;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
770 ncolors = surface->format->palette->ncolors;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
771 for (i = 0; i < ncolors; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
772 SDL_RWwrite(dst, &colors[i].b, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
773 SDL_RWwrite(dst, &colors[i].g, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
774 SDL_RWwrite(dst, &colors[i].r, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
775 SDL_RWwrite(dst, &colors[i].unused, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
776 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
777 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
778
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
779 /* Write the bitmap offset */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
780 bfOffBits = SDL_RWtell(dst) - fp_offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
781 if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
782 SDL_Error(SDL_EFSEEK);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
783 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
784 SDL_WriteLE32(dst, bfOffBits);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
785 if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
786 SDL_Error(SDL_EFSEEK);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
787 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
788
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
789 /* Write the bitmap image upside down */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
790 bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
791 pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
792 while (bits > (Uint8 *) surface->pixels) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
793 bits -= surface->pitch;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
794 if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
795 SDL_Error(SDL_EFWRITE);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
796 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
797 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
798 if (pad) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
799 const Uint8 padbyte = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
800 for (i = 0; i < pad; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
801 SDL_RWwrite(dst, &padbyte, 1, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
802 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
803 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
804 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
805
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
806 /* Write the BMP file size */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
807 bfSize = SDL_RWtell(dst) - fp_offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
808 if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
809 SDL_Error(SDL_EFSEEK);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
810 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
811 SDL_WriteLE32(dst, bfSize);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
812 if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
813 SDL_Error(SDL_EFSEEK);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
814 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
815
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
816 /* Close it up.. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
817 SDL_UnlockSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
818 if (surface != saveme) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
819 SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
820 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
821 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
822
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
823 if (freedst && dst) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
824 SDL_RWclose(dst);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
825 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
826 return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
827 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
828
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
829 /* vi: set ts=4 sw=4 expandtab: */