xref: /third_party/mesa3d/src/mesa/main/image.c (revision bf215546)
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5 * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/**
28 * \file image.c
29 * Image handling.
30 */
31
32
33#include "glheader.h"
34#include "colormac.h"
35#include "glformats.h"
36#include "image.h"
37
38#include "macros.h"
39#include "mtypes.h"
40
41
42
43/**
44 * Flip the order of the 2 bytes in each word in the given array (src) and
45 * store the result in another array (dst). For in-place byte-swapping this
46 * function can be called with the same array for src and dst.
47 *
48 * \param dst the array where byte-swapped data will be stored.
49 * \param src the array with the source data we want to byte-swap.
50 * \param n number of words.
51 */
52static void
53swap2_copy( GLushort *dst, GLushort *src, GLuint n )
54{
55   GLuint i;
56   for (i = 0; i < n; i++) {
57      dst[i] = (src[i] >> 8) | ((src[i] << 8) & 0xff00);
58   }
59}
60
61void
62_mesa_swap2(GLushort *p, GLuint n)
63{
64   swap2_copy(p, p, n);
65}
66
67/*
68 * Flip the order of the 4 bytes in each word in the given array (src) and
69 * store the result in another array (dst). For in-place byte-swapping this
70 * function can be called with the same array for src and dst.
71 *
72 * \param dst the array where byte-swapped data will be stored.
73 * \param src the array with the source data we want to byte-swap.
74 * \param n number of words.
75 */
76static void
77swap4_copy( GLuint *dst, GLuint *src, GLuint n )
78{
79   GLuint i, a, b;
80   for (i = 0; i < n; i++) {
81      b = src[i];
82      a =  (b >> 24)
83	| ((b >> 8) & 0xff00)
84	| ((b << 8) & 0xff0000)
85	| ((b << 24) & 0xff000000);
86      dst[i] = a;
87   }
88}
89
90void
91_mesa_swap4(GLuint *p, GLuint n)
92{
93   swap4_copy(p, p, n);
94}
95
96/**
97 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
98 *
99 * Pixel unpacking/packing parameters are observed according to \p packing.
100 *
101 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
102 * \param packing  the pixelstore attributes
103 * \param width  the image width
104 * \param height  the image height
105 * \param format  the pixel format (must be validated beforehand)
106 * \param type  the pixel data type (must be validated beforehand)
107 * \param img  which image in the volume (0 for 1D or 2D images)
108 * \param row  row of pixel in the image (0 for 1D images)
109 * \param column column of pixel in the image
110 *
111 * \return offset of pixel.
112 *
113 * \sa gl_pixelstore_attrib.
114 */
115GLintptr
116_mesa_image_offset( GLuint dimensions,
117                    const struct gl_pixelstore_attrib *packing,
118                    GLsizei width, GLsizei height,
119                    GLenum format, GLenum type,
120                    GLint img, GLint row, GLint column )
121{
122   GLint alignment;        /* 1, 2 or 4 */
123   GLint pixels_per_row;
124   GLint rows_per_image;
125   GLint skiprows;
126   GLint skippixels;
127   GLint skipimages;       /* for 3-D volume images */
128   GLintptr offset;
129
130   assert(dimensions >= 1 && dimensions <= 3);
131
132   alignment = packing->Alignment;
133   if (packing->RowLength > 0) {
134      pixels_per_row = packing->RowLength;
135   }
136   else {
137      pixels_per_row = width;
138   }
139   if (packing->ImageHeight > 0) {
140      rows_per_image = packing->ImageHeight;
141   }
142   else {
143      rows_per_image = height;
144   }
145
146   skippixels = packing->SkipPixels;
147   /* Note: SKIP_ROWS _is_ used for 1D images */
148   skiprows = packing->SkipRows;
149   /* Note: SKIP_IMAGES is only used for 3D images */
150   skipimages = (dimensions == 3) ? packing->SkipImages : 0;
151
152   if (type == GL_BITMAP) {
153      /* BITMAP data */
154      GLintptr bytes_per_row;
155      GLintptr bytes_per_image;
156      /* components per pixel for color or stencil index: */
157      const GLint comp_per_pixel = 1;
158
159      /* The pixel type and format should have been error checked earlier */
160      assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
161
162      bytes_per_row = alignment
163                    * DIV_ROUND_UP( comp_per_pixel*pixels_per_row, 8*alignment );
164
165      bytes_per_image = bytes_per_row * rows_per_image;
166
167      offset = (skipimages + img) * bytes_per_image
168                 + (skiprows + row) * bytes_per_row
169                 + (skippixels + column) / 8;
170   }
171   else {
172      /* Non-BITMAP data */
173      GLintptr bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
174      GLintptr topOfImage;
175
176      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
177
178      /* The pixel type and format should have been error checked earlier */
179      assert(bytes_per_pixel > 0);
180
181      bytes_per_row = pixels_per_row * bytes_per_pixel;
182      remainder = bytes_per_row % alignment;
183      if (remainder > 0)
184         bytes_per_row += (alignment - remainder);
185
186      assert(bytes_per_row % alignment == 0);
187
188      bytes_per_image = bytes_per_row * rows_per_image;
189
190      if (packing->Invert) {
191         /* set pixel_addr to the last row */
192         topOfImage = bytes_per_row * (height - 1);
193         bytes_per_row = -bytes_per_row;
194      }
195      else {
196         topOfImage = 0;
197      }
198
199      /* compute final pixel address */
200      offset = (skipimages + img) * bytes_per_image
201                 + topOfImage
202                 + (skiprows + row) * bytes_per_row
203                 + (skippixels + column) * bytes_per_pixel;
204   }
205
206   return offset;
207}
208
209
210/**
211 * Return the address of a specific pixel in an image (1D, 2D or 3D).
212 *
213 * Pixel unpacking/packing parameters are observed according to \p packing.
214 *
215 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
216 * \param packing  the pixelstore attributes
217 * \param image  starting address of image data
218 * \param width  the image width
219 * \param height  the image height
220 * \param format  the pixel format (must be validated beforehand)
221 * \param type  the pixel data type (must be validated beforehand)
222 * \param img  which image in the volume (0 for 1D or 2D images)
223 * \param row  row of pixel in the image (0 for 1D images)
224 * \param column column of pixel in the image
225 *
226 * \return address of pixel.
227 *
228 * \sa gl_pixelstore_attrib.
229 */
230GLvoid *
231_mesa_image_address( GLuint dimensions,
232                     const struct gl_pixelstore_attrib *packing,
233                     const GLvoid *image,
234                     GLsizei width, GLsizei height,
235                     GLenum format, GLenum type,
236                     GLint img, GLint row, GLint column )
237{
238   const GLubyte *addr = (const GLubyte *) image;
239
240   addr += _mesa_image_offset(dimensions, packing, width, height,
241                              format, type, img, row, column);
242
243   return (GLvoid *) addr;
244}
245
246
247GLvoid *
248_mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
249                       const GLvoid *image,
250                       GLsizei width,
251                       GLenum format, GLenum type,
252                       GLint column )
253{
254   return _mesa_image_address(1, packing, image, width, 1,
255                              format, type, 0, 0, column);
256}
257
258
259GLvoid *
260_mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
261                       const GLvoid *image,
262                       GLsizei width, GLsizei height,
263                       GLenum format, GLenum type,
264                       GLint row, GLint column )
265{
266   return _mesa_image_address(2, packing, image, width, height,
267                              format, type, 0, row, column);
268}
269
270
271GLvoid *
272_mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
273                       const GLvoid *image,
274                       GLsizei width, GLsizei height,
275                       GLenum format, GLenum type,
276                       GLint img, GLint row, GLint column )
277{
278   return _mesa_image_address(3, packing, image, width, height,
279                              format, type, img, row, column);
280}
281
282
283
284/**
285 * Compute the stride (in bytes) between image rows.
286 *
287 * \param packing the pixelstore attributes
288 * \param width image width.
289 * \param format pixel format.
290 * \param type pixel data type.
291 *
292 * \return the stride in bytes for the given parameters, or -1 if error
293 */
294GLint
295_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
296                        GLint width, GLenum format, GLenum type )
297{
298   GLint bytesPerRow, remainder;
299
300   assert(packing);
301
302   if (type == GL_BITMAP) {
303      if (packing->RowLength == 0) {
304         bytesPerRow = (width + 7) / 8;
305      }
306      else {
307         bytesPerRow = (packing->RowLength + 7) / 8;
308      }
309   }
310   else {
311      /* Non-BITMAP data */
312      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
313      if (bytesPerPixel <= 0)
314         return -1;  /* error */
315      if (packing->RowLength == 0) {
316         bytesPerRow = bytesPerPixel * width;
317      }
318      else {
319         bytesPerRow = bytesPerPixel * packing->RowLength;
320      }
321   }
322
323   remainder = bytesPerRow % packing->Alignment;
324   if (remainder > 0) {
325      bytesPerRow += (packing->Alignment - remainder);
326   }
327
328   if (packing->Invert) {
329      /* negate the bytes per row (negative row stride) */
330      bytesPerRow = -bytesPerRow;
331   }
332
333   return bytesPerRow;
334}
335
336
337/*
338 * Compute the stride between images in a 3D texture (in bytes) for the given
339 * pixel packing parameters and image width, format and type.
340 */
341GLint
342_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
343                          GLint width, GLint height,
344                          GLenum format, GLenum type )
345{
346   GLint bytesPerRow, bytesPerImage, remainder;
347
348   assert(packing);
349
350   if (type == GL_BITMAP) {
351      if (packing->RowLength == 0) {
352         bytesPerRow = (width + 7) / 8;
353      }
354      else {
355         bytesPerRow = (packing->RowLength + 7) / 8;
356      }
357   }
358   else {
359      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
360
361      if (bytesPerPixel <= 0)
362         return -1;  /* error */
363      if (packing->RowLength == 0) {
364         bytesPerRow = bytesPerPixel * width;
365      }
366      else {
367         bytesPerRow = bytesPerPixel * packing->RowLength;
368      }
369   }
370
371   remainder = bytesPerRow % packing->Alignment;
372   if (remainder > 0)
373      bytesPerRow += (packing->Alignment - remainder);
374
375   if (packing->ImageHeight == 0)
376      bytesPerImage = bytesPerRow * height;
377   else
378      bytesPerImage = bytesPerRow * packing->ImageHeight;
379
380   return bytesPerImage;
381}
382
383
384
385/**
386 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
387 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
388 * "On" bits will set texels to \p onValue.
389 * "Off" bits will not modify texels.
390 * \param width  src bitmap width in pixels
391 * \param height  src bitmap height in pixels
392 * \param unpack  bitmap unpacking state
393 * \param bitmap  the src bitmap data
394 * \param destBuffer  start of dest buffer
395 * \param destStride  row stride in dest buffer
396 * \param onValue  if bit is 1, set destBuffer pixel to this value
397 */
398void
399_mesa_expand_bitmap(GLsizei width, GLsizei height,
400                    const struct gl_pixelstore_attrib *unpack,
401                    const GLubyte *bitmap,
402                    GLubyte *destBuffer, GLint destStride,
403                    GLubyte onValue)
404{
405   const GLubyte *srcRow = (const GLubyte *)
406      _mesa_image_address2d(unpack, bitmap, width, height,
407                            GL_COLOR_INDEX, GL_BITMAP, 0, 0);
408   const GLint srcStride = _mesa_image_row_stride(unpack, width,
409                                                  GL_COLOR_INDEX, GL_BITMAP);
410   GLint row, col;
411   GLubyte *dstRow = destBuffer;
412
413   for (row = 0; row < height; row++) {
414      const GLubyte *src = srcRow;
415
416      if (unpack->LsbFirst) {
417         /* Lsb first */
418         GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
419         for (col = 0; col < width; col++) {
420
421            if (*src & mask) {
422               dstRow[col] = onValue;
423            }
424
425            if (mask == 128U) {
426               src++;
427               mask = 1U;
428            }
429            else {
430               mask = mask << 1;
431            }
432         }
433
434         /* get ready for next row */
435         if (mask != 1)
436            src++;
437      }
438      else {
439         /* Msb first */
440         GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
441         for (col = 0; col < width; col++) {
442
443            if (*src & mask) {
444               dstRow[col] = onValue;
445            }
446
447            if (mask == 1U) {
448               src++;
449               mask = 128U;
450            }
451            else {
452               mask = mask >> 1;
453            }
454         }
455
456         /* get ready for next row */
457         if (mask != 128)
458            src++;
459      }
460
461      srcRow += srcStride;
462      dstRow += destStride;
463   } /* row */
464}
465
466
467
468
469/**
470 * Perform basic clipping for glDrawPixels.  The image's position and size
471 * and the unpack SkipPixels and SkipRows are adjusted so that the image
472 * region is entirely within the window and scissor bounds.
473 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
474 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
475 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
476 *
477 * \return  GL_TRUE if image is ready for drawing or
478 *          GL_FALSE if image was completely clipped away (draw nothing)
479 */
480GLboolean
481_mesa_clip_drawpixels(const struct gl_context *ctx,
482                      GLint *destX, GLint *destY,
483                      GLsizei *width, GLsizei *height,
484                      struct gl_pixelstore_attrib *unpack)
485{
486   const struct gl_framebuffer *buffer = ctx->DrawBuffer;
487
488   if (unpack->RowLength == 0) {
489      unpack->RowLength = *width;
490   }
491
492   assert(ctx->Pixel.ZoomX == 1.0F);
493   assert(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
494
495   /* left clipping */
496   if (*destX < buffer->_Xmin) {
497      unpack->SkipPixels += (buffer->_Xmin - *destX);
498      *width -= (buffer->_Xmin - *destX);
499      *destX = buffer->_Xmin;
500   }
501   /* right clipping */
502   if (*destX + *width > buffer->_Xmax)
503      *width -= (*destX + *width - buffer->_Xmax);
504
505   if (*width <= 0)
506      return GL_FALSE;
507
508   if (ctx->Pixel.ZoomY == 1.0F) {
509      /* bottom clipping */
510      if (*destY < buffer->_Ymin) {
511         unpack->SkipRows += (buffer->_Ymin - *destY);
512         *height -= (buffer->_Ymin - *destY);
513         *destY = buffer->_Ymin;
514      }
515      /* top clipping */
516      if (*destY + *height > buffer->_Ymax)
517         *height -= (*destY + *height - buffer->_Ymax);
518   }
519   else { /* upside down */
520      /* top clipping */
521      if (*destY > buffer->_Ymax) {
522         unpack->SkipRows += (*destY - buffer->_Ymax);
523         *height -= (*destY - buffer->_Ymax);
524         *destY = buffer->_Ymax;
525      }
526      /* bottom clipping */
527      if (*destY - *height < buffer->_Ymin)
528         *height -= (buffer->_Ymin - (*destY - *height));
529      /* adjust destY so it's the first row to write to */
530      (*destY)--;
531   }
532
533   if (*height <= 0)
534      return GL_FALSE;
535
536   return GL_TRUE;
537}
538
539
540/**
541 * Perform clipping for glReadPixels.  The image's window position
542 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
543 * so that the image region is entirely within the window bounds.
544 * Note: this is different from _mesa_clip_drawpixels() in that the
545 * scissor box is ignored, and we use the bounds of the current readbuffer
546 * surface or the attached image.
547 *
548 * \return  GL_TRUE if region to read is in bounds
549 *          GL_FALSE if region is completely out of bounds (nothing to read)
550 */
551GLboolean
552_mesa_clip_readpixels(const struct gl_context *ctx,
553                      GLint *srcX, GLint *srcY,
554                      GLsizei *width, GLsizei *height,
555                      struct gl_pixelstore_attrib *pack)
556{
557   const struct gl_framebuffer *buffer = ctx->ReadBuffer;
558   struct gl_renderbuffer *rb = buffer->_ColorReadBuffer;
559   GLsizei clip_width;
560   GLsizei clip_height;
561
562   if (rb) {
563      clip_width = rb->Width;
564      clip_height = rb->Height;
565   } else {
566      clip_width = buffer->Width;
567      clip_height = buffer->Height;
568   }
569
570
571   if (pack->RowLength == 0) {
572      pack->RowLength = *width;
573   }
574
575   /* left clipping */
576   if (*srcX < 0) {
577      pack->SkipPixels += (0 - *srcX);
578      *width -= (0 - *srcX);
579      *srcX = 0;
580   }
581   /* right clipping */
582   if (*srcX + *width > clip_width)
583      *width -= (*srcX + *width - clip_width);
584
585   if (*width <= 0)
586      return GL_FALSE;
587
588   /* bottom clipping */
589   if (*srcY < 0) {
590      pack->SkipRows += (0 - *srcY);
591      *height -= (0 - *srcY);
592      *srcY = 0;
593   }
594   /* top clipping */
595   if (*srcY + *height > clip_height)
596      *height -= (*srcY + *height - clip_height);
597
598   if (*height <= 0)
599      return GL_FALSE;
600
601   return GL_TRUE;
602}
603
604
605/**
606 * Do clipping for a glCopyTexSubImage call.
607 * The framebuffer source region might extend outside the framebuffer
608 * bounds.  Clip the source region against the framebuffer bounds and
609 * adjust the texture/dest position and size accordingly.
610 *
611 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
612 */
613GLboolean
614_mesa_clip_copytexsubimage(const struct gl_context *ctx,
615                           GLint *destX, GLint *destY,
616                           GLint *srcX, GLint *srcY,
617                           GLsizei *width, GLsizei *height)
618{
619   const struct gl_framebuffer *fb = ctx->ReadBuffer;
620   const GLint srcX0 = *srcX, srcY0 = *srcY;
621
622   if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
623                            srcX, srcY, width, height)) {
624      *destX = *destX + *srcX - srcX0;
625      *destY = *destY + *srcY - srcY0;
626
627      return GL_TRUE;
628   }
629   else {
630      return GL_FALSE;
631   }
632}
633
634
635
636/**
637 * Clip the rectangle defined by (x, y, width, height) against the bounds
638 * specified by [xmin, xmax) and [ymin, ymax).
639 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
640 */
641GLboolean
642_mesa_clip_to_region(GLint xmin, GLint ymin,
643                     GLint xmax, GLint ymax,
644                     GLint *x, GLint *y,
645                     GLsizei *width, GLsizei *height )
646{
647   /* left clipping */
648   if (*x < xmin) {
649      *width -= (xmin - *x);
650      *x = xmin;
651   }
652
653   /* right clipping */
654   if (*x + *width > xmax)
655      *width -= (*x + *width - xmax);
656
657   if (*width <= 0)
658      return GL_FALSE;
659
660   /* bottom (or top) clipping */
661   if (*y < ymin) {
662      *height -= (ymin - *y);
663      *y = ymin;
664   }
665
666   /* top (or bottom) clipping */
667   if (*y + *height > ymax)
668      *height -= (*y + *height - ymax);
669
670   if (*height <= 0)
671      return GL_FALSE;
672
673   return GL_TRUE;
674}
675
676
677/**
678 * Clip dst coords against Xmax (or Ymax).
679 */
680static inline void
681clip_right_or_top(GLint *srcX0, GLint *srcX1,
682                  GLint *dstX0, GLint *dstX1,
683                  GLint maxValue)
684{
685   GLfloat t, bias;
686
687   if (*dstX1 > maxValue) {
688      /* X1 outside right edge */
689      assert(*dstX0 < maxValue); /* X0 should be inside right edge */
690      t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
691      /* chop off [t, 1] part */
692      assert(t >= 0.0 && t <= 1.0);
693      *dstX1 = maxValue;
694      bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
695      *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
696   }
697   else if (*dstX0 > maxValue) {
698      /* X0 outside right edge */
699      assert(*dstX1 < maxValue); /* X1 should be inside right edge */
700      t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
701      /* chop off [t, 1] part */
702      assert(t >= 0.0 && t <= 1.0);
703      *dstX0 = maxValue;
704      bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
705      *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
706   }
707}
708
709
710/**
711 * Clip dst coords against Xmin (or Ymin).
712 */
713static inline void
714clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
715                    GLint *dstX0, GLint *dstX1,
716                    GLint minValue)
717{
718   GLfloat t, bias;
719
720   if (*dstX0 < minValue) {
721      /* X0 outside left edge */
722      assert(*dstX1 > minValue); /* X1 should be inside left edge */
723      t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
724      /* chop off [0, t] part */
725      assert(t >= 0.0 && t <= 1.0);
726      *dstX0 = minValue;
727      bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
728      *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
729   }
730   else if (*dstX1 < minValue) {
731      /* X1 outside left edge */
732      assert(*dstX0 > minValue); /* X0 should be inside left edge */
733      t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
734      /* chop off [0, t] part */
735      assert(t >= 0.0 && t <= 1.0);
736      *dstX1 = minValue;
737      bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
738      *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
739   }
740}
741
742
743/**
744 * Do clipping of blit src/dest rectangles.
745 * The dest rect is clipped against both the buffer bounds and scissor bounds.
746 * The src rect is just clipped against the buffer bounds.
747 *
748 * When either the src or dest rect is clipped, the other is also clipped
749 * proportionately!
750 *
751 * Note that X0 need not be less than X1 (same for Y) for either the source
752 * and dest rects.  That makes the clipping a little trickier.
753 *
754 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
755 */
756GLboolean
757_mesa_clip_blit(struct gl_context *ctx,
758                const struct gl_framebuffer *readFb,
759                const struct gl_framebuffer *drawFb,
760                GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
761                GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
762{
763   const GLint srcXmin = 0;
764   const GLint srcXmax = readFb->Width;
765   const GLint srcYmin = 0;
766   const GLint srcYmax = readFb->Height;
767
768   /* these include scissor bounds */
769   const GLint dstXmin = drawFb->_Xmin;
770   const GLint dstXmax = drawFb->_Xmax;
771   const GLint dstYmin = drawFb->_Ymin;
772   const GLint dstYmax = drawFb->_Ymax;
773
774   /*
775   printf("PreClipX:  src: %d .. %d  dst: %d .. %d\n",
776          *srcX0, *srcX1, *dstX0, *dstX1);
777   printf("PreClipY:  src: %d .. %d  dst: %d .. %d\n",
778          *srcY0, *srcY1, *dstY0, *dstY1);
779   */
780
781   /* trivial rejection tests */
782   if (*dstX0 == *dstX1)
783      return GL_FALSE; /* no width */
784   if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
785      return GL_FALSE; /* totally out (left) of bounds */
786   if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
787      return GL_FALSE; /* totally out (right) of bounds */
788
789   if (*dstY0 == *dstY1)
790      return GL_FALSE;
791   if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
792      return GL_FALSE;
793   if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
794      return GL_FALSE;
795
796   if (*srcX0 == *srcX1)
797      return GL_FALSE;
798   if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
799      return GL_FALSE;
800   if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
801      return GL_FALSE;
802
803   if (*srcY0 == *srcY1)
804      return GL_FALSE;
805   if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
806      return GL_FALSE;
807   if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
808      return GL_FALSE;
809
810   /*
811    * dest clip
812    */
813   clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
814   clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
815   clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
816   clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
817
818   /*
819    * src clip (just swap src/dst values from above)
820    */
821   clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
822   clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
823   clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
824   clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
825
826   /*
827   printf("PostClipX: src: %d .. %d  dst: %d .. %d\n",
828          *srcX0, *srcX1, *dstX0, *dstX1);
829   printf("PostClipY: src: %d .. %d  dst: %d .. %d\n",
830          *srcY0, *srcY1, *dstY0, *dstY1);
831   */
832
833   assert(*dstX0 >= dstXmin);
834   assert(*dstX0 <= dstXmax);
835   assert(*dstX1 >= dstXmin);
836   assert(*dstX1 <= dstXmax);
837
838   assert(*dstY0 >= dstYmin);
839   assert(*dstY0 <= dstYmax);
840   assert(*dstY1 >= dstYmin);
841   assert(*dstY1 <= dstYmax);
842
843   assert(*srcX0 >= srcXmin);
844   assert(*srcX0 <= srcXmax);
845   assert(*srcX1 >= srcXmin);
846   assert(*srcX1 <= srcXmax);
847
848   assert(*srcY0 >= srcYmin);
849   assert(*srcY0 <= srcYmax);
850   assert(*srcY1 >= srcYmin);
851   assert(*srcY1 <= srcYmax);
852
853   return GL_TRUE;
854}
855
856/**
857 * Swap the bytes in a 2D image.
858 *
859 * using the packing information this swaps the bytes
860 * according to the format and type of data being input.
861 * It takes into a/c various packing parameters like
862 * Alignment and RowLength.
863 */
864void
865_mesa_swap_bytes_2d_image(GLenum format, GLenum type,
866                          const struct gl_pixelstore_attrib *packing,
867                          GLsizei width, GLsizei height,
868                          GLvoid *dst, const GLvoid *src)
869{
870   GLint swapSize = _mesa_sizeof_packed_type(type);
871
872   assert(packing->SwapBytes);
873
874   if (swapSize == 2 || swapSize == 4) {
875      int swapsPerPixel = _mesa_bytes_per_pixel(format, type) / swapSize;
876      int stride = _mesa_image_row_stride(packing, width, format, type);
877      int row;
878      uint8_t *dstrow;
879      const uint8_t *srcrow;
880      assert(swapsPerPixel > 0);
881      assert(_mesa_bytes_per_pixel(format, type) % swapSize == 0);
882      dstrow = dst;
883      srcrow = src;
884      for (row = 0; row < height; row++) {
885         if (swapSize == 2)
886            swap2_copy((GLushort *)dstrow, (GLushort *)srcrow, width * swapsPerPixel);
887         else if (swapSize == 4)
888            swap4_copy((GLuint *)dstrow, (GLuint *)srcrow, width * swapsPerPixel);
889         dstrow += stride;
890         srcrow += stride;
891      }
892   }
893}
894