File: tgaexample.c

package info (click to toggle)
xshipwars 1.32-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,176 kB
  • ctags: 6,357
  • sloc: ansic: 157,152; makefile: 226; sh: 75
file content (119 lines) | stat: -rw-r--r-- 2,383 bytes parent folder | download | duplicates (2)
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
/*
 *	Example on using the TGA Referance Library.
 *
 *
 *	cc tga.c tgaexample.c -o tgaexample -lX11 -L/usr/X11/lib
 */

#include <stdio.h>
#include "../include/tga.h"

#include <X11/X.h>
#include <X11/Xlib.h>

/*
#include "toggle_btn0.h"
*/


int main(int argc, char *argv[])
{
	int status;
	Display *dpy;
	Window root_w, w;
	XImage *ximage;
	unsigned int depth;
	int scr_num;
	Screen *scr_ptr;
	GC gc;
	event_t xevent;
	tga_data_struct td;

	unsigned char *img_data;
	long img_len8;


	if(argc < 2)
	{
	    fprintf(stderr, "%s: What file do you want to display?\n",
		argv[0]
	    );
	    return(1);
	}


        /* Set up X resources. */
        dpy = XOpenDisplay(NULL);
        scr_num = DefaultScreen(dpy);
        scr_ptr = DefaultScreenOfDisplay(dpy);
        depth = DefaultDepthOfScreen(scr_ptr);
        gc = DefaultGC(dpy, scr_num);
        root_w = DefaultRootWindow(dpy);


	/* Load the image. */
	status = TgaReadFromFile(argv[1], &td, depth);
/*
	status = TgaReadFromData(toggle_btn0, &td, depth);
*/
	if(status != TgaSuccess)
	{
            fprintf(stderr,
		"%s: Cannot read image.\n",
                argv[1]
            );
	    XCloseDisplay(dpy); dpy = NULL;
	    return(1);
	}

	/* Create a segment of data to copy the loaded data to. */
	img_len8 = (long)td.width * (long)td.height *
	    (long)td.data_depth / 8;
	img_data = (unsigned char *)calloc(1, img_len8 * sizeof(unsigned char));
	memcpy(img_data, td.data, img_len8);

	w = XCreateSimpleWindow(dpy, root_w, 0, 0, td.width, td.height, 0,
	    BlackPixel(dpy, scr_num), BlackPixel(dpy, scr_num));
	XSelectInput(dpy, w, KeyPressMask | ButtonReleaseMask
            | ExposureMask);


	ximage = XCreateImage(
	    dpy,
	    DefaultVisual(dpy, scr_num),
	    depth,
	    ZPixmap,
	    0,
	    (char *)img_data,
	    td.width, td.height,
	    td.data_depth,
	    0
	);
	XMapRaised(dpy, w);

	/* We don't need any tga data after this point. */
        TgaDestroyData(&td);

        XPutImage(dpy, (Drawable)w, gc, ximage, 0, 0, 0, 0, ximage->width,
            ximage->height);

	while(1)
	{
	    XNextEvent(dpy, &xevent);

	    XPutImage(dpy, (Drawable)w, gc, ximage, 0, 0, 0, 0, ximage->width,
	        ximage->height);

	    if( (xevent.type == KeyPress) ||
                (xevent.type == ButtonRelease)
            )
		break;
	}


	XDestroyImage(ximage);
	XCloseDisplay(dpy); dpy = NULL;


	return(0);
}