const std = @import("std");
pub const c = @import("c.zig").c;

pub const PixelType = enum(c_int) {
    pixeltypeUnknown,
    pixeltypeIndex1,
    pixeltypeIndex4,
    pixeltypeIndex8,
    pixeltypePacked8,
    pixeltypePacked16,
    pixeltypePacked32,
    pixeltypeArrayu8,
    pixeltypeArrayu16,
    pixeltypeArrayu32,
    pixeltypeArrayf16,
    pixeltypeArrayf32,
    pixeltypeIndex2,
};

pub const BitmapOrder = enum(c_int) {
    bitmaporderNone,
    bitmaporder4321,
    bitmaporder1234,
};

pub const PackedOrder = enum(c_int) {
    packedorderNone,
    packedorderXrgb,
    packedorderRgbx,
    packedorderArgb,
    packedorderRgba,
    packedorderXbgr,
    packedorderBgrx,
    packedorderAbgr,
    packedorderBgra,
};

pub const ArrayOrder = enum(c_int) {
    arrayorderNone,
    arrayorderRgb,
    arrayorderRgba,
    arrayorderArgb,
    arrayorderBgr,
    arrayorderBgra,
    arrayorderAbgr,
};

pub const PackedLayout = enum(c_int) {
    packedlayoutNone,
    packedlayout332,
    packedlayout4444,
    packedlayout1555,
    packedlayout5551,
    packedlayout565,
    packedlayout8888,
    packedlayout2101010,
    packedlayout1010102,
};

pub const PixelFormat = enum(c_int) {
    pixelformatYv12 = 0x32315659, //Planar mode: Y + V + U  (3 planes)
    pixelformatIyuv = 0x56555949, //Planar mode: Y + U + V  (3 planes)
    pixelformatYuy2 = 0x32595559, //Packed mode: Y0+U0+Y1+V0 (1 plane)
    pixelformatUyvy = 0x59565955, //Packed mode: U0+Y0+V0+Y1 (1 plane)
    pixelformatYvyu = 0x55595659, //Packed mode: Y0+V0+Y1+U0 (1 plane)
    pixelformatNv12 = 0x3231564e, //Planar mode: Y + U/V interleaved  (2 planes)
    pixelformatNv21 = 0x3132564e, //Planar mode: Y + V/U interleaved  (2 planes)
    pixelformatP010 = 0x30313050, //Planar mode: Y + U/V interleaved  (2 planes)
    pixelformatExternalOes = 0x2053454f, //Android video texture format
    pixelformatMjpg = 0x47504a4d, //Motion JPEG
};

pub const ColorRange = enum(c_int) {
    colorRangeLimited = 1, //Narrow range, e.g. 16-235 for 8-bit RGB and luma, and 16-240 for 8-bit chroma
    colorRangeFull = 2,
};

pub const ColorPrimaries = enum(c_int) {
    colorPrimariesBt709 = 1, //ITU-R BT.709-6
    colorPrimariesBt470m = 4, //ITU-R BT.470-6 System M
    colorPrimariesBt470bg = 5, //ITU-R BT.470-6 System B, G / ITU-R BT.601-7 625
    colorPrimariesBt601 = 6, //ITU-R BT.601-7 525, SMPTE 170M
    colorPrimariesSmpte240 = 7, //SMPTE 240M, functionally the same as SDL_COLOR_PRIMARIES_BT601
    colorPrimariesGenericFilm = 8, //Generic film (color filters using Illuminant C)
    colorPrimariesBt2020 = 9, //ITU-R BT.2020-2 / ITU-R BT.2100-0
    colorPrimariesXyz = 10, //SMPTE ST 428-1
    colorPrimariesSmpte431 = 11, //SMPTE RP 431-2
    colorPrimariesSmpte432 = 12, //SMPTE EG 432-1 / DCI P3
    colorPrimariesEbu3213 = 22, //EBU Tech. 3213-E
};

pub const TransferCharacteristics = enum(c_int) {
    transferCharacteristicsBt709 = 1, //Rec. ITU-R BT.709-6 / ITU-R BT1361
    transferCharacteristicsGamma22 = 4, //ITU-R BT.470-6 System M / ITU-R BT1700 625 PAL & SECAM
    transferCharacteristicsGamma28 = 5, //ITU-R BT.470-6 System B, G
    transferCharacteristicsBt601 = 6, //SMPTE ST 170M / ITU-R BT.601-7 525 or 625
    transferCharacteristicsSmpte240 = 7, //SMPTE ST 240M
    transferCharacteristicsIec61966 = 11, //IEC 61966-2-4
    transferCharacteristicsBt1361 = 12, //ITU-R BT1361 Extended Colour Gamut
    transferCharacteristicsSrgb = 13, //IEC 61966-2-1 (sRGB or sYCC)
    transferCharacteristicsBt202010bit = 14, //ITU-R BT2020 for 10-bit system
    transferCharacteristicsBt202012bit = 15, //ITU-R BT2020 for 12-bit system
    transferCharacteristicsPq = 16, //SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
    transferCharacteristicsSmpte428 = 17, //SMPTE ST 428-1
    transferCharacteristicsHlg = 18, //ARIB STD-B67, known as "hybrid log-gamma" (HLG)
};

pub const MatrixCoefficients = enum(c_int) {
    matrixCoefficientsBt709 = 1, //ITU-R BT.709-6
    matrixCoefficientsFcc = 4, //US FCC Title 47
    matrixCoefficientsBt470bg = 5, //ITU-R BT.470-6 System B, G / ITU-R BT.601-7 625, functionally the same as SDL_MATRIX_COEFFICIENTS_BT601
    matrixCoefficientsBt601 = 6, //ITU-R BT.601-7 525
    matrixCoefficientsSmpte240 = 7, //SMPTE 240M
    matrixCoefficientsBt2020Ncl = 9, //ITU-R BT.2020-2 non-constant luminance
    matrixCoefficientsBt2020Cl = 10, //ITU-R BT.2020-2 constant luminance
    matrixCoefficientsSmpte2085 = 11, //SMPTE ST 2085
    matrixCoefficientsIctcp = 14, //ITU-R BT.2100-0 ICTCP
};

pub const ChromaLocation = enum(c_int) {
    chromaLocationNone = 0, //RGB, no chroma sampling
    chromaLocationLeft = 1, //In MPEG-2, MPEG-4, and AVC, Cb and Cr are taken on midpoint of the left-edge of the 2x2 square. In other words, they have the same horizontal location as the top-left pixel, but is shifted one-half pixel down vertically.
    chromaLocationCenter = 2, //In JPEG/JFIF, H.261, and MPEG-1, Cb and Cr are taken at the center of the 2x2 square. In other words, they are offset one-half pixel to the right and one-half pixel down compared to the top-left pixel.
    chromaLocationTopleft = 3,
};

pub const Colorspace = enum(c_int) {
    colorspaceSrgb = 0x120005a0, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
    colorRangeFull,
    colorPrimariesBt709,
    transferCharacteristicsSrgb,
    matrixCoefficientsIdentity,
    colorspaceSrgbLinear = 0x12000500, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709
    transferCharacteristicsLinear,
    colorspaceHdr10 = 0x12002600, //Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020
    colorPrimariesBt2020,
    transferCharacteristicsPq,
    colorspaceJpeg = 0x220004c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_FULL_G22_NONE_P709_X601
    transferCharacteristicsBt601,
    matrixCoefficientsBt601,
    colorspaceBt601Limited = 0x211018c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601
    colorRangeLimited,
    colorPrimariesBt601,
    colorspaceBt601Full = 0x221018c6, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601
    colorspaceBt709Limited = 0x21100421, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709
    transferCharacteristicsBt709,
    matrixCoefficientsBt709,
    colorspaceBt709Full = 0x22100421, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709
    colorspaceBt2020Limited = 0x21102609, //Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P2020
    matrixCoefficientsBt2020Ncl,
    colorspaceBt2020Full = 0x22102609, //Equivalent to DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P2020

    pub const colorspaceRgbDefault = .colorspaceSrgb; //The default colorspace for RGB surfaces if no colorspace is specified
    pub const colorspaceYuvDefault = .colorspaceJpeg; //The default colorspace for YUV surfaces if no colorspace is specified
};

pub const Color = extern struct {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
};

pub const FColor = extern struct {
    r: f32,
    g: f32,
    b: f32,
    a: f32,
};

pub const Palette = extern struct {
    ncolors: c_int, // number of elements in `colors`.
    colors: ?*Color, // an array of colors, `ncolors` long.
    version: u32, // internal use only, do not touch.
    refcount: c_int, // internal use only, do not touch.
};

pub const PixelFormatDetails = extern struct {
    format: PixelFormat,
    bits_per_pixel: u8,
    bytes_per_pixel: u8,
    padding: [2]u8,
    Rmask: u32,
    Gmask: u32,
    Bmask: u32,
    Amask: u32,
    Rbits: u8,
    Gbits: u8,
    Bbits: u8,
    Abits: u8,
    Rshift: u8,
    Gshift: u8,
    Bshift: u8,
    Ashift: u8,
};

pub inline fn getPixelFormatName(format: PixelFormat) [*c]const u8 {
    return c.SDL_GetPixelFormatName(@bitCast(format));
}

pub inline fn getMasksForPixelFormat(format: PixelFormat, bpp: *c_int, Rmask: *u32, Gmask: *u32, Bmask: *u32, Amask: *u32) bool {
    return @bitCast(c.SDL_GetMasksForPixelFormat(@bitCast(format), @ptrCast(bpp), @ptrCast(Rmask), @ptrCast(Gmask), @ptrCast(Bmask), @ptrCast(Amask)));
}

pub inline fn getPixelFormatForMasks(bpp: c_int, Rmask: u32, Gmask: u32, Bmask: u32, Amask: u32) PixelFormat {
    return @bitCast(c.SDL_GetPixelFormatForMasks(bpp, Rmask, Gmask, Bmask, Amask));
}

pub inline fn getPixelFormatDetails(format: PixelFormat) *const PixelFormatDetails {
    return @ptrCast(c.SDL_GetPixelFormatDetails(@bitCast(format)));
}

pub inline fn createPalette(ncolors: c_int) ?*Palette {
    return c.SDL_CreatePalette(ncolors);
}

pub inline fn setPaletteColors(palette: ?*Palette, colors: *const Color, firstcolor: c_int, ncolors: c_int) bool {
    return @bitCast(c.SDL_SetPaletteColors(palette, @ptrCast(colors), firstcolor, ncolors));
}

pub inline fn destroyPalette(palette: ?*Palette) void {
    return c.SDL_DestroyPalette(palette);
}

pub inline fn mapRGB(format: *const PixelFormatDetails, palette: *const Palette, r: u8, g: u8, b: u8) u32 {
    return c.SDL_MapRGB(@ptrCast(format), @ptrCast(palette), r, g, b);
}

pub inline fn mapRGBA(format: *const PixelFormatDetails, palette: *const Palette, r: u8, g: u8, b: u8, a: u8) u32 {
    return c.SDL_MapRGBA(@ptrCast(format), @ptrCast(palette), r, g, b, a);
}

pub inline fn getRGB(pixel: u32, format: *const PixelFormatDetails, palette: *const Palette, r: [*c]u8, g: [*c]u8, b: [*c]u8) void {
    return c.SDL_GetRGB(pixel, @ptrCast(format), @ptrCast(palette), r, g, b);
}

pub inline fn getRGBA(pixel: u32, format: *const PixelFormatDetails, palette: *const Palette, r: [*c]u8, g: [*c]u8, b: [*c]u8, a: [*c]u8) void {
    return c.SDL_GetRGBA(pixel, @ptrCast(format), @ptrCast(palette), r, g, b, a);
}
