xref: /aosp_15_r20/external/mesa3d/src/mesa/main/image.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/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  */
52 static void
swap2_copy(GLushort * dst,GLushort * src,GLuint n)53 swap2_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 
61 void
_mesa_swap2(GLushort * p,GLuint n)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  */
76 static void
swap4_copy(GLuint * dst,GLuint * src,GLuint n)77 swap4_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 
90 void
_mesa_swap4(GLuint * p,GLuint n)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  */
115 GLintptr
_mesa_image_offset(GLuint dimensions,const struct gl_pixelstore_attrib * packing,GLsizei width,GLsizei height,GLenum format,GLenum type,GLint img,GLint row,GLint column)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  */
230 GLvoid *
_mesa_image_address(GLuint dimensions,const struct gl_pixelstore_attrib * packing,const GLvoid * image,GLsizei width,GLsizei height,GLenum format,GLenum type,GLint img,GLint row,GLint column)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 
247 GLvoid *
_mesa_image_address1d(const struct gl_pixelstore_attrib * packing,const GLvoid * image,GLsizei width,GLenum format,GLenum type,GLint column)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 
259 GLvoid *
_mesa_image_address2d(const struct gl_pixelstore_attrib * packing,const GLvoid * image,GLsizei width,GLsizei height,GLenum format,GLenum type,GLint row,GLint column)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 
271 GLvoid *
_mesa_image_address3d(const struct gl_pixelstore_attrib * packing,const GLvoid * image,GLsizei width,GLsizei height,GLenum format,GLenum type,GLint img,GLint row,GLint column)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  */
294 GLint
_mesa_image_row_stride(const struct gl_pixelstore_attrib * packing,GLint width,GLenum format,GLenum type)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  */
341 intptr_t
_mesa_image_image_stride(const struct gl_pixelstore_attrib * packing,GLint width,GLint height,GLenum format,GLenum type)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, remainder;
347    intptr_t bytesPerImage;
348 
349    assert(packing);
350 
351    if (type == GL_BITMAP) {
352       if (packing->RowLength == 0) {
353          bytesPerRow = (width + 7) / 8;
354       }
355       else {
356          bytesPerRow = (packing->RowLength + 7) / 8;
357       }
358    }
359    else {
360       const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
361 
362       if (bytesPerPixel <= 0)
363          return -1;  /* error */
364       if (packing->RowLength == 0) {
365          bytesPerRow = bytesPerPixel * width;
366       }
367       else {
368          bytesPerRow = bytesPerPixel * packing->RowLength;
369       }
370    }
371 
372    remainder = bytesPerRow % packing->Alignment;
373    if (remainder > 0)
374       bytesPerRow += (packing->Alignment - remainder);
375 
376    if (packing->ImageHeight == 0)
377       bytesPerImage = (intptr_t)bytesPerRow * height;
378    else
379       bytesPerImage = (intptr_t)bytesPerRow * packing->ImageHeight;
380 
381    return bytesPerImage;
382 }
383 
384 
385 
386 /**
387  * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
388  * This is typically used to convert a bitmap into a GLubyte/pixel texture.
389  * "On" bits will set texels to \p onValue.
390  * "Off" bits will not modify texels.
391  * \param width  src bitmap width in pixels
392  * \param height  src bitmap height in pixels
393  * \param unpack  bitmap unpacking state
394  * \param bitmap  the src bitmap data
395  * \param destBuffer  start of dest buffer
396  * \param destStride  row stride in dest buffer
397  * \param onValue  if bit is 1, set destBuffer pixel to this value
398  */
399 void
_mesa_expand_bitmap(GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap,GLubyte * destBuffer,GLint destStride,GLubyte onValue)400 _mesa_expand_bitmap(GLsizei width, GLsizei height,
401                     const struct gl_pixelstore_attrib *unpack,
402                     const GLubyte *bitmap,
403                     GLubyte *destBuffer, GLint destStride,
404                     GLubyte onValue)
405 {
406    const GLubyte *srcRow = (const GLubyte *)
407       _mesa_image_address2d(unpack, bitmap, width, height,
408                             GL_COLOR_INDEX, GL_BITMAP, 0, 0);
409    const GLint srcStride = _mesa_image_row_stride(unpack, width,
410                                                   GL_COLOR_INDEX, GL_BITMAP);
411    GLint row, col;
412    GLubyte *dstRow = destBuffer;
413 
414    for (row = 0; row < height; row++) {
415       const GLubyte *src = srcRow;
416 
417       if (unpack->LsbFirst) {
418          /* Lsb first */
419          GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
420          for (col = 0; col < width; col++) {
421 
422             if (*src & mask) {
423                dstRow[col] = onValue;
424             }
425 
426             if (mask == 128U) {
427                src++;
428                mask = 1U;
429             }
430             else {
431                mask = mask << 1;
432             }
433          }
434 
435          /* get ready for next row */
436          if (mask != 1)
437             src++;
438       }
439       else {
440          /* Msb first */
441          GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
442          for (col = 0; col < width; col++) {
443 
444             if (*src & mask) {
445                dstRow[col] = onValue;
446             }
447 
448             if (mask == 1U) {
449                src++;
450                mask = 128U;
451             }
452             else {
453                mask = mask >> 1;
454             }
455          }
456 
457          /* get ready for next row */
458          if (mask != 128)
459             src++;
460       }
461 
462       srcRow += srcStride;
463       dstRow += destStride;
464    } /* row */
465 }
466 
467 
468 
469 
470 /**
471  * Perform basic clipping for glDrawPixels.  The image's position and size
472  * and the unpack SkipPixels and SkipRows are adjusted so that the image
473  * region is entirely within the window and scissor bounds.
474  * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
475  * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
476  * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
477  *
478  * \return  GL_TRUE if image is ready for drawing or
479  *          GL_FALSE if image was completely clipped away (draw nothing)
480  */
481 GLboolean
_mesa_clip_drawpixels(const struct gl_context * ctx,GLint * destX,GLint * destY,GLsizei * width,GLsizei * height,struct gl_pixelstore_attrib * unpack)482 _mesa_clip_drawpixels(const struct gl_context *ctx,
483                       GLint *destX, GLint *destY,
484                       GLsizei *width, GLsizei *height,
485                       struct gl_pixelstore_attrib *unpack)
486 {
487    const struct gl_framebuffer *buffer = ctx->DrawBuffer;
488 
489    if (unpack->RowLength == 0) {
490       unpack->RowLength = *width;
491    }
492 
493    assert(ctx->Pixel.ZoomX == 1.0F);
494    assert(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
495 
496    /* left clipping */
497    if (*destX < buffer->_Xmin) {
498       unpack->SkipPixels += (buffer->_Xmin - *destX);
499       *width -= (buffer->_Xmin - *destX);
500       *destX = buffer->_Xmin;
501    }
502    /* right clipping */
503    if (*destX + *width > buffer->_Xmax)
504       *width -= (*destX + *width - buffer->_Xmax);
505 
506    if (*width <= 0)
507       return GL_FALSE;
508 
509    if (ctx->Pixel.ZoomY == 1.0F) {
510       /* bottom clipping */
511       if (*destY < buffer->_Ymin) {
512          unpack->SkipRows += (buffer->_Ymin - *destY);
513          *height -= (buffer->_Ymin - *destY);
514          *destY = buffer->_Ymin;
515       }
516       /* top clipping */
517       if (*destY + *height > buffer->_Ymax)
518          *height -= (*destY + *height - buffer->_Ymax);
519    }
520    else { /* upside down */
521       /* top clipping */
522       if (*destY > buffer->_Ymax) {
523          unpack->SkipRows += (*destY - buffer->_Ymax);
524          *height -= (*destY - buffer->_Ymax);
525          *destY = buffer->_Ymax;
526       }
527       /* bottom clipping */
528       if (*destY - *height < buffer->_Ymin)
529          *height -= (buffer->_Ymin - (*destY - *height));
530       /* adjust destY so it's the first row to write to */
531       (*destY)--;
532    }
533 
534    if (*height <= 0)
535       return GL_FALSE;
536 
537    return GL_TRUE;
538 }
539 
540 
541 /**
542  * Perform clipping for glReadPixels.  The image's window position
543  * and size, and the pack skipPixels, skipRows and rowLength are adjusted
544  * so that the image region is entirely within the window bounds.
545  * Note: this is different from _mesa_clip_drawpixels() in that the
546  * scissor box is ignored, and we use the bounds of the current readbuffer
547  * surface or the attached image.
548  *
549  * \return  GL_TRUE if region to read is in bounds
550  *          GL_FALSE if region is completely out of bounds (nothing to read)
551  */
552 GLboolean
_mesa_clip_readpixels(const struct gl_context * ctx,GLint * srcX,GLint * srcY,GLsizei * width,GLsizei * height,struct gl_pixelstore_attrib * pack)553 _mesa_clip_readpixels(const struct gl_context *ctx,
554                       GLint *srcX, GLint *srcY,
555                       GLsizei *width, GLsizei *height,
556                       struct gl_pixelstore_attrib *pack)
557 {
558    const struct gl_framebuffer *buffer = ctx->ReadBuffer;
559    struct gl_renderbuffer *rb = buffer->_ColorReadBuffer;
560    GLsizei clip_width;
561    GLsizei clip_height;
562 
563    if (rb) {
564       clip_width = rb->Width;
565       clip_height = rb->Height;
566    } else {
567       clip_width = buffer->Width;
568       clip_height = buffer->Height;
569    }
570 
571 
572    if (pack->RowLength == 0) {
573       pack->RowLength = *width;
574    }
575 
576    /* left clipping */
577    if (*srcX < 0) {
578       pack->SkipPixels += (0 - *srcX);
579       *width -= (0 - *srcX);
580       *srcX = 0;
581    }
582    /* right clipping */
583    if (*srcX + *width > clip_width)
584       *width -= (*srcX + *width - clip_width);
585 
586    if (*width <= 0)
587       return GL_FALSE;
588 
589    /* bottom clipping */
590    if (*srcY < 0) {
591       pack->SkipRows += (0 - *srcY);
592       *height -= (0 - *srcY);
593       *srcY = 0;
594    }
595    /* top clipping */
596    if (*srcY + *height > clip_height)
597       *height -= (*srcY + *height - clip_height);
598 
599    if (*height <= 0)
600       return GL_FALSE;
601 
602    return GL_TRUE;
603 }
604 
605 
606 /**
607  * Do clipping for a glCopyTexSubImage call.
608  * The framebuffer source region might extend outside the framebuffer
609  * bounds.  Clip the source region against the framebuffer bounds and
610  * adjust the texture/dest position and size accordingly.
611  *
612  * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
613  */
614 GLboolean
_mesa_clip_copytexsubimage(const struct gl_context * ctx,GLint * destX,GLint * destY,GLint * srcX,GLint * srcY,GLsizei * width,GLsizei * height)615 _mesa_clip_copytexsubimage(const struct gl_context *ctx,
616                            GLint *destX, GLint *destY,
617                            GLint *srcX, GLint *srcY,
618                            GLsizei *width, GLsizei *height)
619 {
620    const struct gl_framebuffer *fb = ctx->ReadBuffer;
621    const GLint srcX0 = *srcX, srcY0 = *srcY;
622 
623    if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
624                             srcX, srcY, width, height)) {
625       *destX = *destX + *srcX - srcX0;
626       *destY = *destY + *srcY - srcY0;
627 
628       return GL_TRUE;
629    }
630    else {
631       return GL_FALSE;
632    }
633 }
634 
635 
636 
637 /**
638  * Clip the rectangle defined by (x, y, width, height) against the bounds
639  * specified by [xmin, xmax) and [ymin, ymax).
640  * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
641  */
642 GLboolean
_mesa_clip_to_region(GLint xmin,GLint ymin,GLint xmax,GLint ymax,GLint * x,GLint * y,GLsizei * width,GLsizei * height)643 _mesa_clip_to_region(GLint xmin, GLint ymin,
644                      GLint xmax, GLint ymax,
645                      GLint *x, GLint *y,
646                      GLsizei *width, GLsizei *height )
647 {
648    /* left clipping */
649    if (*x < xmin) {
650       *width -= (xmin - *x);
651       *x = xmin;
652    }
653 
654    /* right clipping */
655    if (*x + *width > xmax)
656       *width -= (*x + *width - xmax);
657 
658    if (*width <= 0)
659       return GL_FALSE;
660 
661    /* bottom (or top) clipping */
662    if (*y < ymin) {
663       *height -= (ymin - *y);
664       *y = ymin;
665    }
666 
667    /* top (or bottom) clipping */
668    if (*y + *height > ymax)
669       *height -= (*y + *height - ymax);
670 
671    if (*height <= 0)
672       return GL_FALSE;
673 
674    return GL_TRUE;
675 }
676 
677 
678 /**
679  * Clip dst coords against Xmax (or Ymax).
680  */
681 static inline void
clip_right_or_top(GLint * srcX0,GLint * srcX1,GLint * dstX0,GLint * dstX1,GLint maxValue)682 clip_right_or_top(GLint *srcX0, GLint *srcX1,
683                   GLint *dstX0, GLint *dstX1,
684                   GLint maxValue)
685 {
686    GLfloat t, bias;
687 
688    if (*dstX1 > maxValue) {
689       /* X1 outside right edge */
690       assert(*dstX0 < maxValue); /* X0 should be inside right edge */
691       t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
692       /* chop off [t, 1] part */
693       assert(t >= 0.0 && t <= 1.0);
694       *dstX1 = maxValue;
695       bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
696       *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
697    }
698    else if (*dstX0 > maxValue) {
699       /* X0 outside right edge */
700       assert(*dstX1 < maxValue); /* X1 should be inside right edge */
701       t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
702       /* chop off [t, 1] part */
703       assert(t >= 0.0 && t <= 1.0);
704       *dstX0 = maxValue;
705       bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
706       *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
707    }
708 }
709 
710 
711 /**
712  * Clip dst coords against Xmin (or Ymin).
713  */
714 static inline void
clip_left_or_bottom(GLint * srcX0,GLint * srcX1,GLint * dstX0,GLint * dstX1,GLint minValue)715 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
716                     GLint *dstX0, GLint *dstX1,
717                     GLint minValue)
718 {
719    GLfloat t, bias;
720 
721    if (*dstX0 < minValue) {
722       /* X0 outside left edge */
723       assert(*dstX1 > minValue); /* X1 should be inside left edge */
724       t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
725       /* chop off [0, t] part */
726       assert(t >= 0.0 && t <= 1.0);
727       *dstX0 = minValue;
728       bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
729       *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
730    }
731    else if (*dstX1 < minValue) {
732       /* X1 outside left edge */
733       assert(*dstX0 > minValue); /* X0 should be inside left edge */
734       t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
735       /* chop off [0, t] part */
736       assert(t >= 0.0 && t <= 1.0);
737       *dstX1 = minValue;
738       bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
739       *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
740    }
741 }
742 
743 
744 /**
745  * Do clipping of blit src/dest rectangles.
746  * The dest rect is clipped against both the buffer bounds and scissor bounds.
747  * The src rect is just clipped against the buffer bounds.
748  *
749  * When either the src or dest rect is clipped, the other is also clipped
750  * proportionately!
751  *
752  * Note that X0 need not be less than X1 (same for Y) for either the source
753  * and dest rects.  That makes the clipping a little trickier.
754  *
755  * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
756  */
757 GLboolean
_mesa_clip_blit(struct gl_context * ctx,const struct gl_framebuffer * readFb,const struct gl_framebuffer * drawFb,GLint * srcX0,GLint * srcY0,GLint * srcX1,GLint * srcY1,GLint * dstX0,GLint * dstY0,GLint * dstX1,GLint * dstY1)758 _mesa_clip_blit(struct gl_context *ctx,
759                 const struct gl_framebuffer *readFb,
760                 const struct gl_framebuffer *drawFb,
761                 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
762                 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
763 {
764    const GLint srcXmin = 0;
765    const GLint srcXmax = readFb->Width;
766    const GLint srcYmin = 0;
767    const GLint srcYmax = readFb->Height;
768 
769    /* these include scissor bounds */
770    const GLint dstXmin = drawFb->_Xmin;
771    const GLint dstXmax = drawFb->_Xmax;
772    const GLint dstYmin = drawFb->_Ymin;
773    const GLint dstYmax = drawFb->_Ymax;
774 
775    /*
776    printf("PreClipX:  src: %d .. %d  dst: %d .. %d\n",
777           *srcX0, *srcX1, *dstX0, *dstX1);
778    printf("PreClipY:  src: %d .. %d  dst: %d .. %d\n",
779           *srcY0, *srcY1, *dstY0, *dstY1);
780    */
781 
782    /* trivial rejection tests */
783    if (*dstX0 == *dstX1)
784       return GL_FALSE; /* no width */
785    if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
786       return GL_FALSE; /* totally out (left) of bounds */
787    if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
788       return GL_FALSE; /* totally out (right) of bounds */
789 
790    if (*dstY0 == *dstY1)
791       return GL_FALSE;
792    if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
793       return GL_FALSE;
794    if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
795       return GL_FALSE;
796 
797    if (*srcX0 == *srcX1)
798       return GL_FALSE;
799    if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
800       return GL_FALSE;
801    if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
802       return GL_FALSE;
803 
804    if (*srcY0 == *srcY1)
805       return GL_FALSE;
806    if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
807       return GL_FALSE;
808    if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
809       return GL_FALSE;
810 
811    /*
812     * dest clip
813     */
814    clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
815    clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
816    clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
817    clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
818 
819    /*
820     * src clip (just swap src/dst values from above)
821     */
822    clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
823    clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
824    clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
825    clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
826 
827    /*
828    printf("PostClipX: src: %d .. %d  dst: %d .. %d\n",
829           *srcX0, *srcX1, *dstX0, *dstX1);
830    printf("PostClipY: src: %d .. %d  dst: %d .. %d\n",
831           *srcY0, *srcY1, *dstY0, *dstY1);
832    */
833 
834    assert(*dstX0 >= dstXmin);
835    assert(*dstX0 <= dstXmax);
836    assert(*dstX1 >= dstXmin);
837    assert(*dstX1 <= dstXmax);
838 
839    assert(*dstY0 >= dstYmin);
840    assert(*dstY0 <= dstYmax);
841    assert(*dstY1 >= dstYmin);
842    assert(*dstY1 <= dstYmax);
843 
844    assert(*srcX0 >= srcXmin);
845    assert(*srcX0 <= srcXmax);
846    assert(*srcX1 >= srcXmin);
847    assert(*srcX1 <= srcXmax);
848 
849    assert(*srcY0 >= srcYmin);
850    assert(*srcY0 <= srcYmax);
851    assert(*srcY1 >= srcYmin);
852    assert(*srcY1 <= srcYmax);
853 
854    return GL_TRUE;
855 }
856 
857 /**
858  * Swap the bytes in a 2D image.
859  *
860  * using the packing information this swaps the bytes
861  * according to the format and type of data being input.
862  * It takes into a/c various packing parameters like
863  * Alignment and RowLength.
864  */
865 void
_mesa_swap_bytes_2d_image(GLenum format,GLenum type,const struct gl_pixelstore_attrib * packing,GLsizei width,GLsizei height,GLvoid * dst,const GLvoid * src)866 _mesa_swap_bytes_2d_image(GLenum format, GLenum type,
867                           const struct gl_pixelstore_attrib *packing,
868                           GLsizei width, GLsizei height,
869                           GLvoid *dst, const GLvoid *src)
870 {
871    GLint swapSize = _mesa_sizeof_packed_type(type);
872 
873    assert(packing->SwapBytes);
874 
875    if (swapSize == 2 || swapSize == 4) {
876       int swapsPerPixel = _mesa_bytes_per_pixel(format, type) / swapSize;
877       int stride = _mesa_image_row_stride(packing, width, format, type);
878       int row;
879       uint8_t *dstrow;
880       const uint8_t *srcrow;
881       assert(swapsPerPixel > 0);
882       assert(_mesa_bytes_per_pixel(format, type) % swapSize == 0);
883       dstrow = dst;
884       srcrow = src;
885       for (row = 0; row < height; row++) {
886          if (swapSize == 2)
887             swap2_copy((GLushort *)dstrow, (GLushort *)srcrow, width * swapsPerPixel);
888          else if (swapSize == 4)
889             swap4_copy((GLuint *)dstrow, (GLuint *)srcrow, width * swapsPerPixel);
890          dstrow += stride;
891          srcrow += stride;
892       }
893    }
894 }
895