Overhaul of texture image handling.
[mesa.git] / src / mesa / main / texutil.c
index 9628816795788c09c8064767b43874e2ff653e46..428af0384aa8dfcbebcffbfa48e37238dcf9ea74 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: texutil.c,v 1.7 2000/09/13 22:07:20 brianp Exp $ */
+/* $Id: texutil.c,v 1.11 2000/12/26 05:09:29 keithw Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.4
- * 
+ *
  * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
@@ -33,7 +33,7 @@
 #include "image.h"
 #include "mem.h"
 #include "texutil.h"
-#include "types.h"
+#include "mtypes.h"
 #endif
 
 
@@ -127,13 +127,18 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
             /* store as 8-bit texels */
             if (wScale == 1 && hScale == 1) {
                /* no scaling needed - fast case */
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLubyte *dst = (GLubyte *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
+                 GLuint i;
+                 for (i = 0 ; i < dstWidth ; i++)
+                    fprintf(stderr, "%02x ", src[i]);
+                 fprintf(stderr, "\n");
                   MEMCPY(dst, src, dstWidth * sizeof(GLubyte));
                   dst += dstRowStride;
                   src += srcStride;
@@ -145,8 +150,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                             srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      dst[col] = src[col / wScale];
@@ -164,8 +170,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else {
             /* store as 16-bit texels */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLushort *dst = (GLushort *) dstImage;
@@ -186,8 +193,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row, col;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                   for (col = 0; col < dstWidth; col++) {
@@ -207,8 +215,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_RGB && srcType == GL_UNSIGNED_SHORT_5_6_5) {
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLushort *dst = (GLushort *) dstImage;
@@ -225,8 +234,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      dst[col] = src[col / wScale];
@@ -238,8 +248,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGB && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
 #ifdef DO_32BIT_STORES
@@ -290,8 +301,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col3 = (col / wScale) * 3;
@@ -309,8 +321,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case (used by Quake3) */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
 #ifdef DO_32BIT_STORES
@@ -361,8 +374,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -388,8 +402,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV){
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLushort *dst = (GLushort *) dstImage;
@@ -406,8 +421,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      dst[col] = src[col / wScale];
@@ -419,8 +435,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
 #ifdef DO_32BIT_STORES
@@ -477,8 +494,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -506,8 +524,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV){
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLushort *dst = (GLushort *) dstImage;
@@ -524,8 +543,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      dst[col] = src[col / wScale];
@@ -537,8 +557,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
                GLushort *dst = (GLushort *) dstImage;
@@ -565,8 +586,9 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -595,11 +617,12 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV){
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   MEMCPY(dst, src, dstWidth * sizeof(GLuint));
@@ -609,12 +632,13 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
             }
             else {
                /* must rescale image */
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLuint *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLuint *src = (const GLuint *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      dst[col] = src[col / wScale];
@@ -626,11 +650,12 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint col, col4;
@@ -647,12 +672,13 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
             }
             else {
                /* must rescale image */
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -669,11 +695,12 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGB && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  srcWidth, srcFormat, srcType);
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint col, col3;
@@ -690,12 +717,13 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
             }
             else {
                /* must rescale image */
-               GLuint *dst = dstImage;
+               GLuint *dst = (GLuint *) dstImage;
                GLint row;
                for (row = 0; row < dstHeight; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < dstWidth; col++) {
                      GLint col3 = (col / wScale) * 3;
@@ -715,7 +743,7 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
          }
          if (dstFormat == MESA_FF_R8_G8_B8) {
             /* set alpha bytes to 0xff */
-            GLuint i;
+            GLint i;
             GLubyte *dst = (GLubyte *) dstImage;
             for (i = 0; i < dstWidth * dstHeight; i++) {
                dst[i * 4 + 3] = 0xff;
@@ -736,11 +764,11 @@ _mesa_convert_teximage(MesaIntTexFormat dstFormat,
  * Replace a subregion of a texture image with new data.
  * Input:
  *   dstFormat - destination image format
- *   dstXoffset, dstYoffset - destination for new subregion
- *   dstWidth, dstHeight - total size of dest image
+ *   dstXoffset, dstYoffset - destination for new subregion (in texels)
+ *   dstWidth, dstHeight - total size of dest image (in texels)
  *   dstImage - pointer to dest image
- *   dstRowStride - bytes to jump between image rows
- *   width, height - size of region to copy/replace
+ *   dstRowStride - bytes to jump between image rows (in bytes)
+ *   width, height - size of region to copy/replace (in texels)
  *   srcWidth, srcHeight - size of the corresponding gl_texture_image
  *   srcFormat, srcType - source image format and datatype
  *   srcImage - source image
@@ -778,7 +806,7 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
    dstYoffset *= hScale;
 
    /* XXX hscale != 1 and wscale != 1 not tested!!!! */
-   
+
    switch (dstFormat) {
       case MESA_I8:
       case MESA_L8:
@@ -796,8 +824,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
             /* store as 8-bit texels */
             if (wScale == 1 && hScale == 1) {
                /* no scaling needed - fast case */
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                   width, srcFormat, srcType);
                GLubyte *dst = (GLubyte *) dstImage
@@ -816,8 +845,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      dst[col] = src[col / wScale];
@@ -835,8 +865,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          else {
             /* store as 16-bit texels */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -859,8 +890,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row, col;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                   for (col = 0; col < width; col++) {
@@ -880,8 +912,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_RGB && srcType == GL_UNSIGNED_SHORT_5_6_5) {
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -900,8 +933,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      dst[col] = src[col / wScale];
@@ -913,8 +947,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGB && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -941,8 +976,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      GLint col3 = (col / wScale) * 3;
@@ -960,8 +996,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case (used by Quake3) */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -988,8 +1025,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -1015,8 +1053,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV){
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -1035,8 +1074,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      dst[col] = src[col / wScale];
@@ -1048,8 +1088,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -1078,8 +1119,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -1107,8 +1149,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV){
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -1127,8 +1170,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLushort *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLushort *src = (const GLushort *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      dst[col] = src[col / wScale];
@@ -1140,8 +1184,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLushort *dst = (GLushort *) ((GLubyte *) dstImage
@@ -1170,8 +1215,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      GLint col4 = (col / wScale) * 4;
@@ -1197,11 +1243,12 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
       case MESA_A8_R8_G8_B8:
       case MESA_FF_R8_G8_B8:
          /* 32-bit texels */
-         if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV){
+         if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) {
             /* special, optimized case */
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLuint *dst = (GLuint *) ((GLubyte *) dstImage
@@ -1220,8 +1267,9 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLuint *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLuint *src = (const GLuint *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      dst[col] = src[col / wScale];
@@ -1229,12 +1277,27 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                   dst = (GLuint *) ((GLubyte *) dst + dstRowStride);
                }
             }
+            if (dstFormat == MESA_FF_R8_G8_B8) {
+               /* set alpha bytes to 0xff */
+               GLint row, col;
+               GLubyte *dst = (GLubyte *) dstImage
+                              + dstYoffset * dstRowStride + dstXoffset * 4;
+               assert(wScale == 1 && hScale == 1); /* XXX not done */
+               for (row = 0; row < height; row++) {
+                  for (col = 0; col < width; col++) {
+                     dst[col * 4 + 3] = 0xff;
+                  }
+                  dst = dst + dstRowStride;
+               }
+            }
          }
          else if (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) {
             /* general case */
+            const GLubyte aMask = (dstFormat==MESA_FF_R8_G8_B8) ? 0xff : 0x00;
             if (wScale == 1 && hScale == 1) {
-               const GLubyte *src = _mesa_image_address(packing, srcImage,
-                             srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
+               const GLubyte *src = (const GLubyte *)
+                  _mesa_image_address(packing, srcImage, srcWidth, srcHeight,
+                                      srcFormat, srcType, 0, 0, 0);
                const GLint srcStride = _mesa_image_row_stride(packing,
                                                  width, srcFormat, srcType);
                GLuint *dst = (GLuint *) ((GLubyte *) dstImage
@@ -1246,7 +1309,7 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                      GLubyte r = src[col4 + 0];
                      GLubyte g = src[col4 + 1];
                      GLubyte b = src[col4 + 2];
-                     GLubyte a = src[col4 + 3];
+                     GLubyte a = src[col4 + 3] | aMask;
                      dst[col] = (a << 24) | (r << 16) | (g << 8) | b;
                   }
                   src += srcStride;
@@ -1260,15 +1323,16 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
                GLint row;
                for (row = 0; row < height; row++) {
                   GLint srcRow = row / hScale;
-                  const GLubyte *src = _mesa_image_address(packing, srcImage,
-                        srcWidth, srcHeight, srcFormat, srcType, 0, srcRow, 0);
+                  const GLubyte *src = (const GLubyte *)
+                     _mesa_image_address(packing, srcImage, srcWidth,
+                                  srcHeight, srcFormat, srcType, 0, srcRow, 0);
                   GLint col;
                   for (col = 0; col < width; col++) {
                      GLint col4 = (col / wScale) * 4;
                      GLubyte r = src[col4 + 0];
                      GLubyte g = src[col4 + 1];
                      GLubyte b = src[col4 + 2];
-                     GLubyte a = src[col4 + 3];
+                     GLubyte a = src[col4 + 3] | aMask;
                      dst[col] = (a << 24) | (r << 16) | (g << 8) | b;
                   }
                   dst = (GLuint *) ((GLubyte *) dst + dstRowStride);
@@ -1279,18 +1343,6 @@ _mesa_convert_texsubimage(MesaIntTexFormat dstFormat,
             /* can't handle this source format/type combination */
             return GL_FALSE;
          }
-         if (dstFormat == MESA_FF_R8_G8_B8) {
-            /* set alpha bytes to 0xff */
-            GLint row, col;
-            GLubyte *dst = (GLubyte *) dstImage
-                           + dstYoffset * dstRowStride + dstXoffset;
-            for (row = 0; row < height; row++) {
-               for (col = 0; col < width; col++) {
-                  dst[col * 4 + 3] = 0xff;
-               }
-               dst = dst + dstRowStride;
-            }
-         }
          break;