1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rbmp_encode.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <streams/file_stream.h>
27 #include <formats/rbmp.h>
29 void form_bmp_header(uint8_t *header,
30 unsigned width, unsigned height,
33 unsigned line_size = (width * (is32bpp?4:3) + 3) & ~3;
34 unsigned size = line_size * height + 54;
35 unsigned size_array = line_size * height;
37 /* Generic BMP stuff. */
42 header[2] = (uint8_t)(size >> 0);
43 header[3] = (uint8_t)(size >> 8);
44 header[4] = (uint8_t)(size >> 16);
45 header[5] = (uint8_t)(size >> 24);
62 header[18] = (uint8_t)(width >> 0);
63 header[19] = (uint8_t)(width >> 8);
64 header[20] = (uint8_t)(width >> 16);
65 header[21] = (uint8_t)(width >> 24);
67 header[22] = (uint8_t)(height >> 0);
68 header[23] = (uint8_t)(height >> 8);
69 header[24] = (uint8_t)(height >> 16);
70 header[25] = (uint8_t)(height >> 24);
75 header[28] = is32bpp ? 32 : 24;
77 /* Compression method */
83 header[34] = (uint8_t)(size_array >> 0);
84 header[35] = (uint8_t)(size_array >> 8);
85 header[36] = (uint8_t)(size_array >> 16);
86 header[37] = (uint8_t)(size_array >> 24);
87 /* Horizontal resolution */
92 /* Vertical resolution */
102 /* Important color count */
109 static bool write_header_bmp(RFILE *file, unsigned width, unsigned height, bool is32bpp)
112 form_bmp_header(header, width, height, is32bpp);
113 return filestream_write(file, header, sizeof(header)) == sizeof(header);
116 static void dump_line_565_to_24(uint8_t *line, const uint16_t *src, unsigned width)
120 for (i = 0; i < width; i++)
122 uint16_t pixel = *src++;
123 uint8_t b = (pixel >> 0) & 0x1f;
124 uint8_t g = (pixel >> 5) & 0x3f;
125 uint8_t r = (pixel >> 11) & 0x1f;
126 *line++ = (b << 3) | (b >> 2);
127 *line++ = (g << 2) | (g >> 4);
128 *line++ = (r << 3) | (r >> 2);
132 static void dump_line_32_to_24(uint8_t *line, const uint32_t *src, unsigned width)
136 for (i = 0; i < width; i++)
138 uint32_t pixel = *src++;
139 *line++ = (pixel >> 0) & 0xff;
140 *line++ = (pixel >> 8) & 0xff;
141 *line++ = (pixel >> 16) & 0xff;
145 static void dump_content(RFILE *file, const void *frame,
146 int width, int height, int pitch, enum rbmp_source_type type)
150 uint8_t *line = NULL;
151 int bytes_per_pixel = (type==RBMP_SOURCE_TYPE_ARGB8888?4:3);
159 u.u8 = (const uint8_t*)frame;
160 line_size = (width * bytes_per_pixel + 3) & ~3;
164 case RBMP_SOURCE_TYPE_BGR24:
166 /* BGR24 byte order input matches output. Can directly copy, but... need to make sure we pad it. */
168 int padding = (int)(line_size-pitch);
169 for (j = 0; j < height; j++, u.u8 += pitch)
171 filestream_write(file, u.u8, pitch);
173 filestream_write(file, &zeros, padding);
177 case RBMP_SOURCE_TYPE_ARGB8888:
178 /* ARGB8888 byte order input matches output. Can directly copy. */
179 for (j = 0; j < height; j++, u.u8 += pitch)
180 filestream_write(file, u.u8, line_size);
186 /* allocate line buffer, and initialize the final four bytes to zero, for deterministic padding */
187 if (!(line = (uint8_t*)malloc(line_size)))
189 *(uint32_t*)(line + line_size - 4) = 0;
193 case RBMP_SOURCE_TYPE_XRGB888:
194 for (j = 0; j < height; j++, u.u8 += pitch)
196 dump_line_32_to_24(line, u.u32, width);
197 filestream_write(file, line, line_size);
200 case RBMP_SOURCE_TYPE_RGB565:
201 for (j = 0; j < height; j++, u.u8 += pitch)
203 dump_line_565_to_24(line, u.u16, width);
204 filestream_write(file, line, line_size);
211 /* Free allocated line buffer */
215 bool rbmp_save_image(
216 const char *filename,
218 unsigned width, unsigned height,
219 unsigned pitch, enum rbmp_source_type type)
222 RFILE *file = filestream_open(filename,
223 RETRO_VFS_FILE_ACCESS_WRITE,
224 RETRO_VFS_FILE_ACCESS_HINT_NONE);
228 ret = write_header_bmp(file, width, height, type==RBMP_SOURCE_TYPE_ARGB8888);
231 dump_content(file, frame, width, height, pitch, type);
233 filestream_close(file);