blob: 56ee2d76e38bfa88e8b472f756fc0e8b0e004873 [file] [log] [blame]
Brian Paul4ee75bd2000-03-03 15:39:13 +00001/* $Id: drawpix.c,v 1.13 2000/03/03 15:39:13 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 *
Brian Paulbab8f792000-02-08 23:42:14 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +00008 *
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"
Brian Paulea39f042000-02-02 19:17:57 +000040#include "state.h"
jtgafb833d1999-08-19 00:55:39 +000041#include "stencil.h"
42#include "types.h"
43#include "zoom.h"
jtgafb833d1999-08-19 00:55:39 +000044#endif
45
46
47
jtgafb833d1999-08-19 00:55:39 +000048/*
Brian Paul4e176ff1999-11-22 22:21:38 +000049 * Given the dest position, size and skipPixels and skipRows values
50 * for a glDrawPixels command, perform clipping of the image bounds
51 * so the result lies withing the context's buffer bounds.
52 * Return: GL_TRUE if image is ready for drawing
53 * GL_FALSE if image was completely clipped away (draw nothing)
54 */
55GLboolean
56_mesa_clip_pixelrect(const GLcontext *ctx,
57 GLint *destX, GLint *destY,
58 GLsizei *width, GLsizei *height,
59 GLint *skipPixels, GLint *skipRows)
60{
Brian Paul3f02f901999-11-24 18:48:30 +000061 const GLframebuffer *buffer = ctx->DrawBuffer;
62
Brian Paul4e176ff1999-11-22 22:21:38 +000063 /* left clipping */
Brian Paul3f02f901999-11-24 18:48:30 +000064 if (*destX < buffer->Xmin) {
65 *skipPixels += (buffer->Xmin - *destX);
66 *width -= (buffer->Xmin - *destX);
67 *destX = buffer->Xmin;
Brian Paul4e176ff1999-11-22 22:21:38 +000068 }
69 /* right clipping */
Brian Paul3f02f901999-11-24 18:48:30 +000070 if (*destX + *width > buffer->Xmax)
71 *width -= (*destX + *width - buffer->Xmax - 1);
Brian Paul4e176ff1999-11-22 22:21:38 +000072
73 if (*width <= 0)
74 return GL_FALSE;
75
76 /* bottom clipping */
Brian Paul3f02f901999-11-24 18:48:30 +000077 if (*destY < buffer->Ymin) {
78 *skipRows += (buffer->Ymin - *destY);
79 *height -= (buffer->Ymin - *destY);
80 *destY = buffer->Ymin;
Brian Paul4e176ff1999-11-22 22:21:38 +000081 }
82 /* top clipping */
Brian Paul3f02f901999-11-24 18:48:30 +000083 if (*destY + *height > buffer->Ymax)
84 *height -= (*destY + *height - buffer->Ymax - 1);
Brian Paul4e176ff1999-11-22 22:21:38 +000085
86 if (*height <= 0)
87 return GL_TRUE;
88
89 return GL_TRUE;
90}
91
92
93
94/*
Brian Paulfbd8f211999-11-11 01:22:25 +000095 * Try to do a fast and simple RGB(a) glDrawPixels.
jtgafb833d1999-08-19 00:55:39 +000096 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
97 */
Brian Paulfbd8f211999-11-11 01:22:25 +000098static GLboolean
99simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
100 GLsizei width, GLsizei height, GLenum format, GLenum type,
101 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000102{
Brian Paulfbd8f211999-11-11 01:22:25 +0000103 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
jtgafb833d1999-08-19 00:55:39 +0000104 GLubyte rgb[MAX_WIDTH][3];
105 GLubyte rgba[MAX_WIDTH][4];
106
107 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glDrawPixels",
108 GL_FALSE);
109
110
111 if (!ctx->Current.RasterPosValid) {
112 /* no-op */
113 return GL_TRUE;
114 }
115
116 if (ctx->NewState) {
117 gl_update_state(ctx);
118 }
119
jtgafb833d1999-08-19 00:55:39 +0000120 if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0
121 && ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0
122 && ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0
123 && ctx->Pixel.BlueBias==0.0 && ctx->Pixel.BlueScale==1.0
124 && ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0
125 && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0
126 && ctx->Pixel.MapColorFlag==0
127 && unpack->Alignment==1
128 && !unpack->SwapBytes
129 && !unpack->LsbFirst) {
130
Brian Paulfbd8f211999-11-11 01:22:25 +0000131 GLint destX = x;
132 GLint destY = y;
jtgafb833d1999-08-19 00:55:39 +0000133 GLint drawWidth = width; /* actual width drawn */
134 GLint drawHeight = height; /* actual height drawn */
135 GLint skipPixels = unpack->SkipPixels;
136 GLint skipRows = unpack->SkipRows;
137 GLint rowLength;
138 GLdepth zSpan[MAX_WIDTH]; /* only used when zooming */
139 GLint zoomY0;
140
141 if (unpack->RowLength > 0)
142 rowLength = unpack->RowLength;
143 else
144 rowLength = width;
145
146 /* If we're not using pixel zoom then do all clipping calculations
147 * now. Otherwise, we'll let the gl_write_zoomed_*_span() functions
148 * handle the clipping.
149 */
150 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
151 /* horizontal clipping */
Brian Paul3f02f901999-11-24 18:48:30 +0000152 if (destX < ctx->DrawBuffer->Xmin) {
153 skipPixels += (ctx->DrawBuffer->Xmin - destX);
154 drawWidth -= (ctx->DrawBuffer->Xmin - destX);
155 destX = ctx->DrawBuffer->Xmin;
jtgafb833d1999-08-19 00:55:39 +0000156 }
Brian Paul3f02f901999-11-24 18:48:30 +0000157 if (destX + drawWidth > ctx->DrawBuffer->Xmax)
158 drawWidth -= (destX + drawWidth - ctx->DrawBuffer->Xmax - 1);
jtgafb833d1999-08-19 00:55:39 +0000159 if (drawWidth <= 0)
160 return GL_TRUE;
161
162 /* vertical clipping */
Brian Paul3f02f901999-11-24 18:48:30 +0000163 if (destY < ctx->DrawBuffer->Ymin) {
164 skipRows += (ctx->DrawBuffer->Ymin - destY);
165 drawHeight -= (ctx->DrawBuffer->Ymin - destY);
166 destY = ctx->DrawBuffer->Ymin;
jtgafb833d1999-08-19 00:55:39 +0000167 }
Brian Paul3f02f901999-11-24 18:48:30 +0000168 if (destY + drawHeight > ctx->DrawBuffer->Ymax)
169 drawHeight -= (destY + drawHeight - ctx->DrawBuffer->Ymax - 1);
jtgafb833d1999-08-19 00:55:39 +0000170 if (drawHeight <= 0)
171 return GL_TRUE;
Brian Paul4b7526d1999-11-18 15:44:37 +0000172
173 zoomY0 = 0; /* not used - silence compiler warning */
jtgafb833d1999-08-19 00:55:39 +0000174 }
175 else {
176 /* setup array of fragment Z value to pass to zoom function */
177 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
178 GLint i;
179 assert(drawWidth < MAX_WIDTH);
180 for (i=0; i<drawWidth; i++)
181 zSpan[i] = z;
182
183 /* save Y value of first row */
184 zoomY0 = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
185 }
186
187
188 /*
189 * Ready to draw!
190 * The window region at (destX, destY) of size (drawWidth, drawHeight)
191 * will be written to.
192 * We'll take pixel data from buffer pointed to by "pixels" but we'll
193 * skip "skipRows" rows and skip "skipPixels" pixels/row.
194 */
195
196 if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) {
197 if (ctx->Visual->RGBAflag) {
198 GLubyte *src = (GLubyte *) pixels
199 + (skipRows * rowLength + skipPixels) * 4;
200 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
201 /* no zooming */
202 GLint row;
203 for (row=0; row<drawHeight; row++) {
204 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
205 (void *) src, NULL);
206 src += rowLength * 4;
207 destY++;
208 }
209 }
210 else {
211 /* with zooming */
212 GLint row;
213 for (row=0; row<drawHeight; row++) {
214 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
215 zSpan, (void *) src, zoomY0);
216 src += rowLength * 4;
217 destY++;
218 }
219 }
220 }
221 return GL_TRUE;
222 }
223 else if (format==GL_RGB && type==GL_UNSIGNED_BYTE) {
224 if (ctx->Visual->RGBAflag) {
225 GLubyte *src = (GLubyte *) pixels
226 + (skipRows * rowLength + skipPixels) * 3;
227 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
228 GLint row;
229 for (row=0; row<drawHeight; row++) {
230 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
231 (void *) src, NULL);
232 src += rowLength * 3;
233 destY++;
234 }
235 }
236 else {
237 /* with zooming */
238 GLint row;
239 for (row=0; row<drawHeight; row++) {
240 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
241 zSpan, (void *) src, zoomY0);
242 src += rowLength * 3;
243 destY++;
244 }
245 }
246 }
247 return GL_TRUE;
248 }
249 else if (format==GL_LUMINANCE && type==GL_UNSIGNED_BYTE) {
250 if (ctx->Visual->RGBAflag) {
251 GLubyte *src = (GLubyte *) pixels
252 + (skipRows * rowLength + skipPixels);
253 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
254 /* no zooming */
255 GLint row;
256 assert(drawWidth < MAX_WIDTH);
257 for (row=0; row<drawHeight; row++) {
258 GLint i;
259 for (i=0;i<drawWidth;i++) {
260 rgb[i][0] = src[i];
261 rgb[i][1] = src[i];
262 rgb[i][2] = src[i];
263 }
264 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
265 (void *) rgb, NULL);
266 src += rowLength;
267 destY++;
268 }
269 }
270 else {
271 /* with zooming */
272 GLint row;
273 assert(drawWidth < MAX_WIDTH);
274 for (row=0; row<drawHeight; row++) {
275 GLint i;
276 for (i=0;i<drawWidth;i++) {
277 rgb[i][0] = src[i];
278 rgb[i][1] = src[i];
279 rgb[i][2] = src[i];
280 }
281 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
282 zSpan, (void *) rgb, zoomY0);
283 src += rowLength;
284 destY++;
285 }
286 }
287 }
288 return GL_TRUE;
289 }
290 else if (format==GL_LUMINANCE_ALPHA && type==GL_UNSIGNED_BYTE) {
291 if (ctx->Visual->RGBAflag) {
292 GLubyte *src = (GLubyte *) pixels
293 + (skipRows * rowLength + skipPixels)*2;
294 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
295 /* no zooming */
296 GLint row;
297 assert(drawWidth < MAX_WIDTH);
298 for (row=0; row<drawHeight; row++) {
299 GLint i;
300 GLubyte *ptr = src;
301 for (i=0;i<drawWidth;i++) {
302 rgba[i][0] = *ptr;
303 rgba[i][1] = *ptr;
304 rgba[i][2] = *ptr++;
305 rgba[i][3] = *ptr++;
306 }
307 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
308 (void *) rgba, NULL);
309 src += rowLength*2;
310 destY++;
311 }
312 }
313 else {
314 /* with zooming */
315 GLint row;
316 assert(drawWidth < MAX_WIDTH);
317 for (row=0; row<drawHeight; row++) {
318 GLubyte *ptr = src;
319 GLint i;
320 for (i=0;i<drawWidth;i++) {
321 rgba[i][0] = *ptr;
322 rgba[i][1] = *ptr;
323 rgba[i][2] = *ptr++;
324 rgba[i][3] = *ptr++;
325 }
326 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
327 zSpan, (void *) rgba, zoomY0);
328 src += rowLength*2;
329 destY++;
330 }
331 }
332 }
333 return GL_TRUE;
334 }
335 else if (format==GL_COLOR_INDEX && type==GL_UNSIGNED_BYTE) {
336 GLubyte *src = (GLubyte *) pixels + skipRows * rowLength + skipPixels;
337 if (ctx->Visual->RGBAflag) {
338 /* convert CI data to RGBA */
339 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
340 /* no zooming */
341 GLint row;
342 for (row=0; row<drawHeight; row++) {
343 assert(drawWidth < MAX_WIDTH);
344 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
345 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
346 (const GLubyte (*)[4])rgba,
347 NULL);
348 src += rowLength;
349 destY++;
350 }
351 return GL_TRUE;
352 }
353 else {
354 /* with zooming */
355 GLint row;
356 for (row=0; row<drawHeight; row++) {
357 assert(drawWidth < MAX_WIDTH);
358 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
359 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
360 zSpan, (void *) rgba, zoomY0);
361 src += rowLength;
362 destY++;
363 }
364 return GL_TRUE;
365 }
366 }
367 else {
368 /* write CI data to CI frame buffer */
369 GLint row;
370 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
371 /* no zooming */
372 for (row=0; row<drawHeight; row++) {
373 (*ctx->Driver.WriteCI8Span)(ctx, drawWidth, destX, destY,
374 src, NULL);
375 src += rowLength;
376 destY++;
377 }
378 return GL_TRUE;
379 }
380 else {
381 /* with zooming */
382 return GL_FALSE;
383 }
384 }
385 }
386 else {
387 /* can't handle this pixel format and/or data type here */
388 return GL_FALSE;
389 }
390 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000391
392 /* can't do a simple draw, have to use slow path */
393 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000394}
395
396
397
398/*
399 * Do glDrawPixels of index pixels.
400 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000401static void
402draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
403 GLsizei width, GLsizei height,
404 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000405{
jtgafb833d1999-08-19 00:55:39 +0000406 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
Brian Paulfbd8f211999-11-11 01:22:25 +0000407 const GLint desty = y;
408 GLint row, drawWidth;
409 GLdepth zspan[MAX_WIDTH];
jtgafb833d1999-08-19 00:55:39 +0000410
Brian Paulfbd8f211999-11-11 01:22:25 +0000411 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000412
413 /* Fragment depth values */
Brian Paul7110c371999-11-26 16:27:05 +0000414 if (ctx->Depth.Test || ctx->Fog.Enabled) {
jtgafb833d1999-08-19 00:55:39 +0000415 GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
Brian Paulfbd8f211999-11-11 01:22:25 +0000416 GLint i;
417 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000418 zspan[i] = zval;
419 }
420 }
421
Brian Paulfbd8f211999-11-11 01:22:25 +0000422 /*
423 * General solution
424 */
425 for (row = 0; row < height; row++, y++) {
426 GLuint indexes[MAX_WIDTH];
427 const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
428 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
429 _mesa_unpack_index_span(ctx, drawWidth, GL_UNSIGNED_INT, indexes,
430 type, source, &ctx->Unpack, GL_TRUE);
431 if (zoom) {
432 gl_write_zoomed_index_span(ctx, drawWidth, x, y, zspan, indexes, desty);
jtgafb833d1999-08-19 00:55:39 +0000433 }
434 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000435 gl_write_index_span(ctx, drawWidth, x, y, zspan, indexes, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000436 }
437 }
jtgafb833d1999-08-19 00:55:39 +0000438}
439
440
441
442/*
443 * Do glDrawPixels of stencil image. The image datatype may either
444 * be GLubyte or GLbitmap.
445 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000446static void
447draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
448 GLsizei width, GLsizei height,
449 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000450{
jtgafb833d1999-08-19 00:55:39 +0000451 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
Brian Paulfbd8f211999-11-11 01:22:25 +0000452 const GLint desty = y;
453 GLint row, drawWidth;
jtgafb833d1999-08-19 00:55:39 +0000454
Brian Paulfbd8f211999-11-11 01:22:25 +0000455 if (type != GL_BYTE &&
456 type != GL_UNSIGNED_BYTE &&
457 type != GL_SHORT &&
458 type != GL_UNSIGNED_SHORT &&
459 type != GL_INT &&
460 type != GL_UNSIGNED_INT &&
461 type != GL_FLOAT &&
462 type != GL_BITMAP) {
463 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(stencil type)");
Brian Paulbc41b081999-10-19 20:33:57 +0000464 return;
465 }
466
Brian Paulfbd8f211999-11-11 01:22:25 +0000467 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000468
Brian Paulfbd8f211999-11-11 01:22:25 +0000469 for (row = 0; row < height; row++, y++) {
470 GLstencil values[MAX_WIDTH];
471 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
472 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
473 const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack,
474 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
475 _mesa_unpack_index_span(ctx, drawWidth, destType, values,
476 type, source, &ctx->Unpack, GL_TRUE);
jtgafb833d1999-08-19 00:55:39 +0000477
jtgafb833d1999-08-19 00:55:39 +0000478 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000479 gl_write_zoomed_stencil_span( ctx, (GLuint) drawWidth, x, y,
480 values, desty );
jtgafb833d1999-08-19 00:55:39 +0000481 }
482 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000483 gl_write_stencil_span( ctx, (GLuint) drawWidth, x, y, values );
jtgafb833d1999-08-19 00:55:39 +0000484 }
485 }
486}
487
488
489
490/*
491 * Do a glDrawPixels of depth values.
492 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000493static void
494draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
495 GLsizei width, GLsizei height,
496 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000497{
Brian Paulfbd8f211999-11-11 01:22:25 +0000498 const GLboolean bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
499 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
jtgafb833d1999-08-19 00:55:39 +0000500 const GLint desty = y;
501 GLubyte rgba[MAX_WIDTH][4];
502 GLuint ispan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000503 GLint drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
jtgafb833d1999-08-19 00:55:39 +0000504
Brian Paulfbd8f211999-11-11 01:22:25 +0000505 if (type != GL_UNSIGNED_BYTE
506 && type != GL_UNSIGNED_BYTE
507 && type != GL_UNSIGNED_SHORT
508 && type != GL_UNSIGNED_SHORT
509 && type != GL_UNSIGNED_INT
510 && type != GL_UNSIGNED_INT
511 && type != GL_FLOAT) {
512 gl_error(ctx, GL_INVALID_ENUM, "glDrawPixels(type)");
513 return;
514 }
jtgafb833d1999-08-19 00:55:39 +0000515
Brian Paulfbd8f211999-11-11 01:22:25 +0000516 /* Colors or indexes */
jtgafb833d1999-08-19 00:55:39 +0000517 if (ctx->Visual->RGBAflag) {
518 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0F);
519 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0F);
520 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0F);
521 GLint a = (GLint) (ctx->Current.RasterColor[3] * 255.0F);
522 GLint i;
Brian Paulfbd8f211999-11-11 01:22:25 +0000523 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000524 rgba[i][RCOMP] = r;
525 rgba[i][GCOMP] = g;
526 rgba[i][BCOMP] = b;
527 rgba[i][ACOMP] = a;
528 }
529 }
530 else {
531 GLint i;
Brian Paulfbd8f211999-11-11 01:22:25 +0000532 for (i = 0; i < drawWidth; i++) {
jtgafb833d1999-08-19 00:55:39 +0000533 ispan[i] = ctx->Current.RasterIndex;
534 }
535 }
536
Brian Paulfbd8f211999-11-11 01:22:25 +0000537 if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
jtgafb833d1999-08-19 00:55:39 +0000538 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
539 /* Special case: directly write 16-bit depth values */
Brian Paulfbd8f211999-11-11 01:22:25 +0000540 GLint row;
541 for (row = 0; row < height; row++, y++) {
542 const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
543 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
jtgafb833d1999-08-19 00:55:39 +0000544 gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
545 }
546 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000547 else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
jtgafb833d1999-08-19 00:55:39 +0000548 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
549 /* Special case: directly write 32-bit depth values */
Brian Paulfbd8f211999-11-11 01:22:25 +0000550 GLint i, row;
jtgafb833d1999-08-19 00:55:39 +0000551 /* Compute shift value to scale 32-bit uints down to depth values. */
552 GLuint shift = 0;
553 GLuint max = MAX_DEPTH;
Brian Paulfbd8f211999-11-11 01:22:25 +0000554 while ((max & 0x80000000) == 0) {
jtgafb833d1999-08-19 00:55:39 +0000555 max = max << 1;
556 shift++;
557 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000558 for (row = 0; row < height; row++, y++) {
jtgafb833d1999-08-19 00:55:39 +0000559 GLdepth zspan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000560 const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack,
561 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
jtgafb833d1999-08-19 00:55:39 +0000562 for (i=0;i<width;i++) {
563 zspan[i] = zptr[i] >> shift;
564 }
565 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
566 }
567 }
568 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000569 /* General case */
570 GLint row;
571 for (row = 0; row < height; row++, y++) {
jtgafb833d1999-08-19 00:55:39 +0000572 GLdepth zspan[MAX_WIDTH];
Brian Paulfbd8f211999-11-11 01:22:25 +0000573 const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack,
574 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
575 _mesa_unpack_depth_span( ctx, drawWidth, zspan, type, src,
576 &ctx->Unpack, GL_TRUE );
jtgafb833d1999-08-19 00:55:39 +0000577 if (ctx->Visual->RGBAflag) {
578 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000579 gl_write_zoomed_rgba_span(ctx, width, x, y, zspan,
580 (const GLubyte (*)[4])rgba, desty);
jtgafb833d1999-08-19 00:55:39 +0000581 }
582 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000583 gl_write_rgba_span(ctx, width, x, y, zspan, rgba, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000584 }
585 }
586 else {
587 if (zoom) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000588 gl_write_zoomed_index_span(ctx, width, x, y, zspan,
589 ispan, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000590 }
591 else {
Brian Paulfbd8f211999-11-11 01:22:25 +0000592 gl_write_index_span(ctx, width, x, y, zspan, ispan, GL_BITMAP);
jtgafb833d1999-08-19 00:55:39 +0000593 }
594 }
595
596 }
597 }
598}
599
600
jtgafb833d1999-08-19 00:55:39 +0000601/*
602 * Do glDrawPixels of RGBA pixels.
603 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000604static void
605draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
606 GLsizei width, GLsizei height,
607 GLenum format, GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000608{
Brian Paulfbd8f211999-11-11 01:22:25 +0000609 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
610 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
jtgafb833d1999-08-19 00:55:39 +0000611 const GLint desty = y;
612 GLdepth zspan[MAX_WIDTH];
613 GLboolean quickDraw;
jtgafb833d1999-08-19 00:55:39 +0000614
615 /* Try an optimized glDrawPixels first */
Brian Paulfbd8f211999-11-11 01:22:25 +0000616 if (simple_DrawPixels(ctx, x, y, width, height, format, type, pixels))
jtgafb833d1999-08-19 00:55:39 +0000617 return;
618
jtgafb833d1999-08-19 00:55:39 +0000619 /* Fragment depth values */
Brian Paul7110c371999-11-26 16:27:05 +0000620 if (ctx->Depth.Test || ctx->Fog.Enabled) {
jtgafb833d1999-08-19 00:55:39 +0000621 /* fill in array of z values */
622 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
Brian Paulfbd8f211999-11-11 01:22:25 +0000623 GLint i;
jtgafb833d1999-08-19 00:55:39 +0000624 for (i=0;i<width;i++) {
625 zspan[i] = z;
626 }
627 }
628
Brian Paulfbd8f211999-11-11 01:22:25 +0000629
630 if (ctx->RasterMask == 0 && !zoom
631 && x >= 0 && y >= 0
Brian Paul3f02f901999-11-24 18:48:30 +0000632 && x + width <= ctx->DrawBuffer->Width
633 && y + height <= ctx->DrawBuffer->Height) {
jtgafb833d1999-08-19 00:55:39 +0000634 quickDraw = GL_TRUE;
635 }
636 else {
637 quickDraw = GL_FALSE;
638 }
639
Brian Paulfbd8f211999-11-11 01:22:25 +0000640 /*
641 * General solution
642 */
jtgafb833d1999-08-19 00:55:39 +0000643 {
jtgafb833d1999-08-19 00:55:39 +0000644 GLubyte rgba[MAX_WIDTH][4];
Brian Paulfbd8f211999-11-11 01:22:25 +0000645 GLint row;
646 if (width > MAX_WIDTH)
647 width = MAX_WIDTH;
648 for (row = 0; row < height; row++, y++) {
649 const GLvoid *source = gl_pixel_addr_in_image(unpack,
650 pixels, width, height, format, type, 0, row, 0);
651 _mesa_unpack_ubyte_color_span(ctx, width, GL_RGBA, (void*) rgba,
652 format, type, source, unpack, GL_TRUE);
jtgafb833d1999-08-19 00:55:39 +0000653
jtgafb833d1999-08-19 00:55:39 +0000654 if (quickDraw) {
Brian Paulfbd8f211999-11-11 01:22:25 +0000655 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y,
656 (CONST GLubyte (*)[]) rgba, NULL);
jtgafb833d1999-08-19 00:55:39 +0000657 }
658 else if (zoom) {
659 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
Brian Paulfbd8f211999-11-11 01:22:25 +0000660 (CONST GLubyte (*)[]) rgba, desty );
jtgafb833d1999-08-19 00:55:39 +0000661 }
662 else {
663 gl_write_rgba_span( ctx, (GLuint) width, x, y, zspan, rgba, GL_BITMAP);
664 }
665 }
jtgafb833d1999-08-19 00:55:39 +0000666 }
667}
668
669
670
671/*
672 * Execute glDrawPixels
673 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000674void
675_mesa_DrawPixels( GLsizei width, GLsizei height,
676 GLenum format, GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +0000677{
Brian Paulfbd8f211999-11-11 01:22:25 +0000678 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000679 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawPixels");
680
jtgafb833d1999-08-19 00:55:39 +0000681 if (ctx->RenderMode==GL_RENDER) {
682 GLint x, y;
Brian Paulfbd8f211999-11-11 01:22:25 +0000683 if (!pixels || !ctx->Current.RasterPosValid) {
jtgafb833d1999-08-19 00:55:39 +0000684 return;
685 }
686
687 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
688 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
689
Brian Paul4ee75bd2000-03-03 15:39:13 +0000690 /* see if device driver can do the drawpix */
691 if (ctx->Driver.DrawPixels
692 && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
693 &ctx->Unpack, pixels)) {
694 return;
695 }
696
Brian Paulfbd8f211999-11-11 01:22:25 +0000697 switch (format) {
jtgafb833d1999-08-19 00:55:39 +0000698 case GL_STENCIL_INDEX:
Brian Paulfbd8f211999-11-11 01:22:25 +0000699 draw_stencil_pixels( ctx, x, y, width, height, type, pixels );
jtgafb833d1999-08-19 00:55:39 +0000700 break;
701 case GL_DEPTH_COMPONENT:
Brian Paulfbd8f211999-11-11 01:22:25 +0000702 draw_depth_pixels( ctx, x, y, width, height, type, pixels );
703 break;
704 case GL_COLOR_INDEX:
705 if (ctx->Visual->RGBAflag)
Brian Paulfbd8f211999-11-11 01:22:25 +0000706 draw_rgba_pixels(ctx, x,y, width, height, format, type, pixels);
Brian Paulbab8f792000-02-08 23:42:14 +0000707 else
708 draw_index_pixels(ctx, x, y, width, height, type, pixels);
jtgafb833d1999-08-19 00:55:39 +0000709 break;
710 case GL_RED:
711 case GL_GREEN:
712 case GL_BLUE:
713 case GL_ALPHA:
jtgafb833d1999-08-19 00:55:39 +0000714 case GL_LUMINANCE:
715 case GL_LUMINANCE_ALPHA:
Brian Paulfbd8f211999-11-11 01:22:25 +0000716 case GL_RGB:
717 case GL_BGR:
jtgafb833d1999-08-19 00:55:39 +0000718 case GL_RGBA:
Brian Paulfbd8f211999-11-11 01:22:25 +0000719 case GL_BGRA:
720 case GL_ABGR_EXT:
721 draw_rgba_pixels(ctx, x, y, width, height, format, type, pixels);
jtgafb833d1999-08-19 00:55:39 +0000722 break;
723 default:
Brian Paulfbd8f211999-11-11 01:22:25 +0000724 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(format)" );
jtgafb833d1999-08-19 00:55:39 +0000725 return;
726 }
727 }
728 else if (ctx->RenderMode==GL_FEEDBACK) {
729 if (ctx->Current.RasterPosValid) {
730 GLfloat color[4];
731 GLfloat texcoord[4], invq;
732 UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor);
733 invq = 1.0F / ctx->Current.Texcoord[0][3];
734 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
735 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
736 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
737 texcoord[3] = ctx->Current.Texcoord[0][3];
738 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
739 gl_feedback_vertex( ctx,
Keith Whitwell1bf9dfa1999-09-18 20:41:22 +0000740 ctx->Current.RasterPos,
jtgafb833d1999-08-19 00:55:39 +0000741 color, ctx->Current.Index, texcoord );
742 }
743 }
744 else if (ctx->RenderMode==GL_SELECT) {
745 if (ctx->Current.RasterPosValid) {
746 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
747 }
748 }
749}
750