teximage.c revision b7d076fc96ac27117421653a043d00a95f789d24
1/* $Id: teximage.c,v 1.23 2000/03/21 01:03:40 brianp Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.3
6 *
7 * Copyright (C) 1999-2000  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#ifdef PC_HEADER
29#include "all.h"
30#else
31#include "glheader.h"
32#include "context.h"
33#include "image.h"
34#include "mem.h"
35#include "mmath.h"
36#include "span.h"
37#include "teximage.h"
38#include "texstate.h"
39#include "types.h"
40#endif
41
42
43/*
44 * NOTES:
45 *
46 * Mesa's native texture datatype is GLubyte.  Native formats are
47 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA,
48 * and GL_COLOR_INDEX.
49 * Device drivers are free to implement any internal format they want.
50 */
51
52
53/*
54 * Default pixel packing of Mesa's internal texture images:
55 */
56static struct gl_pixelstore_attrib DefaultPacking = {
57      1,            /* Alignment */
58      0,            /* RowLength */
59      0,            /* SkipPixels */
60      0,            /* SkipRows */
61      0,            /* ImageHeight */
62      0,            /* SkipImages */
63      GL_FALSE,     /* SwapBytes */
64      GL_FALSE      /* LsbFirst */
65};
66
67
68
69/*
70 * Compute log base 2 of n.
71 * If n isn't an exact power of two return -1.
72 * If n<0 return -1.
73 */
74static int
75logbase2( int n )
76{
77   GLint i = 1;
78   GLint log2 = 0;
79
80   if (n<0) {
81      return -1;
82   }
83
84   while ( n > i ) {
85      i *= 2;
86      log2++;
87   }
88   if (i != n) {
89      return -1;
90   }
91   else {
92      return log2;
93   }
94}
95
96
97
98/*
99 * Given an internal texture format enum or 1, 2, 3, 4 return the
100 * corresponding _base_ internal format:  GL_ALPHA, GL_LUMINANCE,
101 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
102 * Return -1 if invalid enum.
103 */
104static GLint
105decode_internal_format( GLint format )
106{
107   switch (format) {
108      case GL_ALPHA:
109      case GL_ALPHA4:
110      case GL_ALPHA8:
111      case GL_ALPHA12:
112      case GL_ALPHA16:
113         return GL_ALPHA;
114      case 1:
115      case GL_LUMINANCE:
116      case GL_LUMINANCE4:
117      case GL_LUMINANCE8:
118      case GL_LUMINANCE12:
119      case GL_LUMINANCE16:
120         return GL_LUMINANCE;
121      case 2:
122      case GL_LUMINANCE_ALPHA:
123      case GL_LUMINANCE4_ALPHA4:
124      case GL_LUMINANCE6_ALPHA2:
125      case GL_LUMINANCE8_ALPHA8:
126      case GL_LUMINANCE12_ALPHA4:
127      case GL_LUMINANCE12_ALPHA12:
128      case GL_LUMINANCE16_ALPHA16:
129         return GL_LUMINANCE_ALPHA;
130      case GL_INTENSITY:
131      case GL_INTENSITY4:
132      case GL_INTENSITY8:
133      case GL_INTENSITY12:
134      case GL_INTENSITY16:
135         return GL_INTENSITY;
136      case 3:
137      case GL_RGB:
138      case GL_R3_G3_B2:
139      case GL_RGB4:
140      case GL_RGB5:
141      case GL_RGB8:
142      case GL_RGB10:
143      case GL_RGB12:
144      case GL_RGB16:
145         return GL_RGB;
146      case 4:
147      case GL_RGBA:
148      case GL_RGBA2:
149      case GL_RGBA4:
150      case GL_RGB5_A1:
151      case GL_RGBA8:
152      case GL_RGB10_A2:
153      case GL_RGBA12:
154      case GL_RGBA16:
155         return GL_RGBA;
156      case GL_COLOR_INDEX:
157      case GL_COLOR_INDEX1_EXT:
158      case GL_COLOR_INDEX2_EXT:
159      case GL_COLOR_INDEX4_EXT:
160      case GL_COLOR_INDEX8_EXT:
161      case GL_COLOR_INDEX12_EXT:
162      case GL_COLOR_INDEX16_EXT:
163         return GL_COLOR_INDEX;
164      default:
165         return -1;  /* error */
166   }
167}
168
169
170
171/*
172 * Given an internal texture format enum or 1, 2, 3, 4 return the
173 * corresponding _base_ internal format:  GL_ALPHA, GL_LUMINANCE,
174 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.  Return the
175 * number of components for the format.  Return -1 if invalid enum.
176 */
177static GLint
178components_in_intformat( GLint format )
179{
180   switch (format) {
181      case GL_ALPHA:
182      case GL_ALPHA4:
183      case GL_ALPHA8:
184      case GL_ALPHA12:
185      case GL_ALPHA16:
186         return 1;
187      case 1:
188      case GL_LUMINANCE:
189      case GL_LUMINANCE4:
190      case GL_LUMINANCE8:
191      case GL_LUMINANCE12:
192      case GL_LUMINANCE16:
193         return 1;
194      case 2:
195      case GL_LUMINANCE_ALPHA:
196      case GL_LUMINANCE4_ALPHA4:
197      case GL_LUMINANCE6_ALPHA2:
198      case GL_LUMINANCE8_ALPHA8:
199      case GL_LUMINANCE12_ALPHA4:
200      case GL_LUMINANCE12_ALPHA12:
201      case GL_LUMINANCE16_ALPHA16:
202         return 2;
203      case GL_INTENSITY:
204      case GL_INTENSITY4:
205      case GL_INTENSITY8:
206      case GL_INTENSITY12:
207      case GL_INTENSITY16:
208         return 1;
209      case 3:
210      case GL_RGB:
211      case GL_R3_G3_B2:
212      case GL_RGB4:
213      case GL_RGB5:
214      case GL_RGB8:
215      case GL_RGB10:
216      case GL_RGB12:
217      case GL_RGB16:
218         return 3;
219      case 4:
220      case GL_RGBA:
221      case GL_RGBA2:
222      case GL_RGBA4:
223      case GL_RGB5_A1:
224      case GL_RGBA8:
225      case GL_RGB10_A2:
226      case GL_RGBA12:
227      case GL_RGBA16:
228         return 4;
229      case GL_COLOR_INDEX:
230      case GL_COLOR_INDEX1_EXT:
231      case GL_COLOR_INDEX2_EXT:
232      case GL_COLOR_INDEX4_EXT:
233      case GL_COLOR_INDEX8_EXT:
234      case GL_COLOR_INDEX12_EXT:
235      case GL_COLOR_INDEX16_EXT:
236         return 1;
237      default:
238         return -1;  /* error */
239   }
240}
241
242
243
244/*
245 * Examine the texImage->Format field and set the Red, Green, Blue, etc
246 * texel component sizes to default values.
247 * These fields are set only here by core Mesa but device drivers may
248 * overwritting these fields to indicate true texel resolution.
249 */
250static void
251set_teximage_component_sizes( struct gl_texture_image *texImage )
252{
253   switch (texImage->Format) {
254      case GL_ALPHA:
255         texImage->RedBits = 0;
256         texImage->GreenBits = 0;
257         texImage->BlueBits = 0;
258         texImage->AlphaBits = 8;
259         texImage->IntensityBits = 0;
260         texImage->LuminanceBits = 0;
261         texImage->IndexBits = 0;
262         break;
263      case GL_LUMINANCE:
264         texImage->RedBits = 0;
265         texImage->GreenBits = 0;
266         texImage->BlueBits = 0;
267         texImage->AlphaBits = 0;
268         texImage->IntensityBits = 0;
269         texImage->LuminanceBits = 8;
270         texImage->IndexBits = 0;
271         break;
272      case GL_LUMINANCE_ALPHA:
273         texImage->RedBits = 0;
274         texImage->GreenBits = 0;
275         texImage->BlueBits = 0;
276         texImage->AlphaBits = 8;
277         texImage->IntensityBits = 0;
278         texImage->LuminanceBits = 8;
279         texImage->IndexBits = 0;
280         break;
281      case GL_INTENSITY:
282         texImage->RedBits = 0;
283         texImage->GreenBits = 0;
284         texImage->BlueBits = 0;
285         texImage->AlphaBits = 0;
286         texImage->IntensityBits = 8;
287         texImage->LuminanceBits = 0;
288         texImage->IndexBits = 0;
289         break;
290      case GL_RED:
291         texImage->RedBits = 8;
292         texImage->GreenBits = 0;
293         texImage->BlueBits = 0;
294         texImage->AlphaBits = 0;
295         texImage->IntensityBits = 0;
296         texImage->LuminanceBits = 0;
297         texImage->IndexBits = 0;
298         break;
299      case GL_GREEN:
300         texImage->RedBits = 0;
301         texImage->GreenBits = 8;
302         texImage->BlueBits = 0;
303         texImage->AlphaBits = 0;
304         texImage->IntensityBits = 0;
305         texImage->LuminanceBits = 0;
306         texImage->IndexBits = 0;
307         break;
308      case GL_BLUE:
309         texImage->RedBits = 0;
310         texImage->GreenBits = 0;
311         texImage->BlueBits = 8;
312         texImage->AlphaBits = 0;
313         texImage->IntensityBits = 0;
314         texImage->LuminanceBits = 0;
315         texImage->IndexBits = 0;
316         break;
317      case GL_RGB:
318      case GL_BGR:
319         texImage->RedBits = 8;
320         texImage->GreenBits = 8;
321         texImage->BlueBits = 8;
322         texImage->AlphaBits = 0;
323         texImage->IntensityBits = 0;
324         texImage->LuminanceBits = 0;
325         texImage->IndexBits = 0;
326         break;
327      case GL_RGBA:
328      case GL_BGRA:
329      case GL_ABGR_EXT:
330         texImage->RedBits = 8;
331         texImage->GreenBits = 8;
332         texImage->BlueBits = 8;
333         texImage->AlphaBits = 8;
334         texImage->IntensityBits = 0;
335         texImage->LuminanceBits = 0;
336         texImage->IndexBits = 0;
337         break;
338      case GL_COLOR_INDEX:
339         texImage->RedBits = 0;
340         texImage->GreenBits = 0;
341         texImage->BlueBits = 0;
342         texImage->AlphaBits = 0;
343         texImage->IntensityBits = 0;
344         texImage->LuminanceBits = 0;
345         texImage->IndexBits = 8;
346         break;
347      default:
348         gl_problem(NULL, "unexpected format in set_teximage_component_sizes");
349   }
350}
351
352
353
354/*
355 * Return new gl_texture_image struct with all fields initialized to zero.
356 */
357struct gl_texture_image *
358gl_alloc_texture_image( void )
359{
360   return CALLOC_STRUCT(gl_texture_image);
361}
362
363
364
365/*
366 * Return a new gl_texture_image struct with most field initialized.
367 */
368static struct gl_texture_image *
369new_texture_image( GLsizei width, GLsizei height, GLsizei depth,
370                   GLint border, GLenum internalFormat )
371{
372   struct gl_texture_image *img = CALLOC_STRUCT(gl_texture_image);
373   if (!img)
374      return NULL;
375
376   img->Format = (GLenum) decode_internal_format(internalFormat);
377   set_teximage_component_sizes( img );
378   img->IntFormat = (GLenum) internalFormat;
379   img->Border = border;
380   img->Width = width;
381   img->Height = height;
382   img->Depth = depth;
383   img->WidthLog2 = logbase2(width - 2 * border);
384   if (height == 1)  /* 1-D texture */
385      img->HeightLog2 = 0;
386   else
387      img->HeightLog2 = logbase2(height - 2 * border);
388   if (depth == 1)   /* 2-D texture */
389      img->DepthLog2 = 0;
390   else
391      img->DepthLog2 = logbase2(depth - 2 * border);
392   img->Width2 = 1 << img->WidthLog2;
393   img->Height2 = 1 << img->HeightLog2;
394   img->Depth2 = 1 << img->DepthLog2;
395   img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
396
397   return img;
398}
399
400
401
402void
403gl_free_texture_image( struct gl_texture_image *teximage )
404{
405   if (teximage->Data) {
406      FREE( teximage->Data );
407      teximage->Data = NULL;
408   }
409   FREE( teximage );
410}
411
412
413
414/* Need this to prevent an out-of-bounds memory access when using
415 * X86 optimized code.
416 */
417#ifdef USE_X86_ASM
418#  define EXTRA_BYTE 1
419#else
420#  define EXTRA_BYTE 0
421#endif
422
423
424
425/*
426 * Called by glTexImage[123]D.  Fill in a texture image with data given
427 * by the client.  All pixel transfer and unpack modes are handled here.
428 * NOTE: All texture image parameters should have already been error checked.
429 */
430static void
431make_texture_image( GLcontext *ctx,
432                    struct gl_texture_image *texImage,
433                    GLenum srcFormat, GLenum srcType, const GLvoid *pixels,
434                    const struct gl_pixelstore_attrib *unpacking)
435{
436   GLint components, numPixels;
437   GLint internalFormat, width, height, depth, border;
438
439   ASSERT(ctx);
440   ASSERT(texImage);
441   ASSERT(!texImage->Data);
442   ASSERT(pixels);
443   ASSERT(unpacking);
444
445   internalFormat = texImage->IntFormat;
446   width = texImage->Width;
447   height = texImage->Height;
448   depth = texImage->Depth;
449   border = texImage->Border;
450   components = components_in_intformat(internalFormat);
451
452   ASSERT(width > 0);
453   ASSERT(height > 0);
454   ASSERT(depth > 0);
455   ASSERT(border == 0 || border == 1);
456   ASSERT(pixels);
457   ASSERT(unpacking);
458   ASSERT(components);
459
460   numPixels = width * height * depth;
461
462   texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE);
463   if (!texImage->Data)
464      return;      /* out of memory */
465
466   /*
467    * OK, the texture image struct has been initialized and the texture
468    * image memory has been allocated.
469    * Now fill in the texture image from the source data.
470    * This includes applying the pixel transfer operations.
471    */
472
473   /* try common 2D texture cases first */
474   if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag
475       && !ctx->Pixel.IndexOffset && !ctx->Pixel.IndexShift
476       && srcType == GL_UNSIGNED_BYTE && depth == 1) {
477
478      if (srcFormat == internalFormat ||
479          (srcFormat == GL_LUMINANCE && internalFormat == 1) ||
480          (srcFormat == GL_LUMINANCE_ALPHA && internalFormat == 2) ||
481          (srcFormat == GL_RGB && internalFormat == 3) ||
482          (srcFormat == GL_RGBA && internalFormat == 4)) {
483         /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
484          * GL_LUMINANCE_ALPHA, etc. texture formats.
485          */
486         const GLubyte *src = (const GLubyte *) _mesa_image_address(
487            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
488         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
489                                                        srcFormat, srcType);
490         GLubyte *dst = texImage->Data;
491         GLint dstBytesPerRow = width * components * sizeof(GLubyte);
492         if (srcStride == dstBytesPerRow) {
493            MEMCPY(dst, src, height * dstBytesPerRow);
494         }
495         else {
496            GLint i;
497            for (i = 0; i < height; i++) {
498               MEMCPY(dst, src, dstBytesPerRow);
499               src += srcStride;
500               dst += dstBytesPerRow;
501            }
502         }
503         return;  /* all done */
504      }
505      else if (srcFormat == GL_RGBA && internalFormat == GL_RGB) {
506         /* commonly used by Quake */
507         const GLubyte *src = (const GLubyte *) _mesa_image_address(
508            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
509         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
510                                                        srcFormat, srcType);
511         GLubyte *dst = texImage->Data;
512         GLint i, j;
513         for (i = 0; i < height; i++) {
514            const GLubyte *s = src;
515            for (j = 0; j < width; j++) {
516               *dst++ = *s++;  /*red*/
517               *dst++ = *s++;  /*green*/
518               *dst++ = *s++;  /*blue*/
519               s++;            /*alpha*/
520            }
521            src += srcStride;
522         }
523         return;  /* all done */
524      }
525   }
526
527
528   /*
529    * General case solutions
530    */
531   if (texImage->Format == GL_COLOR_INDEX) {
532      /* color index texture */
533      const GLint destBytesPerRow = width * components * sizeof(GLubyte);
534      const GLenum dstType = GL_UNSIGNED_BYTE;
535      GLubyte *dest = texImage->Data;
536      GLint img, row;
537      for (img = 0; img < depth; img++) {
538         for (row = 0; row < height; row++) {
539            const GLvoid *source = _mesa_image_address(unpacking,
540                pixels, width, height, srcFormat, srcType, img, row, 0);
541            _mesa_unpack_index_span(ctx, width, dstType, dest,
542                                    srcType, source, unpacking, GL_TRUE);
543            dest += destBytesPerRow;
544         }
545      }
546   }
547   else {
548      /* regular, color texture */
549      const GLint destBytesPerRow = width * components * sizeof(GLubyte);
550      const GLenum dstFormat = texImage->Format;
551      GLubyte *dest = texImage->Data;
552      GLint img, row;
553      for (img = 0; img < depth; img++) {
554         for (row = 0; row < height; row++) {
555            const GLvoid *source = _mesa_image_address(unpacking,
556                   pixels, width, height, srcFormat, srcType, img, row, 0);
557            _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, dest,
558                   srcFormat, srcType, source, unpacking, GL_TRUE);
559            dest += destBytesPerRow;
560         }
561      }
562   }
563}
564
565
566
567/*
568 * glTexImage[123]D can accept a NULL image pointer.  In this case we
569 * create a texture image with unspecified image contents per the OpenGL
570 * spec.  This function creates an empty image for the given texture image.
571 */
572static void
573make_null_texture( struct gl_texture_image *texImage )
574{
575   GLint components;
576   GLint numPixels;
577
578   ASSERT(texImage);
579   ASSERT(!texImage->Data);
580
581   components = components_in_intformat(texImage->IntFormat);
582   numPixels = texImage->Width * texImage->Height * texImage->Depth;
583
584   texImage->Data = (GLubyte *) MALLOC( numPixels * components + EXTRA_BYTE );
585
586   /*
587    * Let's see if anyone finds this.  If glTexImage2D() is called with
588    * a NULL image pointer then load the texture image with something
589    * interesting instead of leaving it indeterminate.
590    */
591   if (texImage->Data) {
592      static const char message[8][32] = {
593         "   X   X  XXXXX   XXX     X    ",
594         "   XX XX  X      X   X   X X   ",
595         "   X X X  X      X      X   X  ",
596         "   X   X  XXXX    XXX   XXXXX  ",
597         "   X   X  X          X  X   X  ",
598         "   X   X  X      X   X  X   X  ",
599         "   X   X  XXXXX   XXX   X   X  ",
600         "                               "
601      };
602
603      GLubyte *imgPtr = texImage->Data;
604      GLint i, j, k;
605      for (i = 0; i < texImage->Height; i++) {
606         GLint srcRow = 7 - i % 8;
607         for (j = 0; j < texImage->Width; j++) {
608            GLint srcCol = j % 32;
609            GLint texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
610            for (k=0;k<components;k++) {
611               *imgPtr++ = (GLubyte) texel;
612            }
613         }
614      }
615   }
616}
617
618
619
620/*
621 * Test glTexImage[123]D() parameters for errors.
622 * Input:
623 *         dimensions - must be 1 or 2 or 3
624 * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
625 */
626static GLboolean
627texture_error_check( GLcontext *ctx, GLenum target,
628                     GLint level, GLint internalFormat,
629                     GLenum format, GLenum type,
630                     GLuint dimensions,
631                     GLint width, GLint height,
632                     GLint depth, GLint border )
633{
634   GLboolean isProxy;
635   GLint iformat;
636
637   if (dimensions == 1) {
638      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_1D);
639      if (target != GL_TEXTURE_1D && !isProxy) {
640         gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
641         return GL_TRUE;
642      }
643   }
644   else if (dimensions == 2) {
645      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D);
646      if (target != GL_TEXTURE_2D && !isProxy) {
647          gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
648          return GL_TRUE;
649      }
650   }
651   else if (dimensions == 3) {
652      isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_3D);
653      if (target != GL_TEXTURE_3D && !isProxy) {
654         gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
655         return GL_TRUE;
656      }
657   }
658   else {
659      gl_problem( ctx, "bad dims in texture_error_check" );
660      return GL_TRUE;
661   }
662
663   /* Border */
664   if (border!=0 && border!=1) {
665      if (!isProxy) {
666         char message[100];
667         sprintf(message, "glTexImage%dD(border)", dimensions);
668         gl_error(ctx, GL_INVALID_VALUE, message);
669      }
670      return GL_TRUE;
671   }
672
673   /* Width */
674   if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
675       || logbase2( width - 2 * border ) < 0) {
676      if (!isProxy) {
677         char message[100];
678         sprintf(message, "glTexImage%dD(width)", dimensions);
679         gl_error(ctx, GL_INVALID_VALUE, message);
680      }
681      return GL_TRUE;
682   }
683
684   /* Height */
685   if (dimensions >= 2) {
686      if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
687          || logbase2( height - 2 * border ) < 0) {
688         if (!isProxy) {
689            char message[100];
690            sprintf(message, "glTexImage%dD(height)", dimensions);
691            gl_error(ctx, GL_INVALID_VALUE, message);
692         }
693         return GL_TRUE;
694      }
695   }
696
697   /* Depth */
698   if (dimensions >= 3) {
699      if (depth < 2 * border || depth > 2 + ctx->Const.MaxTextureSize
700          || logbase2( depth - 2 * border ) < 0) {
701         if (!isProxy) {
702            gl_error( ctx, GL_INVALID_VALUE, "glTexImage3D(depth)" );
703         }
704         return GL_TRUE;
705      }
706   }
707
708   /* Level */
709   if (level<0 || level>=ctx->Const.MaxTextureLevels) {
710      if (!isProxy) {
711         char message[100];
712         sprintf(message, "glTexImage%dD(level)", dimensions);
713         gl_error(ctx, GL_INVALID_VALUE, message);
714      }
715      return GL_TRUE;
716   }
717
718   iformat = decode_internal_format( internalFormat );
719   if (iformat < 0) {
720      if (!isProxy) {
721         char message[100];
722         sprintf(message, "glTexImage%dD(internalFormat)", dimensions);
723         gl_error(ctx, GL_INVALID_VALUE, message);
724      }
725      return GL_TRUE;
726   }
727
728   if (!_mesa_is_legal_format_and_type( format, type )) {
729      /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
730       * is a type/format mismatch.  See 1.2 spec page 94, sec 3.6.4.
731       */
732      if (!isProxy) {
733         char message[100];
734         sprintf(message, "glTexImage%dD(format or type)", dimensions);
735         gl_error(ctx, GL_INVALID_OPERATION, message);
736      }
737      return GL_TRUE;
738   }
739
740   /* if we get here, the parameters are OK */
741   return GL_FALSE;
742}
743
744
745
746/*
747 * Test glTexSubImage[123]D() parameters for errors.
748 * Input:
749 *         dimensions - must be 1 or 2 or 3
750 * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
751 */
752static GLboolean
753subtexture_error_check( GLcontext *ctx, GLuint dimensions,
754                        GLenum target, GLint level,
755                        GLint xoffset, GLint yoffset, GLint zoffset,
756                        GLint width, GLint height, GLint depth,
757                        GLenum format, GLenum type )
758{
759   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
760   struct gl_texture_image *destTex;
761
762   if (dimensions == 1) {
763      if (target != GL_TEXTURE_1D) {
764         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
765         return GL_TRUE;
766      }
767   }
768   else if (dimensions == 2) {
769      if (target != GL_TEXTURE_2D) {
770         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
771         return GL_TRUE;
772      }
773   }
774   else if (dimensions == 3) {
775      if (target != GL_TEXTURE_3D) {
776         gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
777         return GL_TRUE;
778      }
779   }
780   else {
781      gl_problem( ctx, "bad dims in texture_error_check" );
782      return GL_TRUE;
783   }
784
785   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
786      gl_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level)");
787      return GL_TRUE;
788   }
789
790   if (width < 0) {
791      char message[100];
792      sprintf(message, "glTexSubImage%dD(width)", dimensions);
793      gl_error(ctx, GL_INVALID_VALUE, message);
794      return GL_TRUE;
795   }
796   if (height < 0 && dimensions > 1) {
797      char message[100];
798      sprintf(message, "glTexSubImage%dD(height)", dimensions);
799      gl_error(ctx, GL_INVALID_VALUE, message);
800      return GL_TRUE;
801   }
802   if (depth < 0 && dimensions > 2) {
803      char message[100];
804      sprintf(message, "glTexSubImage%dD(depth)", dimensions);
805      gl_error(ctx, GL_INVALID_VALUE, message);
806      return GL_TRUE;
807   }
808
809   destTex = texUnit->CurrentD[2]->Image[level];
810   if (!destTex) {
811      gl_error(ctx, GL_INVALID_OPERATION, "glTexSubImage2D");
812      return GL_TRUE;
813   }
814
815   if (xoffset < -((GLint)destTex->Border)) {
816      gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset)");
817      return GL_TRUE;
818   }
819   if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
820      gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset+width)");
821      return GL_TRUE;
822   }
823   if (dimensions > 1) {
824      if (yoffset < -((GLint)destTex->Border)) {
825         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset)");
826         return GL_TRUE;
827      }
828      if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
829         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset+height)");
830         return GL_TRUE;
831      }
832   }
833   if (dimensions > 2) {
834      if (zoffset < -((GLint)destTex->Border)) {
835         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
836         return GL_TRUE;
837      }
838      if (zoffset + depth  > (GLint) (destTex->Depth+destTex->Border)) {
839         gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
840         return GL_TRUE;
841      }
842   }
843
844   if (!_mesa_is_legal_format_and_type(format, type)) {
845      char message[100];
846      sprintf(message, "glTexSubImage%dD(format or type)", dimensions);
847      gl_error(ctx, GL_INVALID_ENUM, message);
848      return GL_TRUE;
849   }
850
851   return GL_FALSE;
852}
853
854
855/*
856 * Test glCopyTexImage[12]D() parameters for errors.
857 * Input:  dimensions - must be 1 or 2 or 3
858 * Return:  GL_TRUE = an error was detected, GL_FALSE = no errors
859 */
860static GLboolean
861copytexture_error_check( GLcontext *ctx, GLuint dimensions,
862                         GLenum target, GLint level, GLint internalFormat,
863                         GLint width, GLint height, GLint border )
864{
865   GLint iformat;
866
867   if (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) {
868      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1/2D(target)" );
869      return GL_TRUE;
870   }
871
872   if (dimensions == 1 && target != GL_TEXTURE_1D) {
873      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
874      return GL_TRUE;
875   }
876   else if (dimensions == 2 && target != GL_TEXTURE_2D) {
877      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
878      return GL_TRUE;
879   }
880
881   /* Border */
882   if (border!=0 && border!=1) {
883      char message[100];
884      sprintf(message, "glCopyTexImage%dD(border)", dimensions);
885      gl_error(ctx, GL_INVALID_VALUE, message);
886      return GL_TRUE;
887   }
888
889   /* Width */
890   if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
891       || logbase2( width - 2 * border ) < 0) {
892      char message[100];
893      sprintf(message, "glCopyTexImage%dD(width)", dimensions);
894      gl_error(ctx, GL_INVALID_VALUE, message);
895      return GL_TRUE;
896   }
897
898   /* Height */
899   if (dimensions >= 2) {
900      if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
901          || logbase2( height - 2 * border ) < 0) {
902         char message[100];
903         sprintf(message, "glCopyTexImage%dD(height)", dimensions);
904         gl_error(ctx, GL_INVALID_VALUE, message);
905         return GL_TRUE;
906      }
907   }
908
909   /* Level */
910   if (level<0 || level>=ctx->Const.MaxTextureLevels) {
911      char message[100];
912      sprintf(message, "glCopyTexImage%dD(level)", dimensions);
913      gl_error(ctx, GL_INVALID_VALUE, message);
914      return GL_TRUE;
915   }
916
917   iformat = decode_internal_format( internalFormat );
918   if (iformat < 0) {
919      char message[100];
920      sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
921      gl_error(ctx, GL_INVALID_VALUE, message);
922      return GL_TRUE;
923   }
924
925   /* if we get here, the parameters are OK */
926   return GL_FALSE;
927}
928
929
930static GLboolean
931copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
932                             GLenum target, GLint level,
933                             GLint xoffset, GLint yoffset, GLint zoffset,
934                             GLsizei width, GLsizei height )
935{
936   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
937   struct gl_texture_image *teximage;
938
939   if (dimensions == 1 && target != GL_TEXTURE_1D) {
940      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
941      return GL_TRUE;
942   }
943   else if (dimensions == 2 && target != GL_TEXTURE_2D) {
944      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
945      return GL_TRUE;
946   }
947   else if (dimensions == 3 && target != GL_TEXTURE_3D) {
948      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
949      return GL_TRUE;
950   }
951
952   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
953      char message[100];
954      sprintf(message, "glCopyTexSubImage%dD(level)", dimensions);
955      gl_error(ctx, GL_INVALID_VALUE, message);
956      return GL_TRUE;
957   }
958
959   if (width < 0) {
960      char message[100];
961      sprintf(message, "glCopyTexSubImage%dD(width)", dimensions );
962      gl_error(ctx, GL_INVALID_VALUE, message);
963      return GL_TRUE;
964   }
965   if (dimensions > 1 && height < 0) {
966      char message[100];
967      sprintf(message, "glCopyTexSubImage%dD(height)", dimensions );
968      gl_error(ctx, GL_INVALID_VALUE, message);
969      return GL_TRUE;
970   }
971
972   teximage = texUnit->CurrentD[dimensions]->Image[level];
973   if (!teximage) {
974      char message[100];
975      sprintf(message, "glCopyTexSubImage%dD(undefined texture)", dimensions);
976      gl_error(ctx, GL_INVALID_OPERATION, message);
977      return GL_TRUE;
978   }
979
980   if (xoffset < -((GLint)teximage->Border)) {
981      char message[100];
982      sprintf(message, "glCopyTexSubImage%dD(xoffset)", dimensions);
983      gl_error(ctx, GL_INVALID_VALUE, message);
984      return GL_TRUE;
985   }
986   if (xoffset+width > (GLint) (teximage->Width+teximage->Border)) {
987      char message[100];
988      sprintf(message, "glCopyTexSubImage%dD(xoffset+width)", dimensions);
989      gl_error(ctx, GL_INVALID_VALUE, message);
990      return GL_TRUE;
991   }
992   if (dimensions > 1) {
993      if (yoffset < -((GLint)teximage->Border)) {
994         char message[100];
995         sprintf(message, "glCopyTexSubImage%dD(yoffset)", dimensions);
996         gl_error(ctx, GL_INVALID_VALUE, message);
997         return GL_TRUE;
998      }
999      /* NOTE: we're adding the border here, not subtracting! */
1000      if (yoffset+height > (GLint) (teximage->Height+teximage->Border)) {
1001         char message[100];
1002         sprintf(message, "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1003         gl_error(ctx, GL_INVALID_VALUE, message);
1004         return GL_TRUE;
1005      }
1006   }
1007
1008   if (dimensions > 2) {
1009      if (zoffset < -((GLint)teximage->Border)) {
1010         char message[100];
1011         sprintf(message, "glCopyTexSubImage%dD(zoffset)", dimensions);
1012         gl_error(ctx, GL_INVALID_VALUE, message);
1013         return GL_TRUE;
1014      }
1015      if (zoffset > (GLint) (teximage->Depth+teximage->Border)) {
1016         char message[100];
1017         sprintf(message, "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1018         gl_error(ctx, GL_INVALID_VALUE, message);
1019         return GL_TRUE;
1020      }
1021   }
1022
1023   /* if we get here, the parameters are OK */
1024   return GL_FALSE;
1025}
1026
1027
1028
1029
1030/*
1031 * Called from the API.  Note that width includes the border.
1032 */
1033void
1034_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1035                  GLsizei width, GLint border, GLenum format,
1036                  GLenum type, const GLvoid *pixels )
1037{
1038   GET_CURRENT_CONTEXT(ctx);
1039   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage1D");
1040
1041   if (target==GL_TEXTURE_1D) {
1042      struct gl_texture_unit *texUnit;
1043      struct gl_texture_image *teximage;
1044
1045      if (texture_error_check( ctx, target, level, internalFormat,
1046                               format, type, 1, width, 1, 1, border )) {
1047         return;   /* error in texture image was detected */
1048      }
1049
1050      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1051
1052      /* free current texture image, if any */
1053      if (texUnit->CurrentD[1]->Image[level]) {
1054         gl_free_texture_image( texUnit->CurrentD[1]->Image[level] );
1055      }
1056
1057      teximage = new_texture_image(width, 1, 1, border, internalFormat);
1058
1059      /* make new texture from source image */
1060      if (pixels) {
1061         make_texture_image(ctx, teximage, format, type, pixels, &ctx->Unpack);
1062      }
1063      else {
1064         make_null_texture(teximage);
1065      }
1066
1067      /* install new texture image */
1068      texUnit->CurrentD[1]->Image[level] = teximage;
1069      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
1070      ctx->NewState |= NEW_TEXTURING;
1071
1072      /* tell driver about change */
1073      if (ctx->Driver.TexImage) {
1074         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
1075                                  texUnit->CurrentD[1],
1076                                  level, internalFormat, teximage );
1077      }
1078   }
1079   else if (target==GL_PROXY_TEXTURE_1D) {
1080      /* Proxy texture: check for errors and update proxy state */
1081      if (texture_error_check( ctx, target, level, internalFormat,
1082                               format, type, 1, width, 1, 1, border )) {
1083         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1084            MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
1085                    sizeof(struct gl_texture_image) );
1086         }
1087      }
1088      else {
1089         ctx->Texture.Proxy1D->Image[level]->Format = (GLenum) format;
1090         set_teximage_component_sizes( ctx->Texture.Proxy1D->Image[level] );
1091         ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalFormat;
1092         ctx->Texture.Proxy1D->Image[level]->Border = border;
1093         ctx->Texture.Proxy1D->Image[level]->Width = width;
1094         ctx->Texture.Proxy1D->Image[level]->Height = 1;
1095         ctx->Texture.Proxy1D->Image[level]->Depth = 1;
1096      }
1097   }
1098   else {
1099      gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1100      return;
1101   }
1102}
1103
1104
1105void
1106_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
1107                  GLsizei width, GLsizei height, GLint border,
1108                  GLenum format, GLenum type,
1109                  const GLvoid *pixels )
1110{
1111   GET_CURRENT_CONTEXT(ctx);
1112   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D");
1113
1114   if (target==GL_TEXTURE_2D) {
1115      struct gl_texture_unit *texUnit;
1116      struct gl_texture_image *teximage;
1117
1118      if (texture_error_check( ctx, target, level, internalFormat,
1119                               format, type, 2, width, height, 1, border )) {
1120         return;   /* error in texture image was detected */
1121      }
1122
1123      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1124
1125      /* free current texture image, if any */
1126      if (texUnit->CurrentD[2]->Image[level]) {
1127         gl_free_texture_image( texUnit->CurrentD[2]->Image[level] );
1128      }
1129
1130      teximage = new_texture_image(width, height, 1, border,internalFormat);
1131
1132      /* make new texture from source image */
1133      if (pixels) {
1134         make_texture_image(ctx, teximage, format, type, pixels, &ctx->Unpack);
1135      }
1136      else {
1137         make_null_texture(teximage);
1138      }
1139
1140      /* install new texture image */
1141      texUnit->CurrentD[2]->Image[level] = teximage;
1142      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[2] );
1143      ctx->NewState |= NEW_TEXTURING;
1144
1145      /* tell driver about change */
1146      if (ctx->Driver.TexImage) {
1147         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
1148                                  texUnit->CurrentD[2],
1149                                  level, internalFormat, teximage );
1150      }
1151   }
1152   else if (target==GL_PROXY_TEXTURE_2D) {
1153      /* Proxy texture: check for errors and update proxy state */
1154      if (texture_error_check( ctx, target, level, internalFormat,
1155                               format, type, 2, width, height, 1, border )) {
1156         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1157            MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
1158                    sizeof(struct gl_texture_image) );
1159         }
1160      }
1161      else {
1162         ctx->Texture.Proxy2D->Image[level]->Format = (GLenum) format;
1163         set_teximage_component_sizes( ctx->Texture.Proxy2D->Image[level] );
1164         ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalFormat;
1165         ctx->Texture.Proxy2D->Image[level]->Border = border;
1166         ctx->Texture.Proxy2D->Image[level]->Width = width;
1167         ctx->Texture.Proxy2D->Image[level]->Height = height;
1168         ctx->Texture.Proxy2D->Image[level]->Depth = 1;
1169      }
1170   }
1171   else {
1172      gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
1173      return;
1174   }
1175}
1176
1177
1178
1179/*
1180 * Called by the API or display list executor.
1181 * Note that width and height include the border.
1182 */
1183void
1184_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
1185                  GLsizei width, GLsizei height, GLsizei depth,
1186                  GLint border, GLenum format, GLenum type,
1187                  const GLvoid *pixels )
1188{
1189   GET_CURRENT_CONTEXT(ctx);
1190   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3D");
1191
1192   if (target==GL_TEXTURE_3D_EXT) {
1193      struct gl_texture_unit *texUnit;
1194      struct gl_texture_image *teximage;
1195      if (texture_error_check( ctx, target, level, internalFormat,
1196                               format, type, 3, width, height, depth,
1197                               border )) {
1198         return;   /* error in texture image was detected */
1199      }
1200
1201      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1202
1203      /* free current texture image, if any */
1204      if (texUnit->CurrentD[3]->Image[level]) {
1205         gl_free_texture_image( texUnit->CurrentD[3]->Image[level] );
1206      }
1207
1208      teximage = new_texture_image(width, height, depth,
1209                                   border, internalFormat);
1210
1211      /* make new texture from source image */
1212      if (pixels) {
1213         make_texture_image(ctx, teximage, format, type, pixels, &ctx->Unpack);
1214      }
1215      else {
1216         make_null_texture(teximage);
1217      }
1218
1219      /* install new texture image */
1220      texUnit->CurrentD[3]->Image[level] = teximage;
1221      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[3] );
1222      ctx->NewState |= NEW_TEXTURING;
1223
1224      /* tell driver about change */
1225      if (ctx->Driver.TexImage) {
1226         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D_EXT,
1227                                  texUnit->CurrentD[3],
1228                                  level, internalFormat, teximage );
1229      }
1230   }
1231   else if (target==GL_PROXY_TEXTURE_3D_EXT) {
1232      /* Proxy texture: check for errors and update proxy state */
1233      if (texture_error_check( ctx, target, level, internalFormat,
1234                               format, type, 3, width, height, depth,
1235                               border )) {
1236         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1237            MEMSET( ctx->Texture.Proxy3D->Image[level], 0,
1238                    sizeof(struct gl_texture_image) );
1239         }
1240      }
1241      else {
1242         ctx->Texture.Proxy3D->Image[level]->Format = (GLenum) format;
1243         set_teximage_component_sizes( ctx->Texture.Proxy3D->Image[level] );
1244         ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalFormat;
1245         ctx->Texture.Proxy3D->Image[level]->Border = border;
1246         ctx->Texture.Proxy3D->Image[level]->Width = width;
1247         ctx->Texture.Proxy3D->Image[level]->Height = height;
1248         ctx->Texture.Proxy3D->Image[level]->Depth  = depth;
1249      }
1250   }
1251   else {
1252      gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
1253      return;
1254   }
1255}
1256
1257
1258void
1259_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
1260                     GLsizei width, GLsizei height, GLsizei depth,
1261                     GLint border, GLenum format, GLenum type,
1262                     const GLvoid *pixels )
1263{
1264   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
1265                    depth, border, format, type, pixels);
1266}
1267
1268
1269/*
1270 * Fetch a texture image from the device driver.
1271 * Store the results in the given texture object at the given mipmap level.
1272 */
1273static void
1274get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
1275                          const struct gl_texture_object *texObj )
1276{
1277   GLvoid *image;
1278   GLenum imgFormat, imgType;
1279   GLboolean freeImage;
1280   struct gl_texture_image *texImage;
1281   GLint destComponents, numPixels, srcBytesPerTexel;
1282
1283   if (!ctx->Driver.GetTexImage)
1284      return;
1285
1286   image = (*ctx->Driver.GetTexImage)( ctx, target, level,
1287                                       &imgFormat, &imgType, &freeImage);
1288   if (!image)
1289      return;
1290
1291   texImage = texObj->Image[level];
1292   ASSERT(texImage);
1293   if (!texImage)
1294      return;
1295
1296   destComponents = components_in_intformat(texImage->Format);
1297   assert(destComponents > 0);
1298   numPixels = texImage->Width * texImage->Height * texImage->Depth;
1299   assert(numPixels > 0);
1300   srcBytesPerTexel = _mesa_bytes_per_pixel(imgFormat, imgType);
1301   assert(srcBytesPerTexel > 0);
1302
1303   if (!texImage->Data) {
1304      /* Allocate memory for the texture image data */
1305      texImage->Data = (GLubyte *) MALLOC(numPixels * destComponents + EXTRA_BYTE);
1306   }
1307
1308   if (imgFormat == texImage->Format && imgType == GL_UNSIGNED_BYTE) {
1309      /* We got lucky!  The driver's format and type match Mesa's format. */
1310      if (texImage->Data) {
1311         MEMCPY(texImage->Data, image, numPixels * destComponents);
1312      }
1313   }
1314   else {
1315      /* Convert the texture image from the driver's format to Mesa's
1316       * internal format.
1317       */
1318      const GLint width = texImage->Width;
1319      const GLint height = texImage->Height;
1320      const GLint depth = texImage->Depth;
1321      const GLint destBytesPerRow = width * destComponents * sizeof(GLchan);
1322      const GLint srcBytesPerRow = width * srcBytesPerTexel;
1323      const GLenum dstType = GL_UNSIGNED_BYTE;
1324      const GLenum dstFormat = texImage->Format;
1325      const GLubyte *srcPtr = (const GLubyte *) image;
1326      GLubyte *destPtr = texImage->Data;
1327
1328      if (texImage->Format == GL_COLOR_INDEX) {
1329         /* color index texture */
1330         GLint img, row;
1331         assert(imgFormat == GL_COLOR_INDEX);
1332         for (img = 0; img < depth; img++) {
1333            for (row = 0; row < height; row++) {
1334               _mesa_unpack_index_span(ctx, width, dstType, destPtr,
1335                                   imgType, srcPtr, &DefaultPacking, GL_FALSE);
1336               destPtr += destBytesPerRow;
1337               srcPtr += srcBytesPerRow;
1338            }
1339         }
1340      }
1341      else {
1342         /* color texture */
1343         GLint img, row;
1344         for (img = 0; img < depth; img++) {
1345            for (row = 0; row < height; row++) {
1346               _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, destPtr,
1347                        imgFormat, imgType, srcPtr, &DefaultPacking, GL_FALSE);
1348               destPtr += destBytesPerRow;
1349               srcPtr += srcBytesPerRow;
1350            }
1351         }
1352      }
1353   }
1354
1355   if (freeImage)
1356      FREE(image);
1357}
1358
1359
1360void
1361_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1362                   GLenum type, GLvoid *pixels )
1363{
1364   GET_CURRENT_CONTEXT(ctx);
1365   const struct gl_texture_object *texObj;
1366   struct gl_texture_image *texImage;
1367   GLboolean discardImage;
1368
1369   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetTexImage");
1370
1371   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1372      gl_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1373      return;
1374   }
1375
1376   if (_mesa_sizeof_type(type) <= 0) {
1377      gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1378      return;
1379   }
1380
1381   if (_mesa_components_in_format(format) <= 0) {
1382      gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1383      return;
1384   }
1385
1386   if (!pixels)
1387      return;
1388
1389   switch (target) {
1390      case GL_TEXTURE_1D:
1391         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[1];
1392         break;
1393      case GL_TEXTURE_2D:
1394         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[2];
1395         break;
1396      case GL_TEXTURE_3D:
1397         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[3];
1398         break;
1399      default:
1400         gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(target)" );
1401         return;
1402   }
1403
1404   texImage = texObj->Image[level];
1405   if (!texImage) {
1406      /* invalid mipmap level */
1407      return;
1408   }
1409
1410   if (!texImage->Data) {
1411      /* try to get the texture image from the device driver */
1412      get_teximage_from_driver(ctx, target, level, texObj);
1413      discardImage = GL_TRUE;
1414   }
1415   else {
1416      discardImage = GL_FALSE;
1417   }
1418
1419   if (texImage->Data) {
1420      GLint width = texImage->Width;
1421      GLint height = texImage->Height;
1422      GLint row;
1423
1424      for (row = 0; row < height; row++) {
1425         /* compute destination address in client memory */
1426         GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels,
1427                                                width, height,
1428                                                format, type, 0, row, 0);
1429
1430         assert(dest);
1431         if (texImage->Format == GL_RGBA) {
1432            const GLubyte *src = texImage->Data + row * width * 4 * sizeof(GLubyte);
1433            _mesa_pack_rgba_span( ctx, width, (CONST GLubyte (*)[4]) src,
1434                                  format, type, dest, &ctx->Pack, GL_TRUE );
1435         }
1436         else {
1437            /* fetch RGBA row from texture image then pack it in client mem */
1438            GLubyte rgba[MAX_WIDTH][4];
1439            GLint i;
1440            const GLubyte *src;
1441            switch (texImage->Format) {
1442               case GL_ALPHA:
1443                  src = texImage->Data + row * width * sizeof(GLubyte);
1444                  for (i = 0; i < width; i++) {
1445                     rgba[i][RCOMP] = 255;
1446                     rgba[i][GCOMP] = 255;
1447                     rgba[i][BCOMP] = 255;
1448                     rgba[i][ACOMP] = src[i];
1449                  }
1450                  break;
1451               case GL_LUMINANCE:
1452                  src = texImage->Data + row * width * sizeof(GLubyte);
1453                  for (i = 0; i < width; i++) {
1454                     rgba[i][RCOMP] = src[i];
1455                     rgba[i][GCOMP] = src[i];
1456                     rgba[i][BCOMP] = src[i];
1457                     rgba[i][ACOMP] = 255;
1458                   }
1459                  break;
1460               case GL_LUMINANCE_ALPHA:
1461                  src = texImage->Data + row * 2 * width * sizeof(GLubyte);
1462                  for (i = 0; i < width; i++) {
1463                     rgba[i][RCOMP] = src[i*2+0];
1464                     rgba[i][GCOMP] = src[i*2+0];
1465                     rgba[i][BCOMP] = src[i*2+0];
1466                     rgba[i][ACOMP] = src[i*2+1];
1467                  }
1468                  break;
1469               case GL_INTENSITY:
1470                  src = texImage->Data + row * width * sizeof(GLubyte);
1471                  for (i = 0; i < width; i++) {
1472                     rgba[i][RCOMP] = src[i];
1473                     rgba[i][GCOMP] = src[i];
1474                     rgba[i][BCOMP] = src[i];
1475                     rgba[i][ACOMP] = 255;
1476                  }
1477                  break;
1478               case GL_RGB:
1479                  src = texImage->Data + row * 3 * width * sizeof(GLubyte);
1480                  for (i = 0; i < width; i++) {
1481                     rgba[i][RCOMP] = src[i*3+0];
1482                     rgba[i][GCOMP] = src[i*3+1];
1483                     rgba[i][BCOMP] = src[i*3+2];
1484                     rgba[i][ACOMP] = 255;
1485                  }
1486                  break;
1487               case GL_RGBA:
1488                  /* this special case should have been handled above! */
1489                  gl_problem( ctx, "error 1 in gl_GetTexImage" );
1490                  break;
1491               case GL_COLOR_INDEX:
1492                  gl_problem( ctx, "GL_COLOR_INDEX not implemented in gl_GetTexImage" );
1493                  break;
1494               default:
1495                  gl_problem( ctx, "bad format in gl_GetTexImage" );
1496            }
1497            _mesa_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba,
1498                                  format, type, dest, &ctx->Pack, GL_TRUE );
1499         }
1500      }
1501
1502      /* if we got the teximage from the device driver we'll discard it now */
1503      if (discardImage) {
1504         FREE(texImage->Data);
1505         texImage->Data = NULL;
1506      }
1507   }
1508}
1509
1510
1511
1512void
1513_mesa_TexSubImage1D( GLenum target, GLint level,
1514                     GLint xoffset, GLsizei width,
1515                     GLenum format, GLenum type,
1516                     const GLvoid *pixels )
1517{
1518   GET_CURRENT_CONTEXT(ctx);
1519   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1520   struct gl_texture_image *destTex;
1521
1522   if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
1523                              width, 1, 1, format, type)) {
1524      return;   /* error was detected */
1525   }
1526
1527   destTex = texUnit->CurrentD[1]->Image[level];
1528   assert(destTex);
1529
1530   if (width == 0 || !pixels)
1531      return;  /* no-op, not an error */
1532
1533
1534   /*
1535    * Replace the texture subimage
1536    */
1537   {
1538      const GLint texComponents = components_in_intformat(destTex->Format);
1539      const GLenum texFormat = destTex->Format;
1540      const GLint xoffsetb = xoffset + destTex->Border;
1541      GLubyte *dst = destTex->Data + xoffsetb * texComponents;
1542      if (texFormat == GL_COLOR_INDEX) {
1543         /* color index texture */
1544         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1545                                  width, 1, format, type, 0, 0, 0);
1546         _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
1547                                    type, src, &ctx->Unpack, GL_TRUE);
1548      }
1549      else {
1550         /* color texture */
1551         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1552                                  width, 1, format, type, 0, 0, 0);
1553         _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
1554                              format, type, src, &ctx->Unpack, GL_TRUE);
1555      }
1556   }
1557
1558   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
1559
1560   /*
1561    * Inform device driver of texture image change.
1562    */
1563   if (ctx->Driver.TexSubImage) {
1564      (*ctx->Driver.TexSubImage)(ctx, GL_TEXTURE_1D, texUnit->CurrentD[1],
1565                                 level, xoffset, 0, width, 1,
1566                                 texUnit->CurrentD[1]->Image[level]->IntFormat,
1567                                 destTex );
1568   }
1569   else {
1570      if (ctx->Driver.TexImage) {
1571         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_1D, texUnit->CurrentD[1],
1572                                 level,
1573                                 texUnit->CurrentD[1]->Image[level]->IntFormat,
1574                                 destTex );
1575      }
1576   }
1577}
1578
1579
1580void
1581_mesa_TexSubImage2D( GLenum target, GLint level,
1582                     GLint xoffset, GLint yoffset,
1583                     GLsizei width, GLsizei height,
1584                     GLenum format, GLenum type,
1585                     const GLvoid *pixels )
1586{
1587   GET_CURRENT_CONTEXT(ctx);
1588   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1589   struct gl_texture_image *destTex;
1590
1591   if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
1592                              width, height, 1, format, type)) {
1593      return;   /* error was detected */
1594   }
1595
1596   destTex = texUnit->CurrentD[2]->Image[level];
1597   assert(destTex);
1598
1599   if (width == 0 || height == 0 || !pixels)
1600      return;  /* no-op, not an error */
1601
1602
1603   /*
1604    * Replace the texture subimage
1605    */
1606   {
1607      const GLint texComponents = components_in_intformat(destTex->Format);
1608      const GLenum texFormat = destTex->Format;
1609      const GLint xoffsetb = xoffset + destTex->Border;
1610      const GLint yoffsetb = yoffset + destTex->Border;
1611      GLubyte *dst = destTex->Data
1612                   + (yoffsetb * destTex->Width + xoffsetb) * texComponents;
1613      if (texFormat == GL_COLOR_INDEX) {
1614         /* color index texture */
1615         const GLint stride = destTex->Width * sizeof(GLubyte);
1616         GLint row;
1617         for (row = 0; row < height; row++) {
1618            const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1619                                     width, height, format, type, 0, row, 0);
1620            _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
1621                                    type, src, &ctx->Unpack, GL_TRUE);
1622            dst += stride;
1623         }
1624      }
1625      else {
1626         /* color texture */
1627         const GLint stride = destTex->Width * texComponents * sizeof(GLubyte);
1628         GLint row;
1629         for (row = 0; row < height; row++) {
1630            const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1631                                     width, height, format, type, 0, row, 0);
1632            _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
1633                                 format, type, src, &ctx->Unpack, GL_TRUE);
1634            dst += stride;
1635         }
1636      }
1637   }
1638
1639   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[2] );
1640
1641   /*
1642    * Inform device driver of texture image change.
1643    */
1644   if (ctx->Driver.TexSubImage) {
1645      (*ctx->Driver.TexSubImage)(ctx, GL_TEXTURE_2D, texUnit->CurrentD[2],
1646                                 level, xoffset, yoffset, width, height,
1647                                 texUnit->CurrentD[2]->Image[level]->IntFormat,
1648                                 destTex );
1649   }
1650   else {
1651      if (ctx->Driver.TexImage) {
1652         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texUnit->CurrentD[2],
1653                                 level,
1654                                 texUnit->CurrentD[2]->Image[level]->IntFormat,
1655                                 destTex );
1656      }
1657   }
1658}
1659
1660
1661
1662void
1663_mesa_TexSubImage3D( GLenum target, GLint level,
1664                     GLint xoffset, GLint yoffset, GLint zoffset,
1665                     GLsizei width, GLsizei height, GLsizei depth,
1666                     GLenum format, GLenum type,
1667                     const GLvoid *pixels )
1668{
1669   GET_CURRENT_CONTEXT(ctx);
1670   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1671   struct gl_texture_image *destTex;
1672
1673   if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
1674                              width, height, depth, format, type)) {
1675      return;   /* error was detected */
1676   }
1677
1678   destTex = texUnit->CurrentD[3]->Image[level];
1679   assert(destTex);
1680
1681   if (width == 0 || height == 0 || height == 0 || !pixels)
1682      return;  /* no-op, not an error */
1683
1684   /*
1685    * Replace the texture subimage
1686    */
1687   {
1688      const GLint texComponents = components_in_intformat(destTex->Format);
1689      const GLenum texFormat = destTex->Format;
1690      const GLint xoffsetb = xoffset + destTex->Border;
1691      const GLint yoffsetb = yoffset + destTex->Border;
1692      const GLint zoffsetb = zoffset + destTex->Border;
1693      GLint dstRectArea = destTex->Width * destTex->Height;
1694      GLubyte *dst = destTex->Data
1695            + (zoffsetb * dstRectArea +  yoffsetb * destTex->Width + xoffsetb)
1696            * texComponents;
1697
1698      if (texFormat == GL_COLOR_INDEX) {
1699         /* color index texture */
1700         const GLint stride = destTex->Width * sizeof(GLubyte);
1701         GLint img, row;
1702         for (img = 0; img < depth; img++) {
1703            for (row = 0; row < height; row++) {
1704               const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1705                                    width, height, format, type, img, row, 0);
1706               _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
1707                                       type, src, &ctx->Unpack, GL_TRUE);
1708               dst += stride;
1709            }
1710         }
1711      }
1712      else {
1713         /* color texture */
1714         const GLint stride = destTex->Width * texComponents * sizeof(GLubyte);
1715         GLint img, row;
1716         for (img = 0; img < depth; img++) {
1717            for (row = 0; row < height; row++) {
1718               const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels,
1719                                     width, height, format, type, img, row, 0);
1720               _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
1721                                    format, type, src, &ctx->Unpack, GL_TRUE);
1722               dst += stride;
1723            }
1724         }
1725      }
1726   }
1727
1728   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
1729
1730   /*
1731    * Inform device driver of texture image change.
1732    */
1733   /* XXX todo */
1734}
1735
1736
1737
1738/*
1739 * Read an RGBA image from the frame buffer.
1740 * This is used by glCopyTexSubImage[12]D().
1741 * Input:  ctx - the context
1742 *         x, y - lower left corner
1743 *         width, height - size of region to read
1744 * Return: pointer to block of GL_RGBA, GLubyte data.
1745 */
1746static GLubyte *
1747read_color_image( GLcontext *ctx, GLint x, GLint y,
1748                  GLsizei width, GLsizei height )
1749{
1750   GLint stride, i;
1751   GLubyte *image, *dst;
1752
1753   image = (GLubyte *) MALLOC(width * height * 4 * sizeof(GLubyte));
1754   if (!image)
1755      return NULL;
1756
1757   /* Select buffer to read from */
1758   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
1759                                 ctx->Pixel.DriverReadBuffer );
1760
1761   dst = image;
1762   stride = width * 4 * sizeof(GLubyte);
1763   for (i = 0; i < height; i++) {
1764      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
1765                         (GLubyte (*)[4]) dst );
1766      dst += stride;
1767   }
1768
1769   /* Read from draw buffer (the default) */
1770   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
1771                                 ctx->Color.DriverDrawBuffer );
1772
1773   return image;
1774}
1775
1776
1777
1778void
1779_mesa_CopyTexImage1D( GLenum target, GLint level,
1780                      GLenum internalFormat,
1781                      GLint x, GLint y,
1782                      GLsizei width, GLint border )
1783{
1784   GET_CURRENT_CONTEXT(ctx);
1785   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage1D");
1786
1787   if (copytexture_error_check(ctx, 1, target, level, internalFormat,
1788                               width, 1, border))
1789      return;
1790
1791   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1792       || !ctx->Driver.CopyTexImage1D
1793       || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
1794                         internalFormat, x, y, width, border))
1795   {
1796      GLubyte *image  = read_color_image( ctx, x, y, width, 1 );
1797      if (!image) {
1798         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
1799         return;
1800      }
1801      (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
1802                                border, GL_RGBA, GL_UNSIGNED_BYTE, image );
1803      FREE(image);
1804   }
1805}
1806
1807
1808
1809void
1810_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
1811                      GLint x, GLint y, GLsizei width, GLsizei height,
1812                      GLint border )
1813{
1814   GET_CURRENT_CONTEXT(ctx);
1815   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage2D");
1816
1817   if (copytexture_error_check(ctx, 2, target, level, internalFormat,
1818                               width, height, border))
1819      return;
1820
1821   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1822       || !ctx->Driver.CopyTexImage2D
1823       || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
1824                         internalFormat, x, y, width, height, border))
1825   {
1826      GLubyte *image  = read_color_image( ctx, x, y, width, height );
1827      if (!image) {
1828         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
1829         return;
1830      }
1831      (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
1832                      height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
1833      FREE(image);
1834   }
1835}
1836
1837
1838
1839/*
1840 * Do the work of glCopyTexSubImage[123]D.
1841 */
1842static void
1843copy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
1844                    GLint width, GLint height,
1845                    GLint srcx, GLint srcy,
1846                    GLint dstx, GLint dsty, GLint dstz )
1847{
1848   static struct gl_pixelstore_attrib packing = {
1849      1,            /* Alignment */
1850      0,            /* RowLength */
1851      0,            /* SkipPixels */
1852      0,            /* SkipRows */
1853      0,            /* ImageHeight */
1854      0,            /* SkipImages */
1855      GL_FALSE,     /* SwapBytes */
1856      GL_FALSE      /* LsbFirst */
1857   };
1858
1859   GLint i;
1860   GLint format, components, rectarea;
1861   GLint texwidth, texheight, zoffset;
1862
1863   /* dst[xyz] may be negative if we have a texture border! */
1864   dstx += dest->Border;
1865   dsty += dest->Border;
1866   dstz += dest->Border;
1867   texwidth = dest->Width;
1868   texheight = dest->Height;
1869   rectarea = texwidth * texheight;
1870   zoffset = dstz * rectarea;
1871   format = dest->Format;
1872   components = components_in_intformat( format );
1873
1874   /* Select buffer to read from */
1875   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
1876                                 ctx->Pixel.DriverReadBuffer );
1877
1878   for (i = 0;i < height; i++) {
1879      GLubyte rgba[MAX_WIDTH][4];
1880      GLubyte *dst;
1881      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, srcy + i, rgba );
1882      dst = dest->Data + ( zoffset + (dsty+i) * texwidth + dstx) * components;
1883      _mesa_unpack_ubyte_color_span(ctx, width, format, dst,
1884                                    GL_RGBA, GL_UNSIGNED_BYTE, rgba,
1885                                    &packing, GL_TRUE);
1886   }
1887
1888   /* Read from draw buffer (the default) */
1889   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
1890                                 ctx->Color.DriverDrawBuffer );
1891}
1892
1893
1894
1895
1896void
1897_mesa_CopyTexSubImage1D( GLenum target, GLint level,
1898                         GLint xoffset, GLint x, GLint y, GLsizei width )
1899{
1900   GET_CURRENT_CONTEXT(ctx);
1901   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage1D");
1902
1903   if (copytexsubimage_error_check(ctx, 1, target, level,
1904                                   xoffset, 0, 0, width, 1))
1905      return;
1906
1907   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1908       || !ctx->Driver.CopyTexSubImage1D
1909       || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
1910                                            xoffset, x, y, width)) {
1911      struct gl_texture_unit *texUnit;
1912      struct gl_texture_image *teximage;
1913      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1914      teximage = texUnit->CurrentD[1]->Image[level];
1915      assert(teximage);
1916      if (teximage->Data) {
1917         copy_tex_sub_image(ctx, teximage, width, 1, x, y, xoffset, 0, 0);
1918         /* tell driver about the change */
1919         if (ctx->Driver.TexImage) {
1920            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
1921                                     texUnit->CurrentD[1],
1922                                     level, teximage->IntFormat, teximage );
1923         }
1924      }
1925   }
1926}
1927
1928
1929
1930void
1931_mesa_CopyTexSubImage2D( GLenum target, GLint level,
1932                         GLint xoffset, GLint yoffset,
1933                         GLint x, GLint y, GLsizei width, GLsizei height )
1934{
1935   GET_CURRENT_CONTEXT(ctx);
1936   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage2D");
1937
1938   if (copytexsubimage_error_check(ctx, 2, target, level,
1939                                   xoffset, yoffset, 0, width, height))
1940      return;
1941
1942   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1943       || !ctx->Driver.CopyTexSubImage2D
1944       || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
1945                                xoffset, yoffset, x, y, width, height )) {
1946      struct gl_texture_unit *texUnit;
1947      struct gl_texture_image *teximage;
1948      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1949      teximage = texUnit->CurrentD[2]->Image[level];
1950      assert(teximage);
1951      if (teximage->Data) {
1952         copy_tex_sub_image(ctx, teximage, width, height,
1953                            x, y, xoffset, yoffset, 0);
1954         /* tell driver about the change */
1955         if (ctx->Driver.TexImage) {
1956            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
1957                                     texUnit->CurrentD[2],
1958                                     level, teximage->IntFormat, teximage );
1959	 }
1960      }
1961   }
1962}
1963
1964
1965
1966void
1967_mesa_CopyTexSubImage3D( GLenum target, GLint level,
1968                         GLint xoffset, GLint yoffset, GLint zoffset,
1969                         GLint x, GLint y, GLsizei width, GLsizei height )
1970{
1971   GET_CURRENT_CONTEXT(ctx);
1972   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage3D");
1973
1974   if (copytexsubimage_error_check(ctx, 3, target, level,
1975                    xoffset, yoffset, zoffset, width, height))
1976      return;
1977
1978    if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1979       || !ctx->Driver.CopyTexSubImage3D
1980       || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
1981                        xoffset, yoffset, zoffset, x, y, width, height )) {
1982       struct gl_texture_unit *texUnit;
1983       struct gl_texture_image *teximage;
1984       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1985       teximage = texUnit->CurrentD[3]->Image[level];
1986       assert(teximage);
1987       if (teximage->Data) {
1988          copy_tex_sub_image(ctx, teximage, width, height,
1989                            x, y, xoffset, yoffset, zoffset);
1990          /* tell driver about the change */
1991          if (ctx->Driver.TexImage) {
1992             (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D,
1993                                      texUnit->CurrentD[3],
1994                                      level, teximage->IntFormat, teximage );
1995         }
1996      }
1997   }
1998}
1999