rearranged order of some functions
[mesa.git] / src / mesa / main / image.c
index 58c76deb0da7202b018aaca94441385f9bfe7282..b1f64fb3fdfcd77ecc10924d4bfa4df069ffeb68 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: image.c,v 1.15 2000/01/05 09:21:32 brianp Exp $ */
+/* $Id: image.c,v 1.20 2000/03/19 01:10:12 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
@@ -493,22 +493,25 @@ GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
  * Unpack a 32x32 pixel polygon stipple from user memory using the
  * current pixel unpack settings.
  */
-void gl_unpack_polygon_stipple( const GLcontext *ctx,
-                                const GLubyte *pattern, GLuint dest[32] )
+void
+_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
+                              const struct gl_pixelstore_attrib *unpacking )
 {
-   GLint i;
-   for (i = 0; i < 32; i++) {
-      GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
-                                  32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
-      dest[i] = (src[0] << 24)
-              | (src[1] << 16)
-              | (src[2] <<  8)
-              | (src[3]      );
-   }
-
-   /* Bit flipping within each byte */
-   if (ctx->Unpack.LsbFirst) {
-      gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
+   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap( 32, 32, pattern, unpacking );
+   if (ptrn) {
+      /* Convert pattern from GLubytes to GLuints and handle big/little
+       * endian differences
+       */
+      GLubyte *p = ptrn;
+      GLint i;
+      for (i = 0; i < 32; i++) {
+         dest[i] = (p[0] << 24)
+                 | (p[1] << 16)
+                 | (p[2] <<  8)
+                 | (p[3]      );
+         p += 4;
+      }
+      FREE(ptrn);
    }
 }
 
@@ -518,24 +521,23 @@ void gl_unpack_polygon_stipple( const GLcontext *ctx,
  * Pack polygon stipple into user memory given current pixel packing
  * settings.
  */
-void gl_pack_polygon_stipple( const GLcontext *ctx,
-                              const GLuint pattern[32],
-                              GLubyte *dest )
+void
+_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
+                            const struct gl_pixelstore_attrib *packing )
 {
+   /* Convert pattern from GLuints to GLubytes to handle big/little
+    * endian differences.
+    */
+   GLubyte ptrn[32*4];
    GLint i;
    for (i = 0; i < 32; i++) {
-      GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
-                                  32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
-      dst[0] = (pattern[i] >> 24) & 0xff;
-      dst[1] = (pattern[i] >> 16) & 0xff;
-      dst[2] = (pattern[i] >>  8) & 0xff;
-      dst[3] = (pattern[i]      ) & 0xff;
-
-      /* Bit flipping within each byte */
-      if (ctx->Pack.LsbFirst) {
-         gl_flip_bytes( (GLubyte *) dst, 4 );
-      }
+      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
+      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
+      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
+      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
    }
+
+   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
 }
 
 
@@ -1516,12 +1518,12 @@ extract_uint_indexes(GLuint n, GLuint indexes[],
                for (i = 0; i < n; i++) {
                   GLfloat value = s[i];
                   SWAP4BYTE(value);
-                  indexes[i] = value;
+                  indexes[i] = (GLuint) value;
                }
             }
             else {
                for (i = 0; i < n; i++)
-                  indexes[i] = s[i];
+                  indexes[i] = (GLuint) s[i];
             }
          }
          break;
@@ -1579,10 +1581,19 @@ extract_float_rgba(GLuint n, GLfloat rgba[][4],
           srcType == GL_INT ||
           srcType == GL_FLOAT ||
           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
+          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
+          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
+          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
-          srcType == GL_UNSIGNED_INT_10_10_10_2);
+          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
+          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
+          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
+
+   rComp = gComp = bComp = aComp = -1;
 
    switch (srcFormat) {
       case GL_RED:
@@ -1978,10 +1989,10 @@ extract_float_rgba(GLuint n, GLfloat rgba[][4],
             for (i = 0; i < n; i ++) {
                GLuint p = uisrc[i];
                SWAP4BYTE(p);
-               rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
-               rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
-               rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
-               rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
+               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
+               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
+               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
+               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
             }
          }
          else {
@@ -1989,10 +2000,10 @@ extract_float_rgba(GLuint n, GLfloat rgba[][4],
             GLuint i;
             for (i = 0; i < n; i ++) {
                GLuint p = uisrc[i];
-               rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
-               rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
-               rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
-               rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
+               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
+               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
+               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
+               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
             }
          }
          break;
@@ -2086,10 +2097,17 @@ _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
           srcType == GL_INT ||
           srcType == GL_FLOAT ||
           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
+          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
+          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
+          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
+          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
-          srcType == GL_UNSIGNED_INT_10_10_10_2);
+          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
+          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
+          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
 
    /* this is intended for RGBA mode */
    assert(ctx->Visual->RGBAflag);
@@ -2628,9 +2646,10 @@ _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
 
    /* clamp depth values to [0,1] and convert from floats to integers */
    {
+      const GLfloat zs = ctx->Visual->DepthMaxF;
       GLuint i;
       for (i = 0; i < n; i++) {
-         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * DEPTH_SCALE);
+         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
       }
    }
 
@@ -2810,3 +2829,94 @@ _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
 
    return buffer;
 }
+
+
+/*
+ * Pack bitmap data.
+ */
+void
+_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
+                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
+{
+   GLint row, width_in_bytes;
+   const GLubyte *src;
+
+   if (!source)
+      return;
+
+   width_in_bytes = CEILING( width, 8 );
+   src = source;
+   for (row = 0; row < height; row++) {
+      GLubyte *dst = gl_pixel_addr_in_image( packing, dest, width, height,
+                                             GL_COLOR_INDEX, GL_BITMAP,
+                                             0, row, 0 );
+      if (!dst)
+         return;
+
+      if (packing->SkipPixels == 0) {
+         MEMCPY( dst, src, width_in_bytes );
+         if (packing->LsbFirst) {
+            gl_flip_bytes( dst, width_in_bytes );
+         }
+      }
+      else {
+         /* handling SkipPixels is a bit tricky (no pun intended!) */
+         GLint i;
+         if (packing->LsbFirst) {
+            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
+            GLubyte dstMask = 128;
+            const GLubyte *s = src;
+            GLubyte *d = dst;
+            *d = 0;
+            for (i = 0; i < width; i++) {
+               if (*s & srcMask) {
+                  *d |= dstMask;
+               }
+               if (srcMask == 128) {
+                  srcMask = 1;
+                  s++;
+               }
+               else {
+                  srcMask = srcMask << 1;
+               }
+               if (dstMask == 1) {
+                  dstMask = 128;
+                  d++;
+                  *d = 0;
+               }
+               else {
+                  dstMask = dstMask >> 1;
+               }
+            }
+         }
+         else {
+            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
+            GLubyte dstMask = 128;
+            const GLubyte *s = src;
+            GLubyte *d = dst;
+            *d = 0;
+            for (i = 0; i < width; i++) {
+               if (*s & srcMask) {
+                  *d |= dstMask;
+               }
+               if (srcMask == 1) {
+                  srcMask = 128;
+                  s++;
+               }
+               else {
+                  srcMask = srcMask >> 1;
+               }
+               if (dstMask == 1) {
+                  dstMask = 128;
+                  d++;
+                  *d = 0;
+               }
+               else {
+                  dstMask = dstMask >> 1;
+               }
+            }
+         }
+      }
+      src += width_in_bytes;
+   }
+}