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"
3401e04c3fSmrg#include "macros.h"
353464ebd5Sriastradh#include "pixeltransfer.h"
367ec681f3Smrg
373464ebd5Sriastradh#include "mtypes.h"
3801e04c3fSmrg#include "util/rounding.h"
393464ebd5Sriastradh
403464ebd5Sriastradh
413464ebd5Sriastradh/*
423464ebd5Sriastradh * Apply scale and bias factors to an array of RGBA pixels.
433464ebd5Sriastradh */
443464ebd5Sriastradhvoid
453464ebd5Sriastradh_mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4],
463464ebd5Sriastradh                          GLfloat rScale, GLfloat gScale,
473464ebd5Sriastradh                          GLfloat bScale, GLfloat aScale,
483464ebd5Sriastradh                          GLfloat rBias, GLfloat gBias,
493464ebd5Sriastradh                          GLfloat bBias, GLfloat aBias)
503464ebd5Sriastradh{
5101e04c3fSmrg   if (rScale != 1.0F || rBias != 0.0F) {
523464ebd5Sriastradh      GLuint i;
533464ebd5Sriastradh      for (i = 0; i < n; i++) {
543464ebd5Sriastradh         rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias;
553464ebd5Sriastradh      }
563464ebd5Sriastradh   }
5701e04c3fSmrg   if (gScale != 1.0F || gBias != 0.0F) {
583464ebd5Sriastradh      GLuint i;
593464ebd5Sriastradh      for (i = 0; i < n; i++) {
603464ebd5Sriastradh         rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias;
613464ebd5Sriastradh      }
623464ebd5Sriastradh   }
6301e04c3fSmrg   if (bScale != 1.0F || bBias != 0.0F) {
643464ebd5Sriastradh      GLuint i;
653464ebd5Sriastradh      for (i = 0; i < n; i++) {
663464ebd5Sriastradh         rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias;
673464ebd5Sriastradh      }
683464ebd5Sriastradh   }
6901e04c3fSmrg   if (aScale != 1.0F || aBias != 0.0F) {
703464ebd5Sriastradh      GLuint i;
713464ebd5Sriastradh      for (i = 0; i < n; i++) {
723464ebd5Sriastradh         rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias;
733464ebd5Sriastradh      }
743464ebd5Sriastradh   }
753464ebd5Sriastradh}
763464ebd5Sriastradh
773464ebd5Sriastradh
783464ebd5Sriastradh/*
793464ebd5Sriastradh * Apply pixel mapping to an array of floating point RGBA pixels.
803464ebd5Sriastradh */
813464ebd5Sriastradhvoid
823464ebd5Sriastradh_mesa_map_rgba( const struct gl_context *ctx, GLuint n, GLfloat rgba[][4] )
833464ebd5Sriastradh{
843464ebd5Sriastradh   const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1);
853464ebd5Sriastradh   const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1);
863464ebd5Sriastradh   const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1);
873464ebd5Sriastradh   const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1);
883464ebd5Sriastradh   const GLfloat *rMap = ctx->PixelMaps.RtoR.Map;
893464ebd5Sriastradh   const GLfloat *gMap = ctx->PixelMaps.GtoG.Map;
903464ebd5Sriastradh   const GLfloat *bMap = ctx->PixelMaps.BtoB.Map;
913464ebd5Sriastradh   const GLfloat *aMap = ctx->PixelMaps.AtoA.Map;
923464ebd5Sriastradh   GLuint i;
933464ebd5Sriastradh   for (i=0;i<n;i++) {
943464ebd5Sriastradh      GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
953464ebd5Sriastradh      GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
963464ebd5Sriastradh      GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
973464ebd5Sriastradh      GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
9801e04c3fSmrg      rgba[i][RCOMP] = rMap[(int)_mesa_lroundevenf(r * rscale)];
9901e04c3fSmrg      rgba[i][GCOMP] = gMap[(int)_mesa_lroundevenf(g * gscale)];
10001e04c3fSmrg      rgba[i][BCOMP] = bMap[(int)_mesa_lroundevenf(b * bscale)];
10101e04c3fSmrg      rgba[i][ACOMP] = aMap[(int)_mesa_lroundevenf(a * ascale)];
1023464ebd5Sriastradh   }
1033464ebd5Sriastradh}
1043464ebd5Sriastradh
1053464ebd5Sriastradh/*
1063464ebd5Sriastradh * Map color indexes to float rgba values.
1073464ebd5Sriastradh */
1083464ebd5Sriastradhvoid
1093464ebd5Sriastradh_mesa_map_ci_to_rgba( const struct gl_context *ctx, GLuint n,
1103464ebd5Sriastradh                      const GLuint index[], GLfloat rgba[][4] )
1113464ebd5Sriastradh{
1123464ebd5Sriastradh   GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1133464ebd5Sriastradh   GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1143464ebd5Sriastradh   GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1153464ebd5Sriastradh   GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1163464ebd5Sriastradh   const GLfloat *rMap = ctx->PixelMaps.ItoR.Map;
1173464ebd5Sriastradh   const GLfloat *gMap = ctx->PixelMaps.ItoG.Map;
1183464ebd5Sriastradh   const GLfloat *bMap = ctx->PixelMaps.ItoB.Map;
1193464ebd5Sriastradh   const GLfloat *aMap = ctx->PixelMaps.ItoA.Map;
1203464ebd5Sriastradh   GLuint i;
1213464ebd5Sriastradh   for (i=0;i<n;i++) {
1223464ebd5Sriastradh      rgba[i][RCOMP] = rMap[index[i] & rmask];
1233464ebd5Sriastradh      rgba[i][GCOMP] = gMap[index[i] & gmask];
1243464ebd5Sriastradh      rgba[i][BCOMP] = bMap[index[i] & bmask];
1253464ebd5Sriastradh      rgba[i][ACOMP] = aMap[index[i] & amask];
1263464ebd5Sriastradh   }
1273464ebd5Sriastradh}
1283464ebd5Sriastradh
1293464ebd5Sriastradh
1303464ebd5Sriastradhvoid
1313464ebd5Sriastradh_mesa_scale_and_bias_depth(const struct gl_context *ctx, GLuint n,
1323464ebd5Sriastradh                           GLfloat depthValues[])
1333464ebd5Sriastradh{
1343464ebd5Sriastradh   const GLfloat scale = ctx->Pixel.DepthScale;
1353464ebd5Sriastradh   const GLfloat bias = ctx->Pixel.DepthBias;
1363464ebd5Sriastradh   GLuint i;
1373464ebd5Sriastradh   for (i = 0; i < n; i++) {
1383464ebd5Sriastradh      GLfloat d = depthValues[i] * scale + bias;
1393464ebd5Sriastradh      depthValues[i] = CLAMP(d, 0.0F, 1.0F);
1403464ebd5Sriastradh   }
1413464ebd5Sriastradh}
1423464ebd5Sriastradh
1433464ebd5Sriastradh
1443464ebd5Sriastradhvoid
1453464ebd5Sriastradh_mesa_scale_and_bias_depth_uint(const struct gl_context *ctx, GLuint n,
1463464ebd5Sriastradh                                GLuint depthValues[])
1473464ebd5Sriastradh{
1483464ebd5Sriastradh   const GLdouble max = (double) 0xffffffff;
1493464ebd5Sriastradh   const GLdouble scale = ctx->Pixel.DepthScale;
1503464ebd5Sriastradh   const GLdouble bias = ctx->Pixel.DepthBias * max;
1513464ebd5Sriastradh   GLuint i;
1523464ebd5Sriastradh   for (i = 0; i < n; i++) {
1533464ebd5Sriastradh      GLdouble d = (GLdouble) depthValues[i] * scale + bias;
1543464ebd5Sriastradh      d = CLAMP(d, 0.0, max);
1553464ebd5Sriastradh      depthValues[i] = (GLuint) d;
1563464ebd5Sriastradh   }
1573464ebd5Sriastradh}
1583464ebd5Sriastradh
1593464ebd5Sriastradh/**
1603464ebd5Sriastradh * Apply various pixel transfer operations to an array of RGBA pixels
1613464ebd5Sriastradh * as indicated by the transferOps bitmask
1623464ebd5Sriastradh */
1633464ebd5Sriastradhvoid
1643464ebd5Sriastradh_mesa_apply_rgba_transfer_ops(struct gl_context *ctx, GLbitfield transferOps,
1653464ebd5Sriastradh                              GLuint n, GLfloat rgba[][4])
1663464ebd5Sriastradh{
1673464ebd5Sriastradh   /* scale & bias */
1683464ebd5Sriastradh   if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1693464ebd5Sriastradh      _mesa_scale_and_bias_rgba(n, rgba,
1703464ebd5Sriastradh                                ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1713464ebd5Sriastradh                                ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1723464ebd5Sriastradh                                ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1733464ebd5Sriastradh                                ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1743464ebd5Sriastradh   }
1753464ebd5Sriastradh   /* color map lookup */
1763464ebd5Sriastradh   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1773464ebd5Sriastradh      _mesa_map_rgba( ctx, n, rgba );
1783464ebd5Sriastradh   }
1793464ebd5Sriastradh
1803464ebd5Sriastradh   /* clamping to [0,1] */
1813464ebd5Sriastradh   if (transferOps & IMAGE_CLAMP_BIT) {
1823464ebd5Sriastradh      GLuint i;
1833464ebd5Sriastradh      for (i = 0; i < n; i++) {
1843464ebd5Sriastradh         rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1853464ebd5Sriastradh         rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1863464ebd5Sriastradh         rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1873464ebd5Sriastradh         rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1883464ebd5Sriastradh      }
1893464ebd5Sriastradh   }
1903464ebd5Sriastradh}
1913464ebd5Sriastradh
1923464ebd5Sriastradh
1933464ebd5Sriastradh/*
1943464ebd5Sriastradh * Apply color index shift and offset to an array of pixels.
1953464ebd5Sriastradh */
1963464ebd5Sriastradhvoid
1973464ebd5Sriastradh_mesa_shift_and_offset_ci(const struct gl_context *ctx,
1983464ebd5Sriastradh                          GLuint n, GLuint indexes[])
1993464ebd5Sriastradh{
2003464ebd5Sriastradh   GLint shift = ctx->Pixel.IndexShift;
2013464ebd5Sriastradh   GLint offset = ctx->Pixel.IndexOffset;
2023464ebd5Sriastradh   GLuint i;
2033464ebd5Sriastradh   if (shift > 0) {
2043464ebd5Sriastradh      for (i=0;i<n;i++) {
2053464ebd5Sriastradh         indexes[i] = (indexes[i] << shift) + offset;
2063464ebd5Sriastradh      }
2073464ebd5Sriastradh   }
2083464ebd5Sriastradh   else if (shift < 0) {
2093464ebd5Sriastradh      shift = -shift;
2103464ebd5Sriastradh      for (i=0;i<n;i++) {
2113464ebd5Sriastradh         indexes[i] = (indexes[i] >> shift) + offset;
2123464ebd5Sriastradh      }
2133464ebd5Sriastradh   }
2143464ebd5Sriastradh   else {
2153464ebd5Sriastradh      for (i=0;i<n;i++) {
2163464ebd5Sriastradh         indexes[i] = indexes[i] + offset;
2173464ebd5Sriastradh      }
2183464ebd5Sriastradh   }
2193464ebd5Sriastradh}
2203464ebd5Sriastradh
2213464ebd5Sriastradh
2223464ebd5Sriastradh
2233464ebd5Sriastradh/**
2243464ebd5Sriastradh * Apply color index shift, offset and table lookup to an array
2253464ebd5Sriastradh * of color indexes;
2263464ebd5Sriastradh */
2273464ebd5Sriastradhvoid
2283464ebd5Sriastradh_mesa_apply_ci_transfer_ops(const struct gl_context *ctx,
2293464ebd5Sriastradh                            GLbitfield transferOps,
2303464ebd5Sriastradh                            GLuint n, GLuint indexes[])
2313464ebd5Sriastradh{
2323464ebd5Sriastradh   if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2333464ebd5Sriastradh      _mesa_shift_and_offset_ci(ctx, n, indexes);
2343464ebd5Sriastradh   }
2353464ebd5Sriastradh   if (transferOps & IMAGE_MAP_COLOR_BIT) {
2363464ebd5Sriastradh      const GLuint mask = ctx->PixelMaps.ItoI.Size - 1;
2373464ebd5Sriastradh      GLuint i;
2383464ebd5Sriastradh      for (i = 0; i < n; i++) {
2393464ebd5Sriastradh         const GLuint j = indexes[i] & mask;
24001e04c3fSmrg         indexes[i] = _mesa_lroundevenf(ctx->PixelMaps.ItoI.Map[j]);
2413464ebd5Sriastradh      }
2423464ebd5Sriastradh   }
2433464ebd5Sriastradh}
2443464ebd5Sriastradh
2453464ebd5Sriastradh
2463464ebd5Sriastradh/**
2473464ebd5Sriastradh * Apply stencil index shift, offset and table lookup to an array
2483464ebd5Sriastradh * of stencil values.
2493464ebd5Sriastradh */
2503464ebd5Sriastradhvoid
2513464ebd5Sriastradh_mesa_apply_stencil_transfer_ops(const struct gl_context *ctx, GLuint n,
252af69d88dSmrg                                 GLubyte stencil[])
2533464ebd5Sriastradh{
2543464ebd5Sriastradh   if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
2553464ebd5Sriastradh      const GLint offset = ctx->Pixel.IndexOffset;
2563464ebd5Sriastradh      GLint shift = ctx->Pixel.IndexShift;
2573464ebd5Sriastradh      GLuint i;
2583464ebd5Sriastradh      if (shift > 0) {
2593464ebd5Sriastradh         for (i = 0; i < n; i++) {
2603464ebd5Sriastradh            stencil[i] = (stencil[i] << shift) + offset;
2613464ebd5Sriastradh         }
2623464ebd5Sriastradh      }
2633464ebd5Sriastradh      else if (shift < 0) {
2643464ebd5Sriastradh         shift = -shift;
2653464ebd5Sriastradh         for (i = 0; i < n; i++) {
2663464ebd5Sriastradh            stencil[i] = (stencil[i] >> shift) + offset;
2673464ebd5Sriastradh         }
2683464ebd5Sriastradh      }
2693464ebd5Sriastradh      else {
2703464ebd5Sriastradh         for (i = 0; i < n; i++) {
2713464ebd5Sriastradh            stencil[i] = stencil[i] + offset;
2723464ebd5Sriastradh         }
2733464ebd5Sriastradh      }
2743464ebd5Sriastradh   }
2753464ebd5Sriastradh   if (ctx->Pixel.MapStencilFlag) {
2763464ebd5Sriastradh      GLuint mask = ctx->PixelMaps.StoS.Size - 1;
2773464ebd5Sriastradh      GLuint i;
2783464ebd5Sriastradh      for (i = 0; i < n; i++) {
279af69d88dSmrg         stencil[i] = (GLubyte) ctx->PixelMaps.StoS.Map[ stencil[i] & mask ];
2803464ebd5Sriastradh      }
2813464ebd5Sriastradh   }
2823464ebd5Sriastradh}
283