michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Image.h: Implements the rx::Image class, an abstract base class for the michael@0: // renderer-specific classes which will define the interface to the underlying michael@0: // surfaces or resources. michael@0: michael@0: #include "libGLESv2/renderer/Image.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: Image::Image() michael@0: { michael@0: mWidth = 0; michael@0: mHeight = 0; michael@0: mInternalFormat = GL_NONE; michael@0: mActualFormat = GL_NONE; michael@0: } michael@0: michael@0: void Image::loadAlphaDataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = 0; michael@0: dest[4 * x + 1] = 0; michael@0: dest[4 * x + 2] = 0; michael@0: dest[4 * x + 3] = source[x]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadAlphaDataToNative(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: memcpy(dest, source, width); michael@0: } michael@0: } michael@0: michael@0: void Image::loadAlphaFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = 0; michael@0: dest[4 * x + 1] = 0; michael@0: dest[4 * x + 2] = 0; michael@0: dest[4 * x + 3] = source[x]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadAlphaHalfFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned short *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = 0; michael@0: dest[4 * x + 1] = 0; michael@0: dest[4 * x + 2] = 0; michael@0: dest[4 * x + 3] = source[x]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceDataToNativeOrBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output, bool native) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: michael@0: if (!native) // BGRA8 destination format michael@0: { michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x]; michael@0: dest[4 * x + 1] = source[x]; michael@0: dest[4 * x + 2] = source[x]; michael@0: dest[4 * x + 3] = 0xFF; michael@0: } michael@0: } michael@0: else // L8 destination format michael@0: { michael@0: memcpy(dest, source, width); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x]; michael@0: dest[4 * x + 1] = source[x]; michael@0: dest[4 * x + 2] = source[x]; michael@0: dest[4 * x + 3] = 1.0f; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceFloatDataToRGB(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[3 * x + 0] = source[x]; michael@0: dest[3 * x + 1] = source[x]; michael@0: dest[3 * x + 2] = source[x]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceHalfFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned short *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x]; michael@0: dest[4 * x + 1] = source[x]; michael@0: dest[4 * x + 2] = source[x]; michael@0: dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1 michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceAlphaDataToNativeOrBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output, bool native) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: michael@0: if (!native) // BGRA8 destination format michael@0: { michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[2*x+0]; michael@0: dest[4 * x + 1] = source[2*x+0]; michael@0: dest[4 * x + 2] = source[2*x+0]; michael@0: dest[4 * x + 3] = source[2*x+1]; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: memcpy(dest, source, width * 2); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceAlphaFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[2*x+0]; michael@0: dest[4 * x + 1] = source[2*x+0]; michael@0: dest[4 * x + 2] = source[2*x+0]; michael@0: dest[4 * x + 3] = source[2*x+1]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadLuminanceAlphaHalfFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned short *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[2*x+0]; michael@0: dest[4 * x + 1] = source[2*x+0]; michael@0: dest[4 * x + 2] = source[2*x+0]; michael@0: dest[4 * x + 3] = source[2*x+1]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBUByteDataToBGRX(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x * 3 + 2]; michael@0: dest[4 * x + 1] = source[x * 3 + 1]; michael@0: dest[4 * x + 2] = source[x * 3 + 0]; michael@0: dest[4 * x + 3] = 0xFF; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBUByteDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x * 3 + 0]; michael@0: dest[4 * x + 1] = source[x * 3 + 1]; michael@0: dest[4 * x + 2] = source[x * 3 + 2]; michael@0: dest[4 * x + 3] = 0xFF; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGB565DataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2); michael@0: dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9); michael@0: dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); michael@0: dest[4 * x + 3] = 0xFF; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGB565DataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); michael@0: dest[4 * x + 1] = ((rgba & 0x07E0) >> 3) | ((rgba & 0x07E0) >> 9); michael@0: dest[4 * x + 2] = ((rgba & 0x001F) << 3) | ((rgba & 0x001F) >> 2); michael@0: dest[4 * x + 3] = 0xFF; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x * 3 + 0]; michael@0: dest[4 * x + 1] = source[x * 3 + 1]; michael@0: dest[4 * x + 2] = source[x * 3 + 2]; michael@0: dest[4 * x + 3] = 1.0f; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBFloatDataToNative(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: memcpy(dest, source, width * 12); michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBHalfFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned short *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: dest[4 * x + 0] = source[x * 3 + 0]; michael@0: dest[4 * x + 1] = source[x * 3 + 1]; michael@0: dest[4 * x + 2] = source[x * 3 + 2]; michael@0: dest[4 * x + 3] = 0x3C00; // SEEEEEMMMMMMMMMM, S = 0, E = 15, M = 0: 16bit flpt representation of 1 michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBAUByteDataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned int *source = NULL; michael@0: unsigned int *dest = NULL; michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned int rgba = source[x]; michael@0: dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBAUByteDataToNative(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned int *source = NULL; michael@0: unsigned int *dest = NULL; michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: michael@0: memcpy(dest, source, width * 4); michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBA4444DataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4); michael@0: dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8); michael@0: dest[4 * x + 2] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12); michael@0: dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBA4444DataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0xF000) >> 8) | ((rgba & 0xF000) >> 12); michael@0: dest[4 * x + 1] = ((rgba & 0x0F00) >> 4) | ((rgba & 0x0F00) >> 8); michael@0: dest[4 * x + 2] = ((rgba & 0x00F0) << 0) | ((rgba & 0x00F0) >> 4); michael@0: dest[4 * x + 3] = ((rgba & 0x000F) << 4) | ((rgba & 0x000F) >> 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBA5551DataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3); michael@0: dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8); michael@0: dest[4 * x + 2] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); michael@0: dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBA5551DataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned short *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = static_cast(output) + y * outputPitch; michael@0: for (int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgba = source[x]; michael@0: dest[4 * x + 0] = ((rgba & 0xF800) >> 8) | ((rgba & 0xF800) >> 13); michael@0: dest[4 * x + 1] = ((rgba & 0x07C0) >> 3) | ((rgba & 0x07C0) >> 8); michael@0: dest[4 * x + 2] = ((rgba & 0x003E) << 2) | ((rgba & 0x003E) >> 3); michael@0: dest[4 * x + 3] = (rgba & 0x0001) ? 0xFF : 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBAFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const float *source = NULL; michael@0: float *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: memcpy(dest, source, width * 16); michael@0: } michael@0: } michael@0: michael@0: void Image::loadRGBAHalfFloatDataToRGBA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: memcpy(dest, source, width * 8); michael@0: } michael@0: } michael@0: michael@0: void Image::loadBGRADataToBGRA(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned char *dest = NULL; michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = static_cast(output) + y * outputPitch; michael@0: memcpy(dest, source, width*4); michael@0: } michael@0: } michael@0: michael@0: }