summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGL/Image.cpp
blob: aaff615eacefbf3857c95d05353750f7cc043ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGL/Image.h>

namespace GL {

ErrorOr<GPU::PixelType> get_validated_pixel_type(GLenum target, GLenum internal_format, GLenum format, GLenum type)
{
    // We accept GL_NONE as target for non-texture related calls (such as `glDrawPixels`)
    if (target != GL_NONE
        && target != GL_TEXTURE_1D
        && target != GL_TEXTURE_2D
        && target != GL_TEXTURE_3D
        && target != GL_TEXTURE_1D_ARRAY
        && target != GL_TEXTURE_2D_ARRAY
        && target != GL_TEXTURE_CUBE_MAP
        && target != GL_PROXY_TEXTURE_1D
        && target != GL_PROXY_TEXTURE_2D
        && target != GL_PROXY_TEXTURE_3D)
        return Error::from_errno(GL_INVALID_ENUM);

    // Internal format can be a number between 1 and 4. Symbolic formats were only added with EXT_texture, promoted to core in OpenGL 1.1
    if (internal_format == 1)
        internal_format = GL_ALPHA;
    else if (internal_format == 2)
        internal_format = GL_LUMINANCE_ALPHA;
    else if (internal_format == 3)
        internal_format = GL_RGB;
    else if (internal_format == 4)
        internal_format = GL_RGBA;

    if (internal_format != GL_NONE
        && internal_format != GL_ALPHA
        && internal_format != GL_ALPHA4
        && internal_format != GL_ALPHA8
        && internal_format != GL_ALPHA12
        && internal_format != GL_ALPHA16
        && internal_format != GL_COMPRESSED_ALPHA
        && internal_format != GL_COMPRESSED_LUMINANCE
        && internal_format != GL_COMPRESSED_LUMINANCE_ALPHA
        && internal_format != GL_COMPRESSED_INTENSITY
        && internal_format != GL_COMPRESSED_RGB
        && internal_format != GL_COMPRESSED_RGBA
        && internal_format != GL_DEPTH_COMPONENT
        && internal_format != GL_DEPTH_COMPONENT16
        && internal_format != GL_DEPTH_COMPONENT24
        && internal_format != GL_DEPTH_COMPONENT32
        && internal_format != GL_DEPTH_STENCIL
        && internal_format != GL_LUMINANCE
        && internal_format != GL_LUMINANCE4
        && internal_format != GL_LUMINANCE8
        && internal_format != GL_LUMINANCE12
        && internal_format != GL_LUMINANCE16
        && internal_format != GL_LUMINANCE_ALPHA
        && internal_format != GL_LUMINANCE4_ALPHA4
        && internal_format != GL_LUMINANCE6_ALPHA2
        && internal_format != GL_LUMINANCE8_ALPHA8
        && internal_format != GL_LUMINANCE12_ALPHA4
        && internal_format != GL_LUMINANCE12_ALPHA12
        && internal_format != GL_LUMINANCE16_ALPHA16
        && internal_format != GL_INTENSITY
        && internal_format != GL_INTENSITY4
        && internal_format != GL_INTENSITY8
        && internal_format != GL_INTENSITY12
        && internal_format != GL_INTENSITY16
        && internal_format != GL_R3_G3_B2
        && internal_format != GL_RED
        && internal_format != GL_RG
        && internal_format != GL_RGB
        && internal_format != GL_RGB4
        && internal_format != GL_RGB5
        && internal_format != GL_RGB8
        && internal_format != GL_RGB10
        && internal_format != GL_RGB12
        && internal_format != GL_RGB16
        && internal_format != GL_RGBA
        && internal_format != GL_RGBA2
        && internal_format != GL_RGBA4
        && internal_format != GL_RGB5_A1
        && internal_format != GL_RGBA8
        && internal_format != GL_RGB10_A2
        && internal_format != GL_RGBA12
        && internal_format != GL_RGBA16
        && internal_format != GL_SLUMINANCE
        && internal_format != GL_SLUMINANCE8
        && internal_format != GL_SLUMINANCE_ALPHA
        && internal_format != GL_SLUMINANCE8_ALPHA8
        && internal_format != GL_SRGB
        && internal_format != GL_SRGB8
        && internal_format != GL_SRGB_ALPHA
        && internal_format != GL_SRGB8_ALPHA8)
        return Error::from_errno(GL_INVALID_ENUM);

    if ((format < GL_COLOR_INDEX || format > GL_LUMINANCE_ALPHA) && format != GL_BGR && format != GL_BGRA)
        return Error::from_errno(GL_INVALID_ENUM);

    if (type != GL_BITMAP
        && (type < GL_BYTE || type > GL_FLOAT)
        && type != GL_HALF_FLOAT
        && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2)
        && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV))
        return Error::from_errno(GL_INVALID_ENUM);

    if (type == GL_BITMAP && format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX)
        return Error::from_errno(GL_INVALID_ENUM);

    if (format != GL_RGB && (type == GL_UNSIGNED_BYTE_3_3_2 || type == GL_UNSIGNED_BYTE_2_3_3_REV || type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_5_6_5_REV))
        return Error::from_errno(GL_INVALID_OPERATION);

    if ((type == GL_UNSIGNED_SHORT_4_4_4_4
            || type == GL_UNSIGNED_SHORT_4_4_4_4_REV
            || type == GL_UNSIGNED_SHORT_5_5_5_1
            || type == GL_UNSIGNED_SHORT_1_5_5_5_REV
            || type == GL_UNSIGNED_INT_8_8_8_8
            || type == GL_UNSIGNED_INT_8_8_8_8_REV
            || type == GL_UNSIGNED_INT_10_10_10_2
            || type == GL_UNSIGNED_INT_2_10_10_10_REV)
        && format != GL_RGBA
        && format != GL_BGRA)
        return Error::from_errno(GL_INVALID_OPERATION);

    if (internal_format != GL_NONE) {
        auto const internal_format_is_depth = internal_format == GL_DEPTH_COMPONENT
            || internal_format == GL_DEPTH_COMPONENT16
            || internal_format == GL_DEPTH_COMPONENT24
            || internal_format == GL_DEPTH_COMPONENT32;

        if ((target != GL_TEXTURE_2D && target != GL_PROXY_TEXTURE_2D && internal_format_is_depth)
            || (format == GL_DEPTH_COMPONENT && !internal_format_is_depth)
            || (format != GL_DEPTH_COMPONENT && internal_format_is_depth))
            return Error::from_errno(GL_INVALID_OPERATION);
    }

    return get_format_specification(format, type);
}

GPU::PixelType get_format_specification(GLenum format, GLenum type)
{
    auto get_format = [](GLenum format) -> GPU::PixelFormat {
        switch (format) {
        case GL_ALPHA:
            return GPU::PixelFormat::Alpha;
        case GL_BGR:
            return GPU::PixelFormat::BGR;
        case GL_BGRA:
            return GPU::PixelFormat::BGRA;
        case GL_BLUE:
            return GPU::PixelFormat::Blue;
        case GL_COLOR_INDEX:
            return GPU::PixelFormat::ColorIndex;
        case GL_DEPTH_COMPONENT:
            return GPU::PixelFormat::DepthComponent;
        case GL_GREEN:
            return GPU::PixelFormat::Green;
        case GL_LUMINANCE:
            return GPU::PixelFormat::Luminance;
        case GL_LUMINANCE_ALPHA:
            return GPU::PixelFormat::LuminanceAlpha;
        case GL_RED:
            return GPU::PixelFormat::Red;
        case GL_RGB:
            return GPU::PixelFormat::RGB;
        case GL_RGBA:
            return GPU::PixelFormat::RGBA;
        case GL_STENCIL_INDEX:
            return GPU::PixelFormat::StencilIndex;
        }
        VERIFY_NOT_REACHED();
    };
    auto pixel_format = get_format(format);

    switch (type) {
    case GL_BITMAP:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::Bitmap, GPU::ComponentsOrder::Normal };
    case GL_BYTE:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::Byte, GPU::ComponentsOrder::Normal };
    case GL_FLOAT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::Float, GPU::ComponentsOrder::Normal };
    case GL_HALF_FLOAT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::HalfFloat, GPU::ComponentsOrder::Normal };
    case GL_INT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::Int, GPU::ComponentsOrder::Normal };
    case GL_SHORT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::Short, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_BYTE:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::UnsignedByte, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_BYTE_2_3_3_REV:
        return { pixel_format, GPU::PixelComponentBits::B2_3_3, GPU::PixelDataType::UnsignedByte, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_BYTE_3_3_2:
        return { pixel_format, GPU::PixelComponentBits::B3_3_2, GPU::PixelDataType::UnsignedByte, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_INT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::UnsignedInt, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_INT_2_10_10_10_REV:
        return { pixel_format, GPU::PixelComponentBits::B2_10_10_10, GPU::PixelDataType::UnsignedInt, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_INT_8_8_8_8:
        return { pixel_format, GPU::PixelComponentBits::B8_8_8_8, GPU::PixelDataType::UnsignedInt, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_INT_8_8_8_8_REV:
        return { pixel_format, GPU::PixelComponentBits::B8_8_8_8, GPU::PixelDataType::UnsignedInt, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_INT_10_10_10_2:
        return { pixel_format, GPU::PixelComponentBits::B10_10_10_2, GPU::PixelDataType::UnsignedInt, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_SHORT:
        return { pixel_format, GPU::PixelComponentBits::AllBits, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
        return { pixel_format, GPU::PixelComponentBits::B1_5_5_5, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_SHORT_4_4_4_4:
        return { pixel_format, GPU::PixelComponentBits::B4_4_4_4, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
        return { pixel_format, GPU::PixelComponentBits::B4_4_4_4, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_SHORT_5_6_5:
        return { pixel_format, GPU::PixelComponentBits::B5_6_5, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Normal };
    case GL_UNSIGNED_SHORT_5_6_5_REV:
        return { pixel_format, GPU::PixelComponentBits::B5_6_5, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Reversed };
    case GL_UNSIGNED_SHORT_5_5_5_1:
        return { pixel_format, GPU::PixelComponentBits::B5_5_5_1, GPU::PixelDataType::UnsignedShort, GPU::ComponentsOrder::Normal };
    }
    VERIFY_NOT_REACHED();
}

}