| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | // LOVE |
| 24 | #include "common/Object.h" |
| 25 | #include "common/Data.h" |
| 26 | #include "common/pixelformat.h" |
| 27 | #include "CompressedSlice.h" |
| 28 | |
| 29 | namespace love |
| 30 | { |
| 31 | namespace image |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * Base class for all ImageData and CompressedImageData encoder/decoder library |
| 36 | * interfaces. We inherit from love::Object to take advantage of ref coounting. |
| 37 | **/ |
| 38 | class FormatHandler : public love::Object |
| 39 | { |
| 40 | public: |
| 41 | |
| 42 | enum EncodedFormat |
| 43 | { |
| 44 | ENCODED_TGA, |
| 45 | ENCODED_PNG, |
| 46 | ENCODED_MAX_ENUM |
| 47 | }; |
| 48 | |
| 49 | // Raw RGBA pixel data. |
| 50 | struct DecodedImage |
| 51 | { |
| 52 | PixelFormat format = PIXELFORMAT_RGBA8; |
| 53 | int width = 0; |
| 54 | int height = 0; |
| 55 | size_t size = 0; |
| 56 | unsigned char *data = nullptr; |
| 57 | }; |
| 58 | |
| 59 | // Pixel data encoded in a particular format. |
| 60 | struct EncodedImage |
| 61 | { |
| 62 | size_t size = 0; |
| 63 | unsigned char *data = nullptr; |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * The default constructor is called when the Image module is initialized. |
| 68 | **/ |
| 69 | FormatHandler(); |
| 70 | virtual ~FormatHandler(); |
| 71 | |
| 72 | /** |
| 73 | * Whether this format handler can decode the given Data into raw pixels. |
| 74 | **/ |
| 75 | virtual bool canDecode(Data *data); |
| 76 | |
| 77 | /** |
| 78 | * Whether this format handler can encode raw pixels to a particular format. |
| 79 | **/ |
| 80 | virtual bool canEncode(PixelFormat rawFormat, EncodedFormat encodedFormat); |
| 81 | |
| 82 | /** |
| 83 | * Decodes an image from its encoded form into raw pixel data. |
| 84 | **/ |
| 85 | virtual DecodedImage decode(Data *data); |
| 86 | |
| 87 | /** |
| 88 | * Encodes an image from raw pixel data into a particular format. |
| 89 | **/ |
| 90 | virtual EncodedImage encode(const DecodedImage &img, EncodedFormat format); |
| 91 | |
| 92 | /** |
| 93 | * Whether this format handler can parse the given Data into a |
| 94 | * CompressedImageData object. |
| 95 | **/ |
| 96 | virtual bool canParseCompressed(Data *data); |
| 97 | |
| 98 | /** |
| 99 | * Parses compressed image data into a list of sub-images and returns a |
| 100 | * single block of memory containing all the images. |
| 101 | * |
| 102 | * @param[in] filedata The data to parse. |
| 103 | * @param[out] images The list of sub-images generated. Byte data is a |
| 104 | * pointer to the returned data. |
| 105 | * @param[out] format The format of the Compressed Data. |
| 106 | * @param[out] sRGB Whether the texture is sRGB-encoded. |
| 107 | * |
| 108 | * @return The single block of memory containing the parsed images. |
| 109 | **/ |
| 110 | virtual StrongRef<CompressedMemory> parseCompressed(Data *filedata, |
| 111 | std::vector<StrongRef<CompressedSlice>> &images, |
| 112 | PixelFormat &format, bool &sRGB); |
| 113 | |
| 114 | /** |
| 115 | * Frees raw pixel memory allocated by the format handler. |
| 116 | **/ |
| 117 | virtual void freeRawPixels(unsigned char *mem); |
| 118 | |
| 119 | }; // FormatHandler |
| 120 | |
| 121 | } // image |
| 122 | } // love |
| 123 | |