Serenity Operating System
at portability 266 lines 8.2 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/BufferStream.h> 28#include <AK/ByteBuffer.h> 29#include <AK/FileSystemPath.h> 30#include <AK/MappedFile.h> 31#include <AK/NonnullOwnPtrVector.h> 32#include <LibGfx/GIFLoader.h> 33#include <stdio.h> 34 35namespace Gfx { 36 37static RefPtr<Gfx::Bitmap> load_gif_impl(const u8*, size_t); 38 39RefPtr<Gfx::Bitmap> load_gif(const StringView& path) 40{ 41 MappedFile mapped_file(path); 42 if (!mapped_file.is_valid()) 43 return nullptr; 44 auto bitmap = load_gif_impl((const u8*)mapped_file.data(), mapped_file.size()); 45 if (bitmap) 46 bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded GIF: %s", bitmap->width(), bitmap->height(), canonicalized_path(path).characters())); 47 return bitmap; 48} 49 50RefPtr<Gfx::Bitmap> load_gif_from_memory(const u8* data, size_t length) 51{ 52 auto bitmap = load_gif_impl(data, length); 53 if (bitmap) 54 bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded GIF: <memory>", bitmap->width(), bitmap->height())); 55 return bitmap; 56} 57 58enum class GIFFormat { 59 GIF87a, 60 GIF89a, 61}; 62 63struct RGB { 64 u8 r; 65 u8 g; 66 u8 b; 67}; 68 69struct LogicalScreen { 70 u16 width; 71 u16 height; 72 RGB color_map[256]; 73}; 74 75struct ImageDescriptor { 76 u16 x; 77 u16 y; 78 u16 width; 79 u16 height; 80 bool use_global_color_map; 81 RGB color_map[256]; 82 u8 lzw_min_code_size; 83 Vector<u8> lzw_encoded_bytes; 84}; 85 86RefPtr<Gfx::Bitmap> load_gif_impl(const u8* data, size_t data_size) 87{ 88 if (data_size < 32) 89 return nullptr; 90 91 auto buffer = ByteBuffer::wrap(data, data_size); 92 BufferStream stream(buffer); 93 94 static const char valid_header_87[] = "GIF87a"; 95 static const char valid_header_89[] = "GIF89a"; 96 97 char header[6]; 98 for (int i = 0; i < 6; ++i) 99 stream >> header[i]; 100 101 GIFFormat format; 102 if (!memcmp(header, valid_header_87, sizeof(header))) 103 format = GIFFormat::GIF87a; 104 else if (!memcmp(header, valid_header_89, sizeof(header))) 105 format = GIFFormat::GIF89a; 106 else 107 return nullptr; 108 109 printf("Format is %s\n", format == GIFFormat::GIF89a ? "GIF89a" : "GIF87a"); 110 111 LogicalScreen logical_screen; 112 stream >> logical_screen.width; 113 stream >> logical_screen.height; 114 if (stream.handle_read_failure()) 115 return nullptr; 116 117 u8 gcm_info = 0; 118 stream >> gcm_info; 119 120 if (stream.handle_read_failure()) 121 return nullptr; 122 123 bool global_color_map_follows_descriptor = gcm_info & 0x80; 124 u8 bits_per_pixel = (gcm_info & 7) + 1; 125 u8 bits_of_color_resolution = (gcm_info >> 4) & 7; 126 127 printf("LogicalScreen: %dx%d\n", logical_screen.width, logical_screen.height); 128 printf("global_color_map_follows_descriptor: %u\n", global_color_map_follows_descriptor); 129 printf("bits_per_pixel: %u\n", bits_per_pixel); 130 printf("bits_of_color_resolution: %u\n", bits_of_color_resolution); 131 132 u8 background_color = 0; 133 stream >> background_color; 134 if (stream.handle_read_failure()) 135 return nullptr; 136 137 printf("background_color: %u\n", background_color); 138 139 u8 pixel_aspect_ratio = 0; 140 stream >> pixel_aspect_ratio; 141 if (stream.handle_read_failure()) 142 return nullptr; 143 144 int color_map_entry_count = 1; 145 for (int i = 0; i < bits_per_pixel; ++i) 146 color_map_entry_count *= 2; 147 148 printf("color_map_entry_count: %d\n", color_map_entry_count); 149 150 for (int i = 0; i < color_map_entry_count; ++i) { 151 stream >> logical_screen.color_map[i].r; 152 stream >> logical_screen.color_map[i].g; 153 stream >> logical_screen.color_map[i].b; 154 } 155 156 if (stream.handle_read_failure()) 157 return nullptr; 158 159 for (int i = 0; i < color_map_entry_count; ++i) { 160 auto& rgb = logical_screen.color_map[i]; 161 printf("[%02x]: %s\n", i, Color(rgb.r, rgb.g, rgb.b).to_string().characters()); 162 } 163 164 NonnullOwnPtrVector<ImageDescriptor> images; 165 166 for (;;) { 167 u8 sentinel = 0; 168 stream >> sentinel; 169 printf("Sentinel: %02x\n", sentinel); 170 171 if (sentinel == 0x21) { 172 u8 extension_type = 0; 173 stream >> extension_type; 174 if (stream.handle_read_failure()) 175 return nullptr; 176 177 printf("Extension block of type %02x\n", extension_type); 178 179 u8 sub_block_length = 0; 180 181 for (;;) { 182 stream >> sub_block_length; 183 184 if (stream.handle_read_failure()) 185 return nullptr; 186 187 if (sub_block_length == 0) 188 break; 189 190 u8 dummy; 191 for (u16 i = 0; i < sub_block_length; ++i) 192 stream >> dummy; 193 194 if (stream.handle_read_failure()) 195 return nullptr; 196 } 197 continue; 198 } 199 200 if (sentinel == 0x2c) { 201 images.append(make<ImageDescriptor>()); 202 auto& image = images.last(); 203 u8 packed_fields { 0 }; 204 stream >> image.x; 205 stream >> image.y; 206 stream >> image.width; 207 stream >> image.height; 208 stream >> packed_fields; 209 if (stream.handle_read_failure()) 210 return nullptr; 211 printf("Image descriptor: %d,%d %dx%d, %02x\n", image.x, image.y, image.width, image.height, packed_fields); 212 213 stream >> image.lzw_min_code_size; 214 215 printf("min code size: %u\n", image.lzw_min_code_size); 216 217 u8 lzw_encoded_bytes_expected = 0; 218 219 for (;;) { 220 stream >> lzw_encoded_bytes_expected; 221 222 if (stream.handle_read_failure()) 223 return nullptr; 224 225 if (lzw_encoded_bytes_expected == 0) 226 break; 227 228 u8 buffer[256]; 229 for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { 230 stream >> buffer[i]; 231 } 232 233 if (stream.handle_read_failure()) 234 return nullptr; 235 236 for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { 237 image.lzw_encoded_bytes.append(buffer[i]); 238 } 239 } 240 continue; 241 } 242 243 if (sentinel == 0x3b) { 244 printf("Trailer! Awesome :)\n"); 245 break; 246 } 247 248 return nullptr; 249 } 250 251 // We exited the block loop after finding a trailer. We should have everything needed. 252 printf("Image count: %zu\n", images.size()); 253 if (images.is_empty()) 254 return nullptr; 255 256 for (size_t i = 0; i < images.size(); ++i) { 257 auto& image = images.at(i); 258 printf("Image %zu: %d,%d %dx%d %zu bytes LZW-encoded\n", i, image.x, image.y, image.width, image.height, image.lzw_encoded_bytes.size()); 259 260 // FIXME: Decode the LZW-encoded bytes and turn them into an image. 261 } 262 263 return nullptr; 264} 265 266}