1 /* minirgb -- simple rgb buffer drawing library for 16, 24 and 32 bit displays
2 * Copyright (C) 2000 timecop@japan.co.jp
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <X11/Xutil.h>
25 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
28 Display *minirgb_display; /* display */
29 Visual *minirgb_visual; /* visual */
30 int minirgb_depth; /* display depth */
31 int minirgb_ok = 0; /* init ok? */
33 /* local prototypes */
34 static __inline void rgb_conv_16(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image);
35 static __inline void rgb_conv_24(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image);
36 static __inline void rgb_conv_32(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image);
38 static __inline void rgb_conv_16(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image)
40 if (image->green_mask & 0x7e0) /* 565 */
41 *(unsigned short int *)ptr = ((r >> 3) << 11) |
44 else /* 555 untested */
45 *(unsigned short int *)ptr = ((r >> 3) << 11) |
50 static __inline void rgb_conv_24(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image)
58 static __inline void rgb_conv_32(unsigned char *ptr, unsigned char r, unsigned char g, unsigned char b, XImage *image)
60 if (image->red_mask & 0xff0000) /* 0888 */
61 *(unsigned int *)ptr = (r << 16) |
64 else /* 8880 untested */
65 *(unsigned int *)ptr = (r << 24) |
70 int minirgb_init(Display *d)
75 screen = DefaultScreen(minirgb_display);
76 minirgb_visual = XDefaultVisual(minirgb_display, screen);
77 minirgb_depth = XDefaultDepth(minirgb_display, screen);
78 if (minirgb_depth <= 8) {
79 fprintf(stderr, "minirgb: No support for 8 bit displays\n");
88 void minirgb_setpixel(MiniRGB *rgb_image, unsigned char *color, unsigned int x, unsigned int y)
92 ptr = rgb_image->mem + (y * rgb_image->rowstride * 3) + (x * 3);
98 void minirgb_copy(MiniRGB *from, MiniRGB *to,
99 unsigned int sx, unsigned int sy, unsigned int w, unsigned int h,
100 unsigned int dx, unsigned int dy)
103 unsigned char *src, *dest;
106 fprintf(stderr, "minirgb: copy %d, %d (%dx%d) -> %d, %d)\n", sx, sy, w, h, dx, dy);
110 if (w == 1) { /* special case - dot operations */
111 src = from->mem + (sy * from->rowstride * 3) + sx * 3;
112 dest = to->mem + (dy * to->rowstride * 3) + dx * 3;
113 dest[0] = dest[0] & src[0];
114 dest[1] = dest[1] & src[1];
115 dest[2] = dest[2] & src[2];
120 for (i = 0; i < h; i++) {
121 src = from->mem + ((sy + i) * from->rowstride * 3) + sx * 3;
122 dest = to->mem + ((dy + i) * to->rowstride * 3) + dx * 3;
123 memcpy(dest, src, w * 3);
127 void minirgb_new(MiniRGB *image, unsigned int width, unsigned int height)
129 image->width = width;
130 image->height = height;
131 image->rowstride = width;
132 image->mem = calloc(1, width * height * 3);
135 void minirgb_draw(Window drawable, GC gc, int x, int y, int width,
136 int height, MiniRGB *rgb_image)
138 XImage *image = NULL;
141 unsigned char *ptr, *col;
142 void (*conv) (unsigned char *, unsigned char, unsigned char, unsigned char, XImage *) = NULL;
147 image = XCreateImage(minirgb_display, minirgb_visual, minirgb_depth, ZPixmap, 0, 0, width, height, 32, 0);
148 bpp = (image->bits_per_pixel + 7) / 8;
161 printf("minirgb: image %p %dx%d (bpp: %d, bpl: %d)\n", image, width, height, bpp, image->bytes_per_line);
163 image->data = malloc(image->bytes_per_line * height);
165 fprintf(stderr, "minirgb: allocation error\n");
166 XDestroyImage(image);
170 for (y0 = 0; y0 < height; y0++) {
171 for (x0 = 0; x0 < width; x0++) {
172 col = rgb_image->mem + (y0 * rgb_image->rowstride * 3 + x0 * 3);
173 ptr = image->data + (y0 * image->bytes_per_line + x0 * bpp);
174 conv(ptr, col[0], col[1], col[2], image);
177 /* draw image onto drawable */
178 XPutImage(minirgb_display, drawable, gc, image, 0, 0, x, y, width, height);
179 XDestroyImage(image);