Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * support routines for texture codecs
25 : */
26 :
27 : #include "precompiled.h"
28 :
29 : #include "tex_codec.h"
30 :
31 : #include "lib/allocators/dynarray.h"
32 : #include "lib/debug.h"
33 : #include "lib/posix/posix_types.h"
34 : #include "lib/tex/tex.h"
35 : #include "lib/tex/tex_internal.h"
36 :
37 : // Statically allocate all of the codecs...
38 : TexCodecDds DdsCodec;
39 : TexCodecPng PngCodec;
40 : TexCodecTga TgaCodec;
41 : TexCodecBmp BmpCodec;
42 : // Codecs will be searched in this order
43 : static const ITexCodec *codecs[] = {(ITexCodec *)&DdsCodec, (ITexCodec *)&PngCodec,
44 : (ITexCodec *)&TgaCodec, (ITexCodec *)&BmpCodec};
45 : static const int codecs_len = sizeof(codecs) / sizeof(ITexCodec*);
46 :
47 : // find codec that recognizes the desired output file extension,
48 : // or return ERR::TEX_UNKNOWN_FORMAT if unknown.
49 : // note: does not raise a warning because it is used by
50 : // tex_is_known_extension.
51 0 : Status tex_codec_for_filename(const OsPath& extension, const ITexCodec** c)
52 : {
53 0 : for(int i = 0; i < codecs_len; ++i)
54 : {
55 : // we found it
56 0 : if(codecs[i]->is_ext(extension)) {
57 0 : *c = codecs[i];
58 0 : return INFO::OK;
59 : }
60 : }
61 :
62 : return ERR::TEX_UNKNOWN_FORMAT; // NOWARN
63 : }
64 :
65 :
66 : // find codec that recognizes the header's magic field
67 14 : Status tex_codec_for_header(const std::uint8_t* file, size_t file_size, const ITexCodec** c)
68 : {
69 : // we guarantee at least 4 bytes for is_hdr to look at
70 14 : if(file_size < 4)
71 : return ERR::TEX_INCOMPLETE_HEADER;
72 :
73 19 : for(int i = 0; i < codecs_len; ++i)
74 : {
75 : // we found it
76 19 : if(codecs[i]->is_hdr(file)) {
77 14 : *c = codecs[i];
78 14 : return INFO::OK;
79 : }
80 : }
81 :
82 : return ERR::TEX_UNKNOWN_FORMAT;
83 : }
84 :
85 30 : Status tex_codec_transform(Tex* t, size_t transforms)
86 : {
87 30 : Status ret = INFO::TEX_CODEC_CANNOT_HANDLE;
88 :
89 : // find codec that understands the data, and transform
90 82 : for(int i = 0; i < codecs_len; ++i)
91 : {
92 69 : Status err = codecs[i]->transform(t, transforms);
93 : // success
94 69 : if(err == INFO::OK)
95 : return INFO::OK;
96 : // something went wrong
97 52 : else if(err != INFO::TEX_CODEC_CANNOT_HANDLE)
98 : {
99 0 : ret = err;
100 0 : DEBUG_WARN_ERR(ERR::LOGIC); // codec indicates error
101 : }
102 : }
103 :
104 : return ret;
105 : }
106 :
107 :
108 : //-----------------------------------------------------------------------------
109 : // helper functions used by codecs
110 : //-----------------------------------------------------------------------------
111 :
112 : // allocate an array of row pointers that point into the given texture data.
113 : // <file_orientation> indicates whether the file format is top-down or
114 : // bottom-up; the row array is inverted if necessary to match global
115 : // orienatation. (this is more efficient than "transforming" later)
116 : //
117 : // used by PNG and JPG codecs.
118 : //
119 : // note: we don't allocate the data param ourselves because this function is
120 : // needed for encoding, too (where data is already present).
121 3 : std::vector<RowPtr> tex_codec_alloc_rows(const std::uint8_t* data, size_t h, size_t pitch,
122 : size_t src_flags, size_t dst_orientation)
123 : {
124 3 : const bool flip = !tex_orientations_match(src_flags, dst_orientation);
125 :
126 3 : std::vector<RowPtr> rows(h);
127 :
128 : // determine start position and direction
129 3 : RowPtr pos = flip? data+pitch*(h-1) : data;
130 3 : const ssize_t add = flip? -(ssize_t)pitch : (ssize_t)pitch;
131 3 : const RowPtr end = flip? data-pitch : data+pitch*h;
132 :
133 75 : for(size_t i = 0; i < h; i++)
134 : {
135 72 : rows[i] = pos;
136 72 : pos += add;
137 : }
138 :
139 3 : ENSURE(pos == end);
140 3 : return rows;
141 0 : }
142 :
143 :
144 0 : Status tex_codec_write(Tex* t, size_t transforms, const void* hdr, size_t hdr_size, DynArray* da)
145 : {
146 0 : RETURN_STATUS_IF_ERR(t->transform(transforms));
147 :
148 0 : void* img_data = t->get_data(); const size_t img_size = t->img_size();
149 0 : RETURN_STATUS_IF_ERR(da_append(da, hdr, hdr_size));
150 0 : RETURN_STATUS_IF_ERR(da_append(da, img_data, img_size));
151 : return INFO::OK;
152 : }
|