home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / copypix.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  16KB  |  548 lines

  1. /* $Id: copypix.c,v 1.4.2.2 1999/12/03 18:25:25 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  * 
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  8.  * 
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  * 
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  * 
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #ifdef PC_HEADER
  32. #include "all.h"
  33. #else
  34. #ifndef XFree86Server
  35. #include <string.h>
  36. #else
  37. #include "GL/xf86glx.h"
  38. #endif
  39. #include "context.h"
  40. #include "copypix.h"
  41. #include "depth.h"
  42. #include "feedback.h"
  43. #include "macros.h"
  44. #include "mmath.h"
  45. #include "pixel.h"
  46. #include "span.h"
  47. #include "stencil.h"
  48. #include "types.h"
  49. #include "zoom.h"
  50. #endif
  51.  
  52.  
  53. /*
  54.  * Determine if there's overlap in an image copy
  55.  */
  56. static GLboolean
  57. regions_overlap(int srcx, int srcy, int dstx, int dsty, int width, int height,
  58.                 float zoomX, float zoomY)
  59. {
  60.    if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
  61.       return GL_FALSE;
  62.    }
  63.    else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
  64.       return GL_FALSE;
  65.    }
  66.    else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
  67.       return GL_FALSE;
  68.    }
  69.    else {
  70.       return GL_TRUE;
  71.    }
  72. }
  73.  
  74.  
  75.  
  76.  
  77. static void copy_rgba_pixels( GLcontext* ctx,
  78.                               GLint srcx, GLint srcy,
  79.                               GLint width, GLint height,
  80.                               GLint destx, GLint desty )
  81. {
  82.    GLdepth zspan[MAX_WIDTH];
  83.    GLubyte rgba[MAX_WIDTH][4];
  84.    GLubyte *prgba,*p;
  85.    GLboolean quick_draw;
  86.    GLint sy, dy, stepy;
  87.    GLint i, j;
  88.    GLboolean changeBuffer;
  89.    GLubyte *saveAlpha;
  90.    const GLboolean zoom = ctx->Pixel.ZoomX!=1.0F || ctx->Pixel.ZoomY!=1.0F;
  91.    GLboolean needbuffer;
  92.  
  93.    /* Determine if copy should be done bottom-to-top or top-to-bottom */
  94.    if (srcy<desty) {
  95.       /* top-down  max-to-min */
  96.       sy = srcy + height - 1;
  97.       dy = desty + height - 1;
  98.       stepy = -1;
  99.    }
  100.    else {
  101.       /* bottom-up  min-to-max */
  102.       sy = srcy;
  103.       dy = desty;
  104.       stepy = 1;
  105.    }
  106.  
  107.    needbuffer = regions_overlap(srcx, srcy, destx, desty, width, height,
  108.                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
  109.  
  110.    if (ctx->Depth.Test || ctx->Fog.Enabled) {
  111.       /* fill in array of z values */
  112.       GLint z = (GLint) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
  113.       for (i=0;i<width;i++) {
  114.          zspan[i] = z;
  115.       }
  116.    }
  117.  
  118.    if (ctx->RasterMask==0 && !zoom
  119.        && destx>=0 && destx+width<=ctx->Buffer->Width) {
  120.       quick_draw = GL_TRUE;
  121.    }
  122.    else {
  123.       quick_draw = GL_FALSE;
  124.    }
  125.  
  126.    /* If read and draw buffer are different we must do buffer switching */
  127.    saveAlpha = ctx->Buffer->Alpha;
  128.    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer;
  129.  
  130.    if (needbuffer) {
  131.       GLint ssy = sy;
  132.       prgba = (GLubyte *) MALLOC(width*height*sizeof(GLubyte)*4);
  133.       if (!prgba) {
  134.          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
  135.          return;
  136.       }
  137.       p = prgba;
  138.       if (changeBuffer) {
  139.          (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
  140.          if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
  141.             ctx->Buffer->Alpha = ctx->Buffer->FrontLeftAlpha;
  142.          else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
  143.             ctx->Buffer->Alpha = ctx->Buffer->BackLeftAlpha;
  144.          else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
  145.             ctx->Buffer->Alpha = ctx->Buffer->FrontRightAlpha;
  146.          else
  147.             ctx->Buffer->Alpha = ctx->Buffer->BackRightAlpha;
  148.       }
  149.       for (j=0; j<height; j++, ssy+=stepy) {
  150.          gl_read_rgba_span( ctx, width, srcx, ssy,(GLubyte (*)[4]) p );
  151.          p += (width*sizeof(GLubyte)*4);
  152.       }
  153.       p = prgba;
  154.    }
  155.  
  156.    for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
  157.       if (needbuffer) {
  158.          MEMCPY(rgba, p, width * sizeof(GLubyte) * 4);
  159.          p += (width * sizeof(GLubyte) * 4);
  160.       }
  161.       else {
  162.          if (changeBuffer) {
  163.             (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
  164.             if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
  165.                ctx->Buffer->Alpha = ctx->Buffer->FrontLeftAlpha;
  166.             else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
  167.                ctx->Buffer->Alpha = ctx->Buffer->BackLeftAlpha;
  168.             else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
  169.                ctx->Buffer->Alpha = ctx->Buffer->FrontRightAlpha;
  170.             else
  171.                ctx->Buffer->Alpha = ctx->Buffer->BackRightAlpha;
  172.          }
  173.          gl_read_rgba_span( ctx, width, srcx, sy, rgba );
  174.       }
  175.       if (ctx->Pixel.ScaleOrBiasRGBA) {
  176.          gl_scale_and_bias_rgba( ctx, width, rgba );
  177.       }
  178.       if (ctx->Pixel.MapColorFlag) {
  179.          gl_map_rgba( ctx, width, rgba );
  180.       }
  181.       if (quick_draw && dy>=0 && dy<ctx->Buffer->Height) {
  182.          (*ctx->Driver.WriteRGBASpan)( ctx, width, destx, dy, 
  183.                        (const GLubyte (*)[4])rgba, NULL );
  184.  
  185.       }
  186.       else if (zoom) {
  187.          gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 
  188.                     (const GLubyte (*)[4])rgba, desty);
  189.       }
  190.       else {
  191.          gl_write_rgba_span( ctx, width, destx, dy, zspan, rgba, GL_BITMAP );
  192.       }
  193.    }
  194.  
  195.    if (needbuffer)
  196.       FREE(prgba);
  197.  
  198.    /* Restore current alpha buffer pointer */
  199.    ctx->Buffer->Alpha = saveAlpha;
  200.    if (changeBuffer)
  201.       (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
  202. }
  203.  
  204.  
  205. static void copy_ci_pixels( GLcontext* ctx,
  206.                             GLint srcx, GLint srcy, GLint width, GLint height,
  207.                             GLint destx, GLint desty )
  208. {
  209.    GLdepth zspan[MAX_WIDTH];
  210.    GLuint *pci,*p;
  211.    GLint sy, dy, stepy;
  212.    GLint i, j;
  213.    GLboolean changeBuffer;
  214.    const GLboolean zoom = ctx->Pixel.ZoomX!=1.0F || ctx->Pixel.ZoomY!=1.0F;
  215.    const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
  216.    GLboolean needbuffer;
  217.  
  218.    /* Determine if copy should be bottom-to-top or top-to-bottom */
  219.    if (srcy<desty) {
  220.       /* top-down  max-to-min */
  221.       sy = srcy + height - 1;
  222.       dy = desty + height - 1;
  223.       stepy = -1;
  224.    }
  225.    else {
  226.       /* bottom-up  min-to-max */
  227.       sy = srcy;
  228.       dy = desty;
  229.       stepy = 1;
  230.    }
  231.  
  232.    needbuffer = regions_overlap(srcx, srcy, destx, desty, width, height,
  233.                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
  234.  
  235.    if (ctx->Depth.Test || ctx->Fog.Enabled) {
  236.       /* fill in array of z values */
  237.       GLint z = (GLint) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
  238.       for (i=0;i<width;i++) {
  239.          zspan[i] = z;
  240.       }
  241.    }
  242.  
  243.    /* If read and draw buffer are different we must do buffer switching */
  244.    changeBuffer = ctx->Pixel.ReadBuffer!=ctx->Color.DrawBuffer;
  245.  
  246.    if (needbuffer) {
  247.       GLint ssy = sy;
  248.       pci = (GLuint *) MALLOC(width * height * sizeof(GLuint));
  249.       if (!pci) {
  250.          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
  251.          return;
  252.       }
  253.       p = pci;
  254.       if (changeBuffer) {
  255.          (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
  256.       }
  257.       for (j=0; j<height; j++, ssy+=stepy) {
  258.          gl_read_index_span( ctx, width, srcx, ssy, p );
  259.          p += width;
  260.       }
  261.       p = pci;
  262.    }
  263.  
  264.    for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
  265.       GLuint indexes[MAX_WIDTH];
  266.       if (needbuffer) {
  267.          MEMCPY(indexes, p, width * sizeof(GLuint));
  268.          p += width;
  269.       }
  270.       else {
  271.          if (changeBuffer) {
  272.             (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
  273.          }
  274.          gl_read_index_span( ctx, width, srcx, sy, indexes );
  275.       }
  276.  
  277.       if (shift_or_offset) {
  278.          gl_shift_and_offset_ci( ctx, width, indexes );
  279.       }
  280.       if (ctx->Pixel.MapColorFlag) {
  281.          gl_map_ci( ctx, width, indexes );
  282.       }
  283.  
  284.       if (changeBuffer) {
  285.          (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
  286.       }
  287.       if (zoom) {
  288.          gl_write_zoomed_index_span( ctx, width, destx, dy, zspan, indexes, desty );
  289.       }
  290.       else {
  291.          gl_write_index_span( ctx, width, destx, dy, zspan, indexes, GL_BITMAP );
  292.       }
  293.    }
  294.  
  295.    if (needbuffer)
  296.       FREE(pci);
  297.  
  298.    if (changeBuffer)
  299.       (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
  300. }
  301.  
  302.  
  303.  
  304. /*
  305.  * TODO: Optimize!!!!
  306.  */
  307. static void copy_depth_pixels( GLcontext* ctx, GLint srcx, GLint srcy,
  308.                                GLint width, GLint height,
  309.                                GLint destx, GLint desty )
  310. {
  311.    GLfloat depth[MAX_WIDTH];
  312.    GLdepth zspan[MAX_WIDTH];
  313.    GLfloat *p,*pdepth;
  314.    GLuint indexes[MAX_WIDTH];
  315.    GLubyte rgba[MAX_WIDTH][4];
  316.    GLint sy, dy, stepy;
  317.    GLint i, j;
  318.    const GLboolean zoom = ctx->Pixel.ZoomX!=1.0F || ctx->Pixel.ZoomY!=1.0F;
  319.    GLboolean needbuffer;
  320.  
  321.    if (!ctx->Buffer->Depth) {
  322.       gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
  323.       return;
  324.    }
  325.  
  326.    /* Determine if copy should be bottom-to-top or top-to-bottom */
  327.    if (srcy<desty) {
  328.       /* top-down  max-to-min */
  329.       sy = srcy + height - 1;
  330.       dy = desty + height - 1;
  331.       stepy = -1;
  332.    }
  333.    else {
  334.       /* bottom-up  min-to-max */
  335.       sy = srcy;
  336.       dy = desty;
  337.       stepy = 1;
  338.    }
  339.  
  340.  
  341.    needbuffer = regions_overlap(srcx, srcy, destx, desty, width, height,
  342.                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
  343.  
  344.    /* setup colors or indexes */
  345.    if (ctx->Visual->RGBAflag) {
  346.       GLuint *rgba32 = (GLuint *) rgba;
  347.       GLuint color = *(GLuint*)( ctx->Current.ByteColor );
  348.       for (i=0; i<width; i++) {
  349.          rgba32[i] = color;
  350.       }
  351.    }
  352.    else {
  353.       for (i=0;i<width;i++) {
  354.          indexes[i] = ctx->Current.Index;
  355.       }
  356.    }
  357.  
  358.    if (needbuffer) {
  359.       GLint ssy = sy;
  360.       pdepth = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
  361.       if (!pdepth) {
  362.          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
  363.          return;
  364.       }
  365.       p = pdepth;
  366.       for (j=0; j<height; j++, ssy+=stepy) {
  367.          (*ctx->Driver.ReadDepthSpanFloat)( ctx, width, srcx, ssy, p );
  368.          p += width;
  369.       }
  370.       p = pdepth;
  371.    }
  372.  
  373.    for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
  374.       if (needbuffer) {
  375.          MEMCPY(depth, p, width * sizeof(GLfloat));
  376.          p += width;
  377.       }
  378.       else {
  379.          (*ctx->Driver.ReadDepthSpanFloat)( ctx, width, srcx, sy, depth );
  380.       }
  381.  
  382.       for (i=0;i<width;i++) {
  383.          GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
  384.          zspan[i] = (GLint) (CLAMP( d, 0.0F, 1.0F ) * DEPTH_SCALE);
  385.       }
  386.  
  387.       if (ctx->Visual->RGBAflag) {
  388.          if (zoom) {
  389.             gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 
  390.                        (const GLubyte (*)[4])rgba, desty );
  391.          }
  392.          else {
  393.             gl_write_rgba_span( ctx, width, destx, dy, zspan, rgba, GL_BITMAP);
  394.          }
  395.       }
  396.       else {
  397.          if (zoom) {
  398.             gl_write_zoomed_index_span( ctx, width, destx, dy,
  399.                                         zspan, indexes, desty );
  400.          }
  401.          else {
  402.             gl_write_index_span( ctx, width, destx, dy,
  403.                                  zspan, indexes, GL_BITMAP );
  404.          }
  405.       }
  406.    }
  407.  
  408.   if (needbuffer)
  409.      FREE(pdepth);
  410. }
  411.  
  412.  
  413.  
  414. static void copy_stencil_pixels( GLcontext* ctx, GLint srcx, GLint srcy,
  415.                                  GLint width, GLint height,
  416.                                  GLint destx, GLint desty )
  417. {
  418.    GLint sy, dy, stepy;
  419.    GLint j;
  420.    GLstencil    *p,*psten;
  421.    const GLboolean zoom = (ctx->Pixel.ZoomX!=1.0F || ctx->Pixel.ZoomY!=1.0F);
  422.    const GLboolean shift_or_offset = ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0;
  423.    GLboolean needbuffer;
  424.  
  425.    if (!ctx->Buffer->Stencil) {
  426.       gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
  427.       return;
  428.    }
  429.  
  430.    /* Determine if copy should be bottom-to-top or top-to-bottom */
  431.    if (srcy<desty) {
  432.       /* top-down  max-to-min */
  433.       sy = srcy + height - 1;
  434.       dy = desty + height - 1;
  435.       stepy = -1;
  436.    }
  437.    else {
  438.       /* bottom-up  min-to-max */
  439.       sy = srcy;
  440.       dy = desty;
  441.       stepy = 1;
  442.    }
  443.  
  444.    needbuffer = regions_overlap(srcx, srcy, destx, desty, width, height,
  445.                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
  446.  
  447.    if (needbuffer) {
  448.       GLint ssy = sy;
  449.       psten = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
  450.       if (!psten) {
  451.          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
  452.          return;
  453.       }
  454.       p = psten;
  455.       for (j=0; j<height; j++, ssy+=stepy) {
  456.          gl_read_stencil_span( ctx, width, srcx, ssy, p );
  457.          p += width;
  458.       }
  459.       p = psten;
  460.    }
  461.  
  462.    for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
  463.       GLstencil stencil[MAX_WIDTH];
  464.  
  465.       if (needbuffer) {
  466.          MEMCPY(stencil, p, width * sizeof(GLstencil));
  467.          p += width;
  468.       }
  469.       else {
  470.          gl_read_stencil_span( ctx, width, srcx, sy, stencil );
  471.       }
  472.  
  473.       if (shift_or_offset) {
  474.          gl_shift_and_offset_stencil( ctx, width, stencil );
  475.       }
  476.       if (ctx->Pixel.MapStencilFlag) {
  477.          gl_map_stencil( ctx, width, stencil );
  478.       }
  479.  
  480.       if (zoom) {
  481.          gl_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
  482.       }
  483.       else {
  484.          gl_write_stencil_span( ctx, width, destx, dy, stencil );
  485.       }
  486.    }
  487.  
  488.    if (needbuffer)
  489.       FREE(psten);
  490. }
  491.  
  492.  
  493.  
  494.  
  495. void gl_CopyPixels( GLcontext* ctx, GLint srcx, GLint srcy,
  496.                     GLsizei width, GLsizei height, GLenum type )
  497. {
  498.    GLint destx, desty;
  499.  
  500.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyPixels");
  501.  
  502.    if (width<0 || height<0) {
  503.       gl_error( ctx, GL_INVALID_VALUE, "glCopyPixels" );
  504.       return;
  505.    }
  506.  
  507.    if (ctx->NewState) {
  508.       gl_update_state(ctx);
  509.    }
  510.  
  511.    if (ctx->RenderMode==GL_RENDER) {
  512.       /* Destination of copy: */
  513.       if (!ctx->Current.RasterPosValid) {
  514.      return;
  515.       }
  516.       destx = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
  517.       desty = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
  518.  
  519.       if (type==GL_COLOR && ctx->Visual->RGBAflag) {
  520.          copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
  521.       }
  522.       else if (type==GL_COLOR && !ctx->Visual->RGBAflag) {
  523.          copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
  524.       }
  525.       else if (type==GL_DEPTH) {
  526.          copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
  527.       }
  528.       else if (type==GL_STENCIL) {
  529.          copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
  530.       }
  531.       else {
  532.      gl_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
  533.       }
  534.    }
  535.    else if (ctx->RenderMode==GL_FEEDBACK) {
  536.       GLfloat color[4];
  537.       UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor );
  538.       FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
  539.       gl_feedback_vertex( ctx, ctx->Current.RasterPos,
  540.               color, ctx->Current.Index,
  541.               ctx->Current.Texcoord[0] );
  542.    }
  543.    else if (ctx->RenderMode==GL_SELECT) {
  544.       gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
  545.    }
  546.  
  547. }
  548.