pixeltransfer.c revision af69d88d
13464ebd5Sriastradh/* 23464ebd5Sriastradh * Mesa 3-D graphics library 33464ebd5Sriastradh * 43464ebd5Sriastradh * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 53464ebd5Sriastradh * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved. 63464ebd5Sriastradh * 73464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a 83464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"), 93464ebd5Sriastradh * to deal in the Software without restriction, including without limitation 103464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense, 113464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the 123464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions: 133464ebd5Sriastradh * 143464ebd5Sriastradh * The above copyright notice and this permission notice shall be included 153464ebd5Sriastradh * in all copies or substantial portions of the Software. 163464ebd5Sriastradh * 173464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 183464ebd5Sriastradh * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 193464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 243464ebd5Sriastradh */ 253464ebd5Sriastradh 263464ebd5Sriastradh 273464ebd5Sriastradh/** 283464ebd5Sriastradh * \file pixeltransfer.c 293464ebd5Sriastradh * Pixel transfer operations (scale, bias, table lookups, etc) 303464ebd5Sriastradh */ 313464ebd5Sriastradh 323464ebd5Sriastradh 333464ebd5Sriastradh#include "glheader.h" 343464ebd5Sriastradh#include "colormac.h" 353464ebd5Sriastradh#include "pixeltransfer.h" 363464ebd5Sriastradh#include "imports.h" 373464ebd5Sriastradh#include "mtypes.h" 383464ebd5Sriastradh 393464ebd5Sriastradh 403464ebd5Sriastradh/* 413464ebd5Sriastradh * Apply scale and bias factors to an array of RGBA pixels. 423464ebd5Sriastradh */ 433464ebd5Sriastradhvoid 443464ebd5Sriastradh_mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4], 453464ebd5Sriastradh GLfloat rScale, GLfloat gScale, 463464ebd5Sriastradh GLfloat bScale, GLfloat aScale, 473464ebd5Sriastradh GLfloat rBias, GLfloat gBias, 483464ebd5Sriastradh GLfloat bBias, GLfloat aBias) 493464ebd5Sriastradh{ 503464ebd5Sriastradh if (rScale != 1.0 || rBias != 0.0) { 513464ebd5Sriastradh GLuint i; 523464ebd5Sriastradh for (i = 0; i < n; i++) { 533464ebd5Sriastradh rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias; 543464ebd5Sriastradh } 553464ebd5Sriastradh } 563464ebd5Sriastradh if (gScale != 1.0 || gBias != 0.0) { 573464ebd5Sriastradh GLuint i; 583464ebd5Sriastradh for (i = 0; i < n; i++) { 593464ebd5Sriastradh rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias; 603464ebd5Sriastradh } 613464ebd5Sriastradh } 623464ebd5Sriastradh if (bScale != 1.0 || bBias != 0.0) { 633464ebd5Sriastradh GLuint i; 643464ebd5Sriastradh for (i = 0; i < n; i++) { 653464ebd5Sriastradh rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias; 663464ebd5Sriastradh } 673464ebd5Sriastradh } 683464ebd5Sriastradh if (aScale != 1.0 || aBias != 0.0) { 693464ebd5Sriastradh GLuint i; 703464ebd5Sriastradh for (i = 0; i < n; i++) { 713464ebd5Sriastradh rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias; 723464ebd5Sriastradh } 733464ebd5Sriastradh } 743464ebd5Sriastradh} 753464ebd5Sriastradh 763464ebd5Sriastradh 773464ebd5Sriastradh/* 783464ebd5Sriastradh * Apply pixel mapping to an array of floating point RGBA pixels. 793464ebd5Sriastradh */ 803464ebd5Sriastradhvoid 813464ebd5Sriastradh_mesa_map_rgba( const struct gl_context *ctx, GLuint n, GLfloat rgba[][4] ) 823464ebd5Sriastradh{ 833464ebd5Sriastradh const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1); 843464ebd5Sriastradh const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1); 853464ebd5Sriastradh const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1); 863464ebd5Sriastradh const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1); 873464ebd5Sriastradh const GLfloat *rMap = ctx->PixelMaps.RtoR.Map; 883464ebd5Sriastradh const GLfloat *gMap = ctx->PixelMaps.GtoG.Map; 893464ebd5Sriastradh const GLfloat *bMap = ctx->PixelMaps.BtoB.Map; 903464ebd5Sriastradh const GLfloat *aMap = ctx->PixelMaps.AtoA.Map; 913464ebd5Sriastradh GLuint i; 923464ebd5Sriastradh for (i=0;i<n;i++) { 933464ebd5Sriastradh GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F); 943464ebd5Sriastradh GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F); 953464ebd5Sriastradh GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F); 963464ebd5Sriastradh GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F); 97af69d88dSmrg rgba[i][RCOMP] = rMap[F_TO_I(r * rscale)]; 98af69d88dSmrg rgba[i][GCOMP] = gMap[F_TO_I(g * gscale)]; 99af69d88dSmrg rgba[i][BCOMP] = bMap[F_TO_I(b * bscale)]; 100af69d88dSmrg rgba[i][ACOMP] = aMap[F_TO_I(a * ascale)]; 1013464ebd5Sriastradh } 1023464ebd5Sriastradh} 1033464ebd5Sriastradh 1043464ebd5Sriastradh/* 1053464ebd5Sriastradh * Map color indexes to float rgba values. 1063464ebd5Sriastradh */ 1073464ebd5Sriastradhvoid 1083464ebd5Sriastradh_mesa_map_ci_to_rgba( const struct gl_context *ctx, GLuint n, 1093464ebd5Sriastradh const GLuint index[], GLfloat rgba[][4] ) 1103464ebd5Sriastradh{ 1113464ebd5Sriastradh GLuint rmask = ctx->PixelMaps.ItoR.Size - 1; 1123464ebd5Sriastradh GLuint gmask = ctx->PixelMaps.ItoG.Size - 1; 1133464ebd5Sriastradh GLuint bmask = ctx->PixelMaps.ItoB.Size - 1; 1143464ebd5Sriastradh GLuint amask = ctx->PixelMaps.ItoA.Size - 1; 1153464ebd5Sriastradh const GLfloat *rMap = ctx->PixelMaps.ItoR.Map; 1163464ebd5Sriastradh const GLfloat *gMap = ctx->PixelMaps.ItoG.Map; 1173464ebd5Sriastradh const GLfloat *bMap = ctx->PixelMaps.ItoB.Map; 1183464ebd5Sriastradh const GLfloat *aMap = ctx->PixelMaps.ItoA.Map; 1193464ebd5Sriastradh GLuint i; 1203464ebd5Sriastradh for (i=0;i<n;i++) { 1213464ebd5Sriastradh rgba[i][RCOMP] = rMap[index[i] & rmask]; 1223464ebd5Sriastradh rgba[i][GCOMP] = gMap[index[i] & gmask]; 1233464ebd5Sriastradh rgba[i][BCOMP] = bMap[index[i] & bmask]; 1243464ebd5Sriastradh rgba[i][ACOMP] = aMap[index[i] & amask]; 1253464ebd5Sriastradh } 1263464ebd5Sriastradh} 1273464ebd5Sriastradh 1283464ebd5Sriastradh 1293464ebd5Sriastradhvoid 1303464ebd5Sriastradh_mesa_scale_and_bias_depth(const struct gl_context *ctx, GLuint n, 1313464ebd5Sriastradh GLfloat depthValues[]) 1323464ebd5Sriastradh{ 1333464ebd5Sriastradh const GLfloat scale = ctx->Pixel.DepthScale; 1343464ebd5Sriastradh const GLfloat bias = ctx->Pixel.DepthBias; 1353464ebd5Sriastradh GLuint i; 1363464ebd5Sriastradh for (i = 0; i < n; i++) { 1373464ebd5Sriastradh GLfloat d = depthValues[i] * scale + bias; 1383464ebd5Sriastradh depthValues[i] = CLAMP(d, 0.0F, 1.0F); 1393464ebd5Sriastradh } 1403464ebd5Sriastradh} 1413464ebd5Sriastradh 1423464ebd5Sriastradh 1433464ebd5Sriastradhvoid 1443464ebd5Sriastradh_mesa_scale_and_bias_depth_uint(const struct gl_context *ctx, GLuint n, 1453464ebd5Sriastradh GLuint depthValues[]) 1463464ebd5Sriastradh{ 1473464ebd5Sriastradh const GLdouble max = (double) 0xffffffff; 1483464ebd5Sriastradh const GLdouble scale = ctx->Pixel.DepthScale; 1493464ebd5Sriastradh const GLdouble bias = ctx->Pixel.DepthBias * max; 1503464ebd5Sriastradh GLuint i; 1513464ebd5Sriastradh for (i = 0; i < n; i++) { 1523464ebd5Sriastradh GLdouble d = (GLdouble) depthValues[i] * scale + bias; 1533464ebd5Sriastradh d = CLAMP(d, 0.0, max); 1543464ebd5Sriastradh depthValues[i] = (GLuint) d; 1553464ebd5Sriastradh } 1563464ebd5Sriastradh} 1573464ebd5Sriastradh 1583464ebd5Sriastradh/** 1593464ebd5Sriastradh * Apply various pixel transfer operations to an array of RGBA pixels 1603464ebd5Sriastradh * as indicated by the transferOps bitmask 1613464ebd5Sriastradh */ 1623464ebd5Sriastradhvoid 1633464ebd5Sriastradh_mesa_apply_rgba_transfer_ops(struct gl_context *ctx, GLbitfield transferOps, 1643464ebd5Sriastradh GLuint n, GLfloat rgba[][4]) 1653464ebd5Sriastradh{ 1663464ebd5Sriastradh /* scale & bias */ 1673464ebd5Sriastradh if (transferOps & IMAGE_SCALE_BIAS_BIT) { 1683464ebd5Sriastradh _mesa_scale_and_bias_rgba(n, rgba, 1693464ebd5Sriastradh ctx->Pixel.RedScale, ctx->Pixel.GreenScale, 1703464ebd5Sriastradh ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale, 1713464ebd5Sriastradh ctx->Pixel.RedBias, ctx->Pixel.GreenBias, 1723464ebd5Sriastradh ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias); 1733464ebd5Sriastradh } 1743464ebd5Sriastradh /* color map lookup */ 1753464ebd5Sriastradh if (transferOps & IMAGE_MAP_COLOR_BIT) { 1763464ebd5Sriastradh _mesa_map_rgba( ctx, n, rgba ); 1773464ebd5Sriastradh } 1783464ebd5Sriastradh 1793464ebd5Sriastradh /* clamping to [0,1] */ 1803464ebd5Sriastradh if (transferOps & IMAGE_CLAMP_BIT) { 1813464ebd5Sriastradh GLuint i; 1823464ebd5Sriastradh for (i = 0; i < n; i++) { 1833464ebd5Sriastradh rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F); 1843464ebd5Sriastradh rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F); 1853464ebd5Sriastradh rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F); 1863464ebd5Sriastradh rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F); 1873464ebd5Sriastradh } 1883464ebd5Sriastradh } 1893464ebd5Sriastradh} 1903464ebd5Sriastradh 1913464ebd5Sriastradh 1923464ebd5Sriastradh/* 1933464ebd5Sriastradh * Apply color index shift and offset to an array of pixels. 1943464ebd5Sriastradh */ 1953464ebd5Sriastradhvoid 1963464ebd5Sriastradh_mesa_shift_and_offset_ci(const struct gl_context *ctx, 1973464ebd5Sriastradh GLuint n, GLuint indexes[]) 1983464ebd5Sriastradh{ 1993464ebd5Sriastradh GLint shift = ctx->Pixel.IndexShift; 2003464ebd5Sriastradh GLint offset = ctx->Pixel.IndexOffset; 2013464ebd5Sriastradh GLuint i; 2023464ebd5Sriastradh if (shift > 0) { 2033464ebd5Sriastradh for (i=0;i<n;i++) { 2043464ebd5Sriastradh indexes[i] = (indexes[i] << shift) + offset; 2053464ebd5Sriastradh } 2063464ebd5Sriastradh } 2073464ebd5Sriastradh else if (shift < 0) { 2083464ebd5Sriastradh shift = -shift; 2093464ebd5Sriastradh for (i=0;i<n;i++) { 2103464ebd5Sriastradh indexes[i] = (indexes[i] >> shift) + offset; 2113464ebd5Sriastradh } 2123464ebd5Sriastradh } 2133464ebd5Sriastradh else { 2143464ebd5Sriastradh for (i=0;i<n;i++) { 2153464ebd5Sriastradh indexes[i] = indexes[i] + offset; 2163464ebd5Sriastradh } 2173464ebd5Sriastradh } 2183464ebd5Sriastradh} 2193464ebd5Sriastradh 2203464ebd5Sriastradh 2213464ebd5Sriastradh 2223464ebd5Sriastradh/** 2233464ebd5Sriastradh * Apply color index shift, offset and table lookup to an array 2243464ebd5Sriastradh * of color indexes; 2253464ebd5Sriastradh */ 2263464ebd5Sriastradhvoid 2273464ebd5Sriastradh_mesa_apply_ci_transfer_ops(const struct gl_context *ctx, 2283464ebd5Sriastradh GLbitfield transferOps, 2293464ebd5Sriastradh GLuint n, GLuint indexes[]) 2303464ebd5Sriastradh{ 2313464ebd5Sriastradh if (transferOps & IMAGE_SHIFT_OFFSET_BIT) { 2323464ebd5Sriastradh _mesa_shift_and_offset_ci(ctx, n, indexes); 2333464ebd5Sriastradh } 2343464ebd5Sriastradh if (transferOps & IMAGE_MAP_COLOR_BIT) { 2353464ebd5Sriastradh const GLuint mask = ctx->PixelMaps.ItoI.Size - 1; 2363464ebd5Sriastradh GLuint i; 2373464ebd5Sriastradh for (i = 0; i < n; i++) { 2383464ebd5Sriastradh const GLuint j = indexes[i] & mask; 239af69d88dSmrg indexes[i] = F_TO_I(ctx->PixelMaps.ItoI.Map[j]); 2403464ebd5Sriastradh } 2413464ebd5Sriastradh } 2423464ebd5Sriastradh} 2433464ebd5Sriastradh 2443464ebd5Sriastradh 2453464ebd5Sriastradh/** 2463464ebd5Sriastradh * Apply stencil index shift, offset and table lookup to an array 2473464ebd5Sriastradh * of stencil values. 2483464ebd5Sriastradh */ 2493464ebd5Sriastradhvoid 2503464ebd5Sriastradh_mesa_apply_stencil_transfer_ops(const struct gl_context *ctx, GLuint n, 251af69d88dSmrg GLubyte stencil[]) 2523464ebd5Sriastradh{ 2533464ebd5Sriastradh if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) { 2543464ebd5Sriastradh const GLint offset = ctx->Pixel.IndexOffset; 2553464ebd5Sriastradh GLint shift = ctx->Pixel.IndexShift; 2563464ebd5Sriastradh GLuint i; 2573464ebd5Sriastradh if (shift > 0) { 2583464ebd5Sriastradh for (i = 0; i < n; i++) { 2593464ebd5Sriastradh stencil[i] = (stencil[i] << shift) + offset; 2603464ebd5Sriastradh } 2613464ebd5Sriastradh } 2623464ebd5Sriastradh else if (shift < 0) { 2633464ebd5Sriastradh shift = -shift; 2643464ebd5Sriastradh for (i = 0; i < n; i++) { 2653464ebd5Sriastradh stencil[i] = (stencil[i] >> shift) + offset; 2663464ebd5Sriastradh } 2673464ebd5Sriastradh } 2683464ebd5Sriastradh else { 2693464ebd5Sriastradh for (i = 0; i < n; i++) { 2703464ebd5Sriastradh stencil[i] = stencil[i] + offset; 2713464ebd5Sriastradh } 2723464ebd5Sriastradh } 2733464ebd5Sriastradh } 2743464ebd5Sriastradh if (ctx->Pixel.MapStencilFlag) { 2753464ebd5Sriastradh GLuint mask = ctx->PixelMaps.StoS.Size - 1; 2763464ebd5Sriastradh GLuint i; 2773464ebd5Sriastradh for (i = 0; i < n; i++) { 278af69d88dSmrg stencil[i] = (GLubyte) ctx->PixelMaps.StoS.Map[ stencil[i] & mask ]; 2793464ebd5Sriastradh } 2803464ebd5Sriastradh } 2813464ebd5Sriastradh} 282