Commit 6a79f587 authored by Jakob Bornecrantz's avatar Jakob Bornecrantz

aux/util: Add format helper functions

parent b0337f29
......@@ -24,6 +24,8 @@ set(UTIL_SOURCE_FILES
util/u_device.c
util/u_device.h
util/u_documentation.h
util/u_format.c
util/u_format.h
util/u_hashmap.cpp
util/u_hashmap.h
util/u_hashset.cpp
......
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Format helpers and block code.
* @author Jakob Bornecrantz <jakob@collabora.com>
* @ingroup aux_util
*/
#include "util/u_format.h"
#include <assert.h>
const char *
u_format_str(enum xrt_format f)
{
switch (f) {
case XRT_FORMAT_R8G8B8X8: return "XRT_FORMAT_R8G8B8X8";
case XRT_FORMAT_R8G8B8A8: return "XRT_FORMAT_R8G8B8A8";
case XRT_FORMAT_R8G8B8: return "XRT_FORMAT_R8G8B8";
case XRT_FORMAT_BITMAP_8X1: return "XRT_FORMAT_BITMAP_8X1";
case XRT_FORMAT_BITMAP_8X8: return "XRT_FORMAT_BITMAP_8X8";
case XRT_FORMAT_YUV888: return "XRT_FORMAT_YUV888";
case XRT_FORMAT_YUV422: return "XRT_FORMAT_YUV422";
case XRT_FORMAT_MJPEG: return "XRT_FORMAT_MJPEG";
default: assert(!"unsupported format");
}
}
bool
u_format_is_blocks(enum xrt_format f)
{
switch (f) {
case XRT_FORMAT_R8G8B8X8:
case XRT_FORMAT_R8G8B8A8:
case XRT_FORMAT_R8G8B8:
case XRT_FORMAT_BITMAP_8X1:
case XRT_FORMAT_BITMAP_8X8:
case XRT_FORMAT_YUV888:
case XRT_FORMAT_YUV422:
// Yes
return true;
case XRT_FORMAT_MJPEG:
// Compressed
return false;
default: assert(!"unsupported format");
}
}
uint32_t
u_format_block_width(enum xrt_format f)
{
switch (f) {
case XRT_FORMAT_R8G8B8X8:
case XRT_FORMAT_R8G8B8A8:
case XRT_FORMAT_R8G8B8:
case XRT_FORMAT_YUV888:
// Regular one pixel per block formats.
return 1;
case XRT_FORMAT_YUV422:
// Two pixels per block.
return 2;
case XRT_FORMAT_BITMAP_8X8:
case XRT_FORMAT_BITMAP_8X1:
// Eight pixels per block.
return 8;
default: assert(!"unsupported format");
}
}
uint32_t
u_format_block_height(enum xrt_format f)
{
switch (f) {
case XRT_FORMAT_R8G8B8X8:
case XRT_FORMAT_R8G8B8A8:
case XRT_FORMAT_R8G8B8:
case XRT_FORMAT_BITMAP_8X1:
case XRT_FORMAT_YUV888:
case XRT_FORMAT_YUV422:
// One pixel high.
return 1;
case XRT_FORMAT_BITMAP_8X8:
// Eight pixels high.
return 8;
default: assert(!"unsupported format");
}
}
size_t
u_format_block_size(enum xrt_format f)
{
switch (f) {
case XRT_FORMAT_BITMAP_8X1: return 1;
case XRT_FORMAT_R8G8B8:
case XRT_FORMAT_YUV888:
// Weird 24bit pixel formats.
return 3;
case XRT_FORMAT_R8G8B8X8:
case XRT_FORMAT_R8G8B8A8:
case XRT_FORMAT_YUV422: // Four bytes per two pixels.
// 32bit pixel formats.
return 4;
case XRT_FORMAT_BITMAP_8X8: // 64 bits.
return 8;
default: assert(!"unsupported format");
}
}
void
u_format_size_for_dimensions(enum xrt_format f,
uint32_t width,
uint32_t height,
size_t *out_stride,
size_t *out_size)
{
uint32_t sw = u_format_block_width(f);
uint32_t sh = u_format_block_height(f);
size_t block_size = u_format_block_size(f);
// Round up
uint32_t num_blocks_x = (width + (sw - 1)) / sw;
uint32_t num_blocks_y = (height + (sh - 1)) / sh;
// Add it all together
size_t stride = num_blocks_x * block_size;
size_t size = num_blocks_y * stride;
*out_stride = stride;
*out_size = size;
}
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Format helpers and block code.
* @author Jakob Bornecrantz <jakob@collabora.com>
* @ingroup aux_util
*/
#pragma once
#include "xrt/xrt_defines.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* Return string for this format.
*
* @ingroup aux_util
*/
const char *
u_format_str(enum xrt_format f);
/*!
* Is this format block based, also returns true for formats that 1x1 blocks.
*
* @ingroup aux_util
*/
bool
u_format_is_blocks(enum xrt_format f);
/*!
* Returns the width of the block for the given format.
*
* @ingroup aux_util
*/
uint32_t
u_format_block_width(enum xrt_format f);
/*!
* Returns the height of the block for the given format.
*
* @ingroup aux_util
*/
uint32_t
u_format_block_height(enum xrt_format f);
/*!
* Returns the size of the block for the given format.
*
* @ingroup aux_util
*/
size_t
u_format_block_size(enum xrt_format f);
/*!
* Calculate stride and size for the format and given width and height.
*
* @ingroup aux_util
*/
void
u_format_size_for_dimensions(enum xrt_format f,
uint32_t width,
uint32_t height,
size_t *out_stride,
size_t *out_size);
#ifdef __cplusplus
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment