blob: 103855f7fa82bcc450d0729372593762070c27ee [file] [log] [blame]
Brian Paulfbd8f211999-11-11 01:22:25 +00001/* $Id: drawpix.c,v 1.6 1999/11/11 01:22:26 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paulfbd8f211999-11-11 01:22:25 +00005 * Version: 3.3
jtgafb833d1999-08-19 00:55:39 +00006 *
7 * Copyright (C) 1999 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
jtgafb833d1999-08-19 00:55:39 +000028#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
jtgafb833d1999-08-19 00:55:39 +000032#include "context.h"
33#include "drawpix.h"
34#include "feedback.h"
35#include "image.h"
36#include "macros.h"
37#include "mmath.h"
38#include "pixel.h"
39#include "span.h"
40#include "stencil.h"
41#include "types.h"
42#include "zoom.h"
jtgafb833d1999-08-19 00:55:39 +000043#endif
44
45
46
jtgafb833d1999-08-19 00:55:39 +000047/*
Brian Paulfbd8f211999-11-11 01:22:25 +000048 * Try to do a fast and simple RGB(a) glDrawPixels.
jtgafb833d1999-08-19 00:55:39 +000049 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
50 */
Brian Paulfbd8f211999-11-11 01:22:25 +000051static GLboolean
52simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
53 GLsizei width, GLsizei height, GLenum format, GLenum type,
54 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +000055{
Brian Paulfbd8f211999-11-11 01:22:25 +000056 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
jtgafb833d1999-08-19 00:55:39 +000057 GLubyte rgb[MAX_WIDTH][3];
58 GLubyte rgba[MAX_WIDTH][4];
59
60 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glDrawPixels",
61 GL_FALSE);
62
63
64 if (!ctx->Current.RasterPosValid) {
65 /* no-op */
66 return GL_TRUE;
67 }
68
69 if (ctx->NewState) {
70 gl_update_state(ctx);
71 }
72
73 /* see if device driver can do the drawpix */
Brian Paulfbd8f211999-11-11 01:22:25 +000074 if (ctx->Driver.DrawPixels
75 && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
76 unpack, pixels)) {
77 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +000078 }
79
80 if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0
81 && ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0
82 && ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0
83 && ctx->Pixel.BlueBias==0.0 && ctx->Pixel.BlueScale==1.0
84 && ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0
85 && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0
86 && ctx->Pixel.MapColorFlag==0
87 && unpack->Alignment==1
88 && !unpack->SwapBytes
89 && !unpack->LsbFirst) {
90
Brian Paulfbd8f211999-11-11 01:22:25 +000091 GLint destX = x;
92 GLint destY = y;
jtgafb833d1999-08-19 00:55:39 +000093 GLint drawWidth = width; /* actual width drawn */
94 GLint drawHeight = height; /* actual height drawn */
95 GLint skipPixels = unpack->SkipPixels;
96 GLint skipRows = unpack->SkipRows;
97 GLint rowLength;
98 GLdepth zSpan[MAX_WIDTH]; /* only used when zooming */
99 GLint zoomY0;
100
101 if (unpack->RowLength > 0)
102 rowLength = unpack->RowLength;
103 else
104 rowLength = width;
105
106 /* If we're not using pixel zoom then do all clipping calculations
107 * now. Otherwise, we'll let the gl_write_zoomed_*_span() functions
108 * handle the clipping.
109 */
110 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
111 /* horizontal clipping */
112 if (destX < ctx->Buffer->Xmin) {
113 skipPixels += (ctx->Buffer->Xmin - destX);
114 drawWidth -= (ctx->Buffer->Xmin - destX);
115 destX = ctx->Buffer->Xmin;
116 }
117 if (destX + drawWidth > ctx->Buffer->Xmax)
118 drawWidth -= (destX + drawWidth - ctx->Buffer->Xmax - 1);
119 if (drawWidth <= 0)
120 return GL_TRUE;
121
122 /* vertical clipping */
123 if (destY < ctx->Buffer->Ymin) {
124 skipRows += (ctx->Buffer->Ymin - destY);
125 drawHeight -= (ctx->Buffer->Ymin - destY);
126 destY = ctx->Buffer->Ymin;
127 }
128 if (destY + drawHeight > ctx->Buffer->Ymax)
129 drawHeight -= (destY + drawHeight - ctx->Buffer->Ymax - 1);
130 if (drawHeight <= 0)
131 return GL_TRUE;
132 }
133 else {
134 /* setup array of fragment Z value to pass to zoom function */
135 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
136 GLint i;
137 assert(drawWidth < MAX_WIDTH);
138 for (i=0; i<drawWidth; i++)
139 zSpan[i] = z;
140
141 /* save Y value of first row */
142 zoomY0 = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
143 }
144
145
146 /*
147 * Ready to draw!
148 * The window region at (destX, destY) of size (drawWidth, drawHeight)
149 * will be written to.
150 * We'll take pixel data from buffer pointed to by "pixels" but we'll
151 * skip "skipRows" rows and skip "skipPixels" pixels/row.
152 */
153
154 if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) {
155 if (ctx->Visual->RGBAflag) {
156 GLubyte *src = (GLubyte *) pixels
157 + (skipRows * rowLength + skipPixels) * 4;
158 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
159 /* no zooming */
160 GLint row;
161 for (row=0; row<drawHeight; row++) {
162 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
163 (void *) src, NULL);
164 src += rowLength * 4;
165 destY++;
166 }
167 }
168 else {
169 /* with zooming */
170 GLint row;
171 for (row=0; row<drawHeight; row++) {
172 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
173 zSpan, (void *) src, zoomY0);
174 src += rowLength * 4;
175 destY++;
176 }
177 }
178 }
179 return GL_TRUE;
180 }
181 else if (format==GL_RGB && type==GL_UNSIGNED_BYTE) {
182 if (ctx->Visual->RGBAflag) {
183 GLubyte *src = (GLubyte *) pixels
184 + (skipRows * rowLength + skipPixels) * 3;
185 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
186 GLint row;
187 for (row=0; row<drawHeight; row++) {
188 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
189 (void *) src, NULL);
190 src += rowLength * 3;
191 destY++;
192 }
193 }
194 else {
195 /* with zooming */
196 GLint row;
197 for (row=0; row<drawHeight; row++) {
198 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
199 zSpan, (void *) src, zoomY0);
200 src += rowLength * 3;
201 destY++;
202 }
203 }
204 }
205 return GL_TRUE;
206 }
207 else if (format==GL_LUMINANCE && type==GL_UNSIGNED_BYTE) {
208 if (ctx->Visual->RGBAflag) {
209 GLubyte *src = (GLubyte *) pixels
210 + (skipRows * rowLength + skipPixels);
211 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
212 /* no zooming */
213 GLint row;
214 assert(drawWidth < MAX_WIDTH);
215 for (row=0; row<drawHeight; row++) {
216 GLint i;
217 for (i=0;i<drawWidth;i++) {
218 rgb[i][0] = src[i];
219 rgb[i][1] = src[i];
220 rgb[i][2] = src[i];
221 }
222 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
223 (void *) rgb, NULL);
224 src += rowLength;
225 destY++;
226 }
227 }
228 else {
229 /* with zooming */
230 GLint row;
231 assert(drawWidth < MAX_WIDTH);
232 for (row=0; row<drawHeight; row++) {
233 GLint i;
234 for (i=0;i<drawWidth;i++) {
235 rgb[i][0] = src[i];
236 rgb[i][1] = src[i];
237 rgb[i][2] = src[i];
238 }
239 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
240 zSpan, (void *) rgb, zoomY0);
241 src += rowLength;
242 destY++;
243 }
244 }
245 }
246 return GL_TRUE;
247 }
248 else if (format==GL_LUMINANCE_ALPHA && type==GL_UNSIGNED_BYTE) {
249 if (ctx->Visual->RGBAflag) {
250 GLubyte *src = (GLubyte *) pixels
251 + (skipRows * rowLength + skipPixels)*2;
252 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
253 /* no zooming */
254 GLint row;
255 assert(drawWidth < MAX_WIDTH);
256 for (row=0; row<drawHeight; row++) {
257 GLint i;
258 GLubyte *ptr = src;
259 for (i=0;i<drawWidth;i++) {
260 rgba[i][0] = *ptr;
261 rgba[i][1] = *ptr;
262 rgba[i][2] = *ptr++;
263 rgba[i][3] = *ptr++;
264 }
265 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
266 (void *) rgba, NULL);
267 src += rowLength*2;
268 destY++;
269 }
270 }
271 else {
272 /* with zooming */
273 GLint row;
274 assert(drawWidth < MAX_WIDTH);
275 for (row=0; row<drawHeight; row++) {
276 GLubyte *ptr = src;
277 GLint i;
278 for (i=0;i<drawWidth;i++) {
279 rgba[i][0] = *ptr;
280 rgba[i][1] = *ptr;
281 rgba[i][2] = *ptr++;
282 rgba[i][3] = *ptr++;
283 }
284 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
285 zSpan, (void *) rgba, zoomY0);
286 src += rowLength*2;
287 destY++;
288 }
289 }
290 }
291 return GL_TRUE;
292 }
293 else if (format==GL_COLOR_INDEX && type==GL_UNSIGNED_BYTE) {
294 GLubyte *src = (GLubyte *) pixels + skipRows * rowLength + skipPixels;
295 if (ctx->Visual->RGBAflag) {
296 /* convert CI data to RGBA */
297 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
298 /* no zooming */
299 GLint row;
300 for (row=0; row<drawHeight; row++) {
301 assert(drawWidth < MAX_WIDTH);
302 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
303 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
304 (const GLubyte (*)[4])rgba,
305 NULL);
306 src += rowLength;
307 destY++;
308 }
309 return GL_TRUE;
310 }
311 else {
312 /* with zooming */
313 GLint row;
314 for (row=0; row<drawHeight; row++) {
315 assert(drawWidth < MAX_WIDTH);
316 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
317 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
318 zSpan, (void *) rgba, zoomY0);
319 src += rowLength;
320 destY++;
321 }
322 return GL_TRUE;
323 }
324 }
325 else {
326 /* write CI data to CI frame buffer */
327 GLint row;
328 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
329 /* no zooming */
330 for (row=0; row<drawHeight; row++) {
331 (*ctx->Driver.WriteCI8Span)(ctx, drawWidth, destX, destY,
332 src, NULL);
333 src += rowLength;
334 destY++;
335 }
336 return GL_TRUE;
337 }
338 else {
339 /* with zooming */
340 return GL_FALSE;
341 }
342 }
343 }
344 else {
345 /* can't handle this pixel format and/or data type here */
346 return GL_FALSE;
347 }
348 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000349
350 /* can't do a simple draw, have to use slow path */
351 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000352}
353
354
355
356/*
357 * Do glDrawPixels of index pixels.
358 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000359static void
360draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
361 GLsizei width, GLsizei height,
362 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000363{
jtgafb833d1999-08-19 00:55:39 +0000364 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
Brian Paulfbd8f211999-11-11 01:22:25 +0000365 const GLint desty = y;
366 GLint row, drawWidth;
367 GLdepth zspan[MAX_WIDTH];
jtgafb833d1999-08-19 00:55:39 +0000368
Brian Paulfbd8f211999-11-11 01:22:25 +0000369 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000370
371 /* Fragment depth values */
372 if (ctx->Depth.Test) {
373 GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
Brian Paulfbd8f211999-11-11 01:22:25 +0000374 GLint i;
375 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000376 zspan[i] = zval;
377 }
378 }
379
Brian Paulfbd8f211999-11-11 01:22:25 +0000380 /*
381 * General solution
382 */
383 for (row = 0; row < height; row++, y++) {
384 GLuint indexes[MAX_WIDTH];
385 const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
386 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
387 _mesa_unpack_index_span(ctx, drawWidth, GL_UNSIGNED_INT, indexes,
388 type, source, &ctx->Unpack, GL_TRUE);
389 if (zoom) {
390 gl_write_zoomed_index_span(ctx, drawWidth, x, y, zspan, indexes, desty);
jtgafb833d1999-08-19 00:55:39 +0000391 }
392 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000393 gl_write_index_span(ctx, drawWidth, x, y, zspan, indexes, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000394 }
395 }
jtgafb833d1999-08-19 00:55:39 +0000396}
397
398
399
400/*
401 * Do glDrawPixels of stencil image. The image datatype may either
402 * be GLubyte or GLbitmap.
403 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000404static void
405draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
406 GLsizei width, GLsizei height,
407 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000408{
jtgafb833d1999-08-19 00:55:39 +0000409 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
Brian Paulfbd8f211999-11-11 01:22:25 +0000410 const GLint desty = y;
411 GLint row, drawWidth;
jtgafb833d1999-08-19 00:55:39 +0000412
Brian Paulfbd8f211999-11-11 01:22:25 +0000413 if (type != GL_BYTE &&
414 type != GL_UNSIGNED_BYTE &&
415 type != GL_SHORT &&
416 type != GL_UNSIGNED_SHORT &&
417 type != GL_INT &&
418 type != GL_UNSIGNED_INT &&
419 type != GL_FLOAT &&
420 type != GL_BITMAP) {
421 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(stencil type)");
Brian Paulbc41b081999-10-19 20:33:57 +0000422 return;
423 }
424
Brian Paulfbd8f211999-11-11 01:22:25 +0000425 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000426
Brian Paulfbd8f211999-11-11 01:22:25 +0000427 for (row = 0; row < height; row++, y++) {
428 GLstencil values[MAX_WIDTH];
429 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
430 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
431 const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
432 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
433 _mesa_unpack_index_span(ctx, drawWidth, destType, values,
434 type, source, &ctx->Unpack, GL_TRUE);
jtgafb833d1999-08-19 00:55:39 +0000435
jtgafb833d1999-08-19 00:55:39 +0000436 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000437 gl_write_zoomed_stencil_span( ctx, (GLuint) drawWidth, x, y,
438 values, desty );
jtgafb833d1999-08-19 00:55:39 +0000439 }
440 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000441 gl_write_stencil_span( ctx, (GLuint) drawWidth, x, y, values );
jtgafb833d1999-08-19 00:55:39 +0000442 }
443 }
444}
445
446
447
448/*
449 * Do a glDrawPixels of depth values.
450 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000451static void
452draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
453 GLsizei width, GLsizei height,
454 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000455{
Brian Paulfbd8f211999-11-11 01:22:25 +0000456 const GLboolean bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
457 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
jtgafb833d1999-08-19 00:55:39 +0000458 const GLint desty = y;
459 GLubyte rgba[MAX_WIDTH][4];
460 GLuint ispan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000461 GLint drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000462
Brian Paulfbd8f211999-11-11 01:22:25 +0000463 if (type != GL_UNSIGNED_BYTE
464 && type != GL_UNSIGNED_BYTE
465 && type != GL_UNSIGNED_SHORT
466 && type != GL_UNSIGNED_SHORT
467 && type != GL_UNSIGNED_INT
468 && type != GL_UNSIGNED_INT
469 && type != GL_FLOAT) {
470 gl_error(ctx, GL_INVALID_ENUM, "glDrawPixels(type)");
471 return;
472 }
jtgafb833d1999-08-19 00:55:39 +0000473
Brian Paulfbd8f211999-11-11 01:22:25 +0000474 /* Colors or indexes */
jtgafb833d1999-08-19 00:55:39 +0000475 if (ctx->Visual->RGBAflag) {
476 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0F);
477 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0F);
478 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0F);
479 GLint a = (GLint) (ctx->Current.RasterColor[3] * 255.0F);
480 GLint i;
Brian Paulfbd8f211999-11-11 01:22:25 +0000481 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000482 rgba[i][RCOMP] = r;
483 rgba[i][GCOMP] = g;
484 rgba[i][BCOMP] = b;
485 rgba[i][ACOMP] = a;
486 }
487 }
488 else {
489 GLint i;
Brian Paulfbd8f211999-11-11 01:22:25 +0000490 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000491 ispan[i] = ctx->Current.RasterIndex;
492 }
493 }
494
Brian Paulfbd8f211999-11-11 01:22:25 +0000495 if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
jtgafb833d1999-08-19 00:55:39 +0000496 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
497 /* Special case: directly write 16-bit depth values */
Brian Paulfbd8f211999-11-11 01:22:25 +0000498 GLint row;
499 for (row = 0; row < height; row++, y++) {
500 const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
501 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
jtgafb833d1999-08-19 00:55:39 +0000502 gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
503 }
504 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000505 else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
jtgafb833d1999-08-19 00:55:39 +0000506 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
507 /* Special case: directly write 32-bit depth values */
Brian Paulfbd8f211999-11-11 01:22:25 +0000508 GLint i, row;
jtgafb833d1999-08-19 00:55:39 +0000509 /* Compute shift value to scale 32-bit uints down to depth values. */
510 GLuint shift = 0;
511 GLuint max = MAX_DEPTH;
Brian Paulfbd8f211999-11-11 01:22:25 +0000512 while ((max & 0x80000000) == 0) {
jtgafb833d1999-08-19 00:55:39 +0000513 max = max << 1;
514 shift++;
515 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000516 for (row = 0; row < height; row++, y++) {
jtgafb833d1999-08-19 00:55:39 +0000517 GLdepth zspan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000518 const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
519 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
jtgafb833d1999-08-19 00:55:39 +0000520 for (i=0;i<width;i++) {
521 zspan[i] = zptr[i] >> shift;
522 }
523 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
524 }
525 }
526 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000527 /* General case */
528 GLint row;
529 for (row = 0; row < height; row++, y++) {
jtgafb833d1999-08-19 00:55:39 +0000530 GLdepth zspan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000531 const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack,
532 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
533 _mesa_unpack_depth_span( ctx, drawWidth, zspan, type, src,
534 &ctx->Unpack, GL_TRUE );
jtgafb833d1999-08-19 00:55:39 +0000535 if (ctx->Visual->RGBAflag) {
536 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000537 gl_write_zoomed_rgba_span(ctx, width, x, y, zspan,
538 (const GLubyte (*)[4])rgba, desty);
jtgafb833d1999-08-19 00:55:39 +0000539 }
540 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000541 gl_write_rgba_span(ctx, width, x, y, zspan, rgba, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000542 }
543 }
544 else {
545 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000546 gl_write_zoomed_index_span(ctx, width, x, y, zspan,
547 ispan, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000548 }
549 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000550 gl_write_index_span(ctx, width, x, y, zspan, ispan, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000551 }
552 }
553
554 }
555 }
556}
557
558
jtgafb833d1999-08-19 00:55:39 +0000559/*
560 * Do glDrawPixels of RGBA pixels.
561 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000562static void
563draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
564 GLsizei width, GLsizei height,
565 GLenum format, GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000566{
Brian Paulfbd8f211999-11-11 01:22:25 +0000567 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
568 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
jtgafb833d1999-08-19 00:55:39 +0000569 const GLint desty = y;
570 GLdepth zspan[MAX_WIDTH];
571 GLboolean quickDraw;
jtgafb833d1999-08-19 00:55:39 +0000572
573 /* Try an optimized glDrawPixels first */
Brian Paulfbd8f211999-11-11 01:22:25 +0000574 if (simple_DrawPixels(ctx, x, y, width, height, format, type, pixels))
jtgafb833d1999-08-19 00:55:39 +0000575 return;
576
jtgafb833d1999-08-19 00:55:39 +0000577 /* Fragment depth values */
578 if (ctx->Depth.Test) {
579 /* fill in array of z values */
580 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
Brian Paulfbd8f211999-11-11 01:22:25 +0000581 GLint i;
jtgafb833d1999-08-19 00:55:39 +0000582 for (i=0;i<width;i++) {
583 zspan[i] = z;
584 }
585 }
586
Brian Paulfbd8f211999-11-11 01:22:25 +0000587
588 if (ctx->RasterMask == 0 && !zoom
589 && x >= 0 && y >= 0
590 && x + width <= ctx->Buffer->Width
591 && y + height <= ctx->Buffer->Height) {
jtgafb833d1999-08-19 00:55:39 +0000592 quickDraw = GL_TRUE;
593 }
594 else {
595 quickDraw = GL_FALSE;
596 }
597
Brian Paulfbd8f211999-11-11 01:22:25 +0000598 /*
599 * General solution
600 */
jtgafb833d1999-08-19 00:55:39 +0000601 {
jtgafb833d1999-08-19 00:55:39 +0000602 GLubyte rgba[MAX_WIDTH][4];
Brian Paulfbd8f211999-11-11 01:22:25 +0000603 GLint row;
604 if (width > MAX_WIDTH)
605 width = MAX_WIDTH;
606 for (row = 0; row < height; row++, y++) {
607 const GLvoid *source = gl_pixel_addr_in_image(unpack,
608 pixels, width, height, format, type, 0, row, 0);
609 _mesa_unpack_ubyte_color_span(ctx, width, GL_RGBA, (void*) rgba,
610 format, type, source, unpack, GL_TRUE);
jtgafb833d1999-08-19 00:55:39 +0000611
jtgafb833d1999-08-19 00:55:39 +0000612 if (quickDraw) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000613 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y,
614 (CONST GLubyte (*)[]) rgba, NULL);
jtgafb833d1999-08-19 00:55:39 +0000615 }
616 else if (zoom) {
617 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
Brian Paulfbd8f211999-11-11 01:22:25 +0000618 (CONST GLubyte (*)[]) rgba, desty );
jtgafb833d1999-08-19 00:55:39 +0000619 }
620 else {
621 gl_write_rgba_span( ctx, (GLuint) width, x, y, zspan, rgba, GL_BITMAP);
622 }
623 }
jtgafb833d1999-08-19 00:55:39 +0000624 }
625}
626
627
628
629/*
630 * Execute glDrawPixels
631 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000632void
633_mesa_DrawPixels( GLsizei width, GLsizei height,
634 GLenum format, GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000635{
Brian Paulfbd8f211999-11-11 01:22:25 +0000636 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000637 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawPixels");
638
jtgafb833d1999-08-19 00:55:39 +0000639 if (ctx->RenderMode==GL_RENDER) {
640 GLint x, y;
Brian Paulfbd8f211999-11-11 01:22:25 +0000641 if (!pixels || !ctx->Current.RasterPosValid) {
jtgafb833d1999-08-19 00:55:39 +0000642 return;
643 }
644
645 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
646 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
647
Brian Paulfbd8f211999-11-11 01:22:25 +0000648 switch (format) {
jtgafb833d1999-08-19 00:55:39 +0000649 case GL_STENCIL_INDEX:
Brian Paulfbd8f211999-11-11 01:22:25 +0000650 draw_stencil_pixels( ctx, x, y, width, height, type, pixels );
jtgafb833d1999-08-19 00:55:39 +0000651 break;
652 case GL_DEPTH_COMPONENT:
Brian Paulfbd8f211999-11-11 01:22:25 +0000653 draw_depth_pixels( ctx, x, y, width, height, type, pixels );
654 break;
655 case GL_COLOR_INDEX:
656 if (ctx->Visual->RGBAflag)
657 draw_index_pixels(ctx, x, y, width, height, type, pixels);
658 else
659 draw_rgba_pixels(ctx, x,y, width, height, format, type, pixels);
jtgafb833d1999-08-19 00:55:39 +0000660 break;
661 case GL_RED:
662 case GL_GREEN:
663 case GL_BLUE:
664 case GL_ALPHA:
jtgafb833d1999-08-19 00:55:39 +0000665 case GL_LUMINANCE:
666 case GL_LUMINANCE_ALPHA:
Brian Paulfbd8f211999-11-11 01:22:25 +0000667 case GL_RGB:
668 case GL_BGR:
jtgafb833d1999-08-19 00:55:39 +0000669 case GL_RGBA:
Brian Paulfbd8f211999-11-11 01:22:25 +0000670 case GL_BGRA:
671 case GL_ABGR_EXT:
672 draw_rgba_pixels(ctx, x, y, width, height, format, type, pixels);
jtgafb833d1999-08-19 00:55:39 +0000673 break;
674 default:
Brian Paulfbd8f211999-11-11 01:22:25 +0000675 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(format)" );
jtgafb833d1999-08-19 00:55:39 +0000676 return;
677 }
678 }
679 else if (ctx->RenderMode==GL_FEEDBACK) {
680 if (ctx->Current.RasterPosValid) {
681 GLfloat color[4];
682 GLfloat texcoord[4], invq;
683 UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor);
684 invq = 1.0F / ctx->Current.Texcoord[0][3];
685 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
686 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
687 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
688 texcoord[3] = ctx->Current.Texcoord[0][3];
689 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
690 gl_feedback_vertex( ctx,
Keith Whitwell1bf9dfa1999-09-18 20:41:22 +0000691 ctx->Current.RasterPos,
jtgafb833d1999-08-19 00:55:39 +0000692 color, ctx->Current.Index, texcoord );
693 }
694 }
695 else if (ctx->RenderMode==GL_SELECT) {
696 if (ctx->Current.RasterPosValid) {
697 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
698 }
699 }
700}
701