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