Related
I need to do some math on the pixels of a GL texture. I have a working OpenCL kernel. I can convert the GL texture to an OpenCL buffer (at least, it doesn't give an error). But when I try to set that buffer as an argument to the kernel I get error -38 (CL_INVALID_MEM_OBJECT).
I originally tried it using rust, but when that was failing, I switched to C just to see if the problem existed independent of rust's wrappers.
This is my C app that is duct taped together from a couple of examples (c++ -g -o check2 check2.cxx -lOpenCL -lGL -lX11):
/*
https://www.khronos.org/opengl/wiki/Tutorial:_OpenGL_3.0_Context_Creation_(GLX)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <CL/cl.h>
#include <CL/cl_gl.h>
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
// Helper to check for extension string presence. Adapted from:
// http://www.opengl.org/resources/features/OGLextensions/
static bool isExtensionSupported(const char *extList, const char *extension)
{
const char *start;
const char *where, *terminator;
/* Extension names should not have spaces. */
where = strchr(extension, ' ');
if (where || *extension == '\0')
return false;
/* It takes a bit of care to be fool-proof about parsing the
OpenGL extensions string. Don't be fooled by sub-strings,
etc. */
for (start=extList;;) {
where = strstr(start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if ( where == start || *(where - 1) == ' ' )
if ( *terminator == ' ' || *terminator == '\0' )
return true;
start = terminator;
}
return false;
}
static bool ctxErrorOccurred = false;
static int ctxErrorHandler( Display *dpy, XErrorEvent *ev )
{
ctxErrorOccurred = true;
return 0;
}
void test_kernel(cl_mem a_mem_obj, cl_mem b_mem_obj, cl_mem c_mem_obj, cl_kernel kernel, int LIST_SIZE, cl_command_queue command_queue)
{
int err;
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
printf("err? %d\tset kernel arg 0\n", err);
err = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
printf("err? %d\n", err);
err = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
printf("err? %d\n", err);
// Execute the OpenCL kernel on the list
size_t global_item_size = LIST_SIZE; // Process the entire lists
size_t local_item_size = 64; // Divide work items into groups of 64
err = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
printf("err? %d\t(kernel diff)\n", err);
// Read the memory buffer C on the device to the local variable C
long long *C;
C= (long long*)malloc(sizeof(*C)*LIST_SIZE);
err = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0,
LIST_SIZE * sizeof(*C), C, 0, NULL, NULL);
printf("err? %d(copy result)\n", err);
for (int i=0; i<LIST_SIZE && i<10; i++) {
printf("%lld\n", C[i]);
}
free(C);
}
int main(int argc, char* argv[])
{
Display *display = XOpenDisplay(NULL);
if (!display)
{
printf("Failed to open X display\n");
exit(1);
}
// Get a matching FB config
static int visual_attribs[] =
{
GLX_X_RENDERABLE , True,
GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
GLX_RENDER_TYPE , GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
GLX_RED_SIZE , 8,
GLX_GREEN_SIZE , 8,
GLX_BLUE_SIZE , 8,
GLX_ALPHA_SIZE , 8,
GLX_DEPTH_SIZE , 24,
GLX_STENCIL_SIZE , 8,
GLX_DOUBLEBUFFER , True,
//GLX_SAMPLE_BUFFERS , 1,
//GLX_SAMPLES , 4,
None
};
int glx_major, glx_minor;
// FBConfigs were added in GLX version 1.3.
if ( !glXQueryVersion( display, &glx_major, &glx_minor ) ||
( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) )
{
printf("Invalid GLX version");
exit(1);
}
printf( "Getting matching framebuffer configs\n" );
int fbcount;
GLXFBConfig* fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount);
if (!fbc)
{
printf( "Failed to retrieve a framebuffer config\n" );
exit(1);
}
printf( "Found %d matching FB configs.\n", fbcount );
// Pick the FB config/visual with the most samples per pixel
printf( "Getting XVisualInfos\n" );
int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
int i;
for (i=0; i<fbcount; ++i)
{
XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
if ( vi )
{
int samp_buf, samples;
glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES , &samples );
printf( " Matching fbconfig %d, visual ID 0x%2lx: SAMPLE_BUFFERS = %d,"
" SAMPLES = %d\n",
i, vi -> visualid, samp_buf, samples );
if ( best_fbc < 0 || samp_buf && samples > best_num_samp )
best_fbc = i, best_num_samp = samples;
if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp )
worst_fbc = i, worst_num_samp = samples;
}
XFree( vi );
}
GLXFBConfig bestFbc = fbc[ best_fbc ];
// Be sure to free the FBConfig list allocated by glXChooseFBConfig()
XFree( fbc );
#if 0
// Get a visual
XVisualInfo *vi = glXGetVisualFromFBConfig( display, bestFbc );
printf( "Chosen visual ID = 0x%lx\n", vi->visualid );
printf( "Creating colormap\n" );
XSetWindowAttributes swa;
Colormap cmap;
swa.colormap = cmap = XCreateColormap( display,
RootWindow( display, vi->screen ),
vi->visual, AllocNone );
swa.background_pixmap = None ;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
printf( "Creating window\n" );
Window win = XCreateWindow( display, RootWindow( display, vi->screen ),
0, 0, 100, 100, 0, vi->depth, InputOutput,
vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa );
if ( !win )
{
printf( "Failed to create window.\n" );
exit(1);
}
// Done with the visual info data
XFree( vi );
XStoreName( display, win, "GL 3.0 Window" );
printf( "Mapping window\n" );
XMapWindow( display, win );
#endif
// Get the default screen's GLX extension list
const char *glxExts = glXQueryExtensionsString( display,
DefaultScreen( display ) );
// NOTE: It is not necessary to create or make current to a context before
// calling glXGetProcAddressARB
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
GLXContext ctx = 0;
// Install an X error handler so the application won't exit if GL 3.0
// context allocation fails.
//
// Note this error handler is global. All display connections in all threads
// of a process use the same error handler, so be sure to guard against other
// threads issuing X commands while this code is running.
ctxErrorOccurred = false;
int (*oldHandler)(Display*, XErrorEvent*) =
XSetErrorHandler(&ctxErrorHandler);
// Check for the GLX_ARB_create_context extension string and the function.
// If either is not present, use GLX 1.3 context creation method.
if ( !isExtensionSupported( glxExts, "GLX_ARB_create_context" ) ||
!glXCreateContextAttribsARB )
{
printf( "glXCreateContextAttribsARB() not found"
" ... using old-style GLX context\n" );
ctx = glXCreateNewContext( display, bestFbc, GLX_RGBA_TYPE, 0, True );
}
// If it does, try to get a GL 3.0 context!
else
{
int context_attribs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
//GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
None
};
printf( "Creating context\n" );
ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
True, context_attribs );
// Sync to ensure any errors generated are processed.
XSync( display, False );
if ( !ctxErrorOccurred && ctx )
printf( "Created GL 3.0 context\n" );
else
{
// Couldn't create GL 3.0 context. Fall back to old-style 2.x context.
// When a context version below 3.0 is requested, implementations will
// return the newest context version compatible with OpenGL versions less
// than version 3.0.
// GLX_CONTEXT_MAJOR_VERSION_ARB = 1
context_attribs[1] = 1;
// GLX_CONTEXT_MINOR_VERSION_ARB = 0
context_attribs[3] = 0;
ctxErrorOccurred = false;
printf( "Failed to create GL 3.0 context"
" ... using old-style GLX context\n" );
ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
True, context_attribs );
}
}
// Sync to ensure any errors generated are processed.
XSync( display, False );
// Restore the original error handler
XSetErrorHandler( oldHandler );
if ( ctxErrorOccurred || !ctx )
{
printf( "Failed to create an OpenGL context\n" );
exit(1);
}
// Verifying that context is a direct context
if ( ! glXIsDirect ( display, ctx ) )
{
printf( "Indirect GLX rendering context obtained\n" );
}
else
{
printf( "Direct GLX rendering context obtained\n" );
}
printf( "Making context current\n" );
glXMakeCurrent( display, 0, ctx );
#define IMAGE_DIAM 512
#define LIST_SIZE (IMAGE_DIAM*IMAGE_DIAM*3)
GLuint texture_id;
{
glGenTextures(1, &texture_id);
printf("err? %d\n", glGetError());
glBindTexture(GL_TEXTURE_2D, texture_id);
printf("err? %d\t(bind texture)\n", glGetError());
unsigned char random_stack_crap[LIST_SIZE];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, IMAGE_DIAM, IMAGE_DIAM, 0, GL_RGB, GL_UNSIGNED_BYTE, random_stack_crap);
printf("err? %d\n", glGetError());
}
//
//
//
cl_platform_id platform;
clGetPlatformIDs(1, &platform, NULL);
cl_device_id device;
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
printf("device\t%p\n", device);
printf ("context\t%p\t%p\n", ctx, glXGetCurrentContext());
printf ("display\t%p\t%p\n", display, glXGetCurrentDisplay());
cl_context_properties props[] =
{
CL_GL_CONTEXT_KHR,
(cl_context_properties) glXGetCurrentContext(),
CL_GLX_DISPLAY_KHR,
(cl_context_properties) glXGetCurrentDisplay(),
CL_CONTEXT_PLATFORM,
(cl_context_properties) platform,
0
};
int err=0;
cl_context cl = clCreateContext(props, 1, &device, NULL, NULL, &err);
printf("err? %d\n", err);
printf("cl context %p\n", cl);
//
// https://www.eriksmistad.no/getting-started-with-opencl-and-gpu-computing/
//
cl_command_queue command_queue = clCreateCommandQueue(cl, device, 0, &err);
printf("err? %d\n", err);
printf("cl queue %p\n", command_queue);
// Create memory buffers on the device for each vector
cl_mem z_mem_obj = clCreateFromGLTexture( cl,
CL_MEM_READ_WRITE,
GL_TEXTURE_2D,
0,
texture_id,
&err);
cl_mem a_mem_obj = clCreateBuffer(cl, CL_MEM_READ_ONLY,
LIST_SIZE , NULL, &err);
printf("err? %d\n", err);
cl_mem b_mem_obj = clCreateBuffer(cl, CL_MEM_READ_ONLY,
LIST_SIZE , NULL, &err);
printf("err? %d\n", err);
cl_mem c_mem_obj = clCreateBuffer(cl, CL_MEM_WRITE_ONLY,
LIST_SIZE*8 , NULL, &err);
printf("err? %d\n", err);
// Copy the lists A and B to their respective memory buffers
{
unsigned char *A = (unsigned char*) malloc(LIST_SIZE);
for (int i=0; i<LIST_SIZE; i++) {
A[i] = i+2;
}
unsigned char *B = (unsigned char*) malloc(LIST_SIZE);
for (int i=0; i<LIST_SIZE; i++) {
B[i] = 2*i;
}
err = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,
LIST_SIZE, A, 0, NULL, NULL);
printf("err? %d\tcopy A\n", err);
err = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0,
LIST_SIZE, B, 0, NULL, NULL);
printf("err? %d\tcopy B\n", err);
}
const char* source_str = "__kernel void diff(__global uchar* rgb_a, __global uchar* rgb_b, __global ulong * diff_out)\n\
{\n\
int idx = get_global_id(0);\n\
diff_out[idx] = abs((int)rgb_a[idx] - (int)rgb_b[idx]);\n\
}";
size_t source_size = strlen(source_str);
// Create a program from the kernel source
cl_program program = clCreateProgramWithSource(cl, 1,
(const char **)&source_str, (const size_t *)&source_size, &err);
printf("err? %d\t(create program)\n", err);
// Build the program
err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
printf("err? %d\n", err);
// Create the OpenCL kernel
cl_kernel kernel = clCreateKernel(program, "diff", &err);
printf("err? %d\n", err);
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
printf("err? %d\n", err);
err = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
printf("err? %d\n", err);
err = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
printf("err? %d\n", err);
// Execute the OpenCL kernel on the list
size_t global_item_size = LIST_SIZE; // Process the entire lists
size_t local_item_size = 64; // Divide work items into groups of 64
err = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
printf("err? %d\t(kernel diff)\n", err);
// Read the memory buffer C on the device to the local variable C
long long *C;
C= (long long*)malloc(sizeof(*C)*LIST_SIZE);
err = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0,
LIST_SIZE * sizeof(*C), C, 0, NULL, NULL);
printf("err? %d(copy resul)\n", err);
for (int i=0; i<LIST_SIZE && i<10; i++) {
printf("%lld\n", C[i]);
}
free(C);
test_kernel(a_mem_obj, b_mem_obj, c_mem_obj, kernel, LIST_SIZE, command_queue);
test_kernel(z_mem_obj, b_mem_obj, c_mem_obj, kernel, LIST_SIZE, command_queue);
return 0;
}
The output on my linux/gentoo box with mesa-progs-8.4.0 is
Getting matching framebuffer configs
Found 10 matching FB configs.
Getting XVisualInfos
Matching fbconfig 0, visual ID 0x24: SAMPLE_BUFFERS = 0, SAMPLES = 0
Matching fbconfig 1, visual ID 0x7a: SAMPLE_BUFFERS = 0, SAMPLES = 0
Matching fbconfig 2, visual ID 0x38: SAMPLE_BUFFERS = 1, SAMPLES = 2
Matching fbconfig 3, visual ID 0x8e: SAMPLE_BUFFERS = 1, SAMPLES = 2
Matching fbconfig 4, visual ID 0x3a: SAMPLE_BUFFERS = 1, SAMPLES = 4
Matching fbconfig 5, visual ID 0x90: SAMPLE_BUFFERS = 1, SAMPLES = 4
Matching fbconfig 6, visual ID 0x44: SAMPLE_BUFFERS = 1, SAMPLES = 8
Matching fbconfig 7, visual ID 0x9a: SAMPLE_BUFFERS = 1, SAMPLES = 8
Matching fbconfig 8, visual ID 0x4c: SAMPLE_BUFFERS = 1, SAMPLES = 16
Matching fbconfig 9, visual ID 0xa2: SAMPLE_BUFFERS = 1, SAMPLES = 16
Creating context
Created GL 3.0 context
Direct GLX rendering context obtained
Making context current
err? 0
err? 0 (bind texture)
err? 0
device 0x557c1755cef0
context 0x557c173e0b68 0x557c173e0b68
display 0x557c172ee950 0x557c172ee950
err? 0
cl context 0x557c17559340
err? 0
cl queue 0x557c177dd080
err? 0
err? 0
err? 0
err? 0 copy A
err? 0 copy B
err? 0 (create program)
err? 0
err? 0
err? 0
err? 0
err? 0
err? 0 (kernel diff)
err? 0(copy resul)
2
1
0
1
2
3
4
5
6
7
err? 0 set kernel arg 0
err? 0
err? 0
err? 0 (kernel diff)
err? 0(copy result)
2
1
0
1
2
3
4
5
6
7
err? -38 set kernel arg 0
err? 0
err? 0
err? 0 (kernel diff)
err? 0(copy result)
2
1
0
1
2
3
4
5
6
7
Notice the err? -38 set kernel arg 0 which corresponds to the final test where I pass z_mem_obj to use the buffer converted from a GL texture.
clinfo | grep khr lists cl_khr_gl_sharing in the Platform Extensions and Device Extensions.
So I am working on a project in C on the Raspberry Pi Model 3B+ and I am using GTK to draw. I have GTK on its own thread and my program logic on its own thread.
Basically, my program takes pictures from 3 DSLRs using gphoto2 and saves them to jpgs, then GTK loads those JPGs, resizes them to 500x300 with a pixbuf and updates the picture in the GUI
The problem I am having, is that the GTK window will fail to redraw and it is very inconsistent when, sometimes it will happen after 11 pictures are taken, sometimes it will happen after even 2 or 3 pictures are taken, there is no consistent fault.
Any help would be appreciated, one thread just initializes gtk and then calls GTK main to do the GUI, and the other does my program logic, updates the jpg files and then updates the GTK objects for the gtk main to redraw. Maybe this has something to do with my thread updating the gtk objects at a bad time and the loop running gtk main crashes and halts??
My original GUI looks like this:
The black boxes arent the problem, those are just the default blank pixbufs before any pictures have been taken. The problem is that it will fail and the image will just turn to the background color (see on the grid "CAM2 Front") and then after this call the whole window glitches and does nothing. I can drag it around and it will draw stuff I drag it over but thats it.
I know it is the whole app that is failing to draw because I am getting results like this when I move the window around.
Any help is super appreciated. I have been playing around with this for a while now.
This is the code called by my program logic main thread loop to update the image to the new jpg taken:
void set_image_scaled(GtkWidget* img, const char* path) {
pixbuf = gdk_pixbuf_new_from_file(path, &err);
g_assert_no_error(err);
pxbscaled = gdk_pixbuf_scale_simple(pixbuf, 500, 300, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(img), pxbscaled);
g_object_unref(pixbuf);
g_object_unref(pxbscaled);
}
This is the main logic thread that updates the GtkImage object (calls set_image_scaled()):
void* cam_main() {
while (running == 1) {
if (digitalRead(4) == 0)
{
printf("taking pics of %s item %i\n", (front == 1) ? "front" : "back", pic);
for (int i = 0; i < count; i++)
{
int fd, retval;
CameraFile *file;
CameraFilePath cfPath;
strcpy(cfPath.folder, "/");
strcpy(cfPath.name, "foo1.jpg");
printf("Capturing cam%i...\n", i + 1);
int res = gp_camera_capture(cams[i], GP_CAPTURE_IMAGE, &cfPath, context);
//printf(gp_port_result_as_string(res));
printf("capture result: %i\n", res);
//Camera won't take pic if busy and will continue to program end
char buf[256];
snprintf(buf, sizeof(buf), "%s/cam-%i_%i_%s.jpg", workingDir, i + 1, pic, (front == 1) ? "a_front" : "b_back"); //a_ to make front come before back otherwise systems will order incorrectly
fd = open(buf, O_CREAT | O_WRONLY, 0644);
retval = gp_file_new_from_fd(&file, fd);
retval = gp_camera_file_get(cams[i], cfPath.folder, cfPath.name, GP_FILE_TYPE_NORMAL, file, context);
retval = gp_camera_file_delete(cams[i], cfPath.folder, cfPath.name, context);
gp_file_free(file);
//debug
//if (front == 1 && i == 0)
//set_image_scaled(front_cams[0], buf);
if (front == 1)
set_image_scaled(front_cams[i], buf);
else
set_image_scaled(back_cams[i], buf);
}
if (front == 1)
front = 0;
else
{
front = 1;
pic += 1;
}
printf("pics taken...\n");
}
}
}
My whole code for reference:
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <gphoto2/gphoto2-camera.h>
#include <gphoto2/gphoto2-context.h>
#include <wiringPi.h>
#include <gtk/gtk.h>
#include <pthread.h>
//program
static int running = 1;
//libgphoto2
static CameraList* list;
static Camera** cams;
static GPContext* context;
static const char *name, *value;
static int ret, count;
static int pic = 0;
static int front = 1;
static const char* workingDir;
//GTK
static GtkWidget *window, *vbox,*hboxDir, *hboxCamLabels, *hboxFrontPics, *hboxBackPics, *hboxStatus, *lblDir, *btnConfigDir, *lblCams, *lblFront, *lblBack, *lblCurrentStatus;
static GtkWidget *front_cams[3];
static GtkWidget *back_cams[3];
static GdkPixbuf *pxbscaled = NULL;
static GdkPixbuf *pixbuf = NULL;
static GError* err = NULL;
static void btnConfigDir_ConfigureDirectory(GtkWidget* widget, gpointer data) {
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;
dialog = gtk_file_chooser_dialog_new ("Open File", window,
action,
"_Cancel",
GTK_RESPONSE_CANCEL,
"_Open",
GTK_RESPONSE_ACCEPT,
NULL);
res = gtk_dialog_run (GTK_DIALOG (dialog));
if (res == GTK_RESPONSE_ACCEPT)
{
char *filename;
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
workingDir = gtk_file_chooser_get_filename (chooser);
}
//gtk_label_set_text((GTK_LABEL)lblDir, workingDir);
gtk_widget_destroy (dialog);
}
void* main_gtk() {
//INIT GTK
//SETUP WINDOW
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//button1=gtk_button_new_with_label("Click me");
//gtk_button_set_label(GTK_BUTTON(button1), "click me 1");
g_signal_connect(window,"delete-event", G_CALLBACK(gtk_main_quit), NULL);
workingDir = "/media/pi/SD CARD";
lblDir = gtk_label_new("Save to: dir\t");
btnConfigDir = gtk_button_new_with_label("Configure Directory");
g_signal_connect(btnConfigDir, "clicked", G_CALLBACK(btnConfigDir_ConfigureDirectory), NULL);
lblCams = gtk_label_new("\t\t\t\t\t\t\t\tCAM 1\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCAM2\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCAM3");
lblFront = gtk_label_new("Front");
lblBack = gtk_label_new("Back ");
lblCurrentStatus = gtk_label_new("Current Status: Idle");
//SET WINDOW SIZE AND TITLE
//gtk_widget_set_size_request(window, 600, 400);
gtk_window_set_title(GTK_WINDOW(window), "CaptureGui");
//RESIZE IMAGES
//image
front_cams[0] = gtk_image_new();
front_cams[1] = gtk_image_new();
front_cams[2] = gtk_image_new();
back_cams[0] = gtk_image_new();
back_cams[1] = gtk_image_new();
back_cams[2] = gtk_image_new();
GdkPixbuf* pxb;
pxb = gdk_pixbuf_new(0, 0, 8, 500, 300);
gtk_image_set_from_pixbuf(GTK_IMAGE(front_cams[0]), pxb);
gtk_image_set_from_pixbuf(GTK_IMAGE(front_cams[1]), pxb);
gtk_image_set_from_pixbuf(GTK_IMAGE(front_cams[2]), pxb);
gtk_image_set_from_pixbuf(GTK_IMAGE(back_cams[0]), pxb);
gtk_image_set_from_pixbuf(GTK_IMAGE(back_cams[1]), pxb);
gtk_image_set_from_pixbuf(GTK_IMAGE(back_cams[2]), pxb);
//g_print("1");
//front_cams[0] = gtk_image_new_from_file("/media/pi/SD CARD/cam-1_0_a_front.jpg");
//set_image_scaled(front_cams[0], "/media/pi/SD CARD/cam-1_1_a_front.jpg");
//set_image_scaled(front_cams[1], "/media/pi/SD CARD/cam-1_1_a_front.jpg");
//set_image_scaled(front_cams[2], "/media/pi/SD CARD/cam-1_1_b_back.jpg");
//PACK
hboxDir = gtk_box_new(0, 0);
hboxCamLabels = gtk_box_new(0, 0);
hboxFrontPics = gtk_box_new(0, 0);
hboxBackPics = gtk_box_new(0, 0);
hboxStatus = gtk_box_new(0, 0);
vbox = gtk_vbox_new(0, 10);
gtk_box_pack_end(GTK_BOX(hboxDir), btnConfigDir, 0, 0, 0);
gtk_box_pack_end(GTK_BOX(hboxDir), lblDir, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(hboxCamLabels), lblCams, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(hboxFrontPics), lblFront, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(hboxFrontPics), front_cams[0], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxFrontPics), front_cams[1], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxFrontPics), front_cams[2], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxBackPics), lblBack, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(hboxBackPics), back_cams[0], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxBackPics), back_cams[1], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxBackPics), back_cams[2], 0, 0, 10);
gtk_box_pack_start(GTK_BOX(hboxStatus), lblCurrentStatus, 0, 0, 10);
gtk_box_pack_start(GTK_BOX(vbox), hboxDir, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox), hboxCamLabels, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox), hboxFrontPics, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox), hboxBackPics, 0, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox), hboxStatus, 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), front_cams[0], 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), front_cams[1], 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), front_cams[2], 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), back_cams[0], 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), back_cams[1], 0, 0, 0);
//gtk_box_pack_start(GTK_BOX(hbox), back_cams[2], 0, 0, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
//ADD ELEMENTS TO GUI
//gtk_container_add(GTK_CONTAINER(window), image1);
//gtk_container_add(GTK_CONTAINER(window), image2);
//image2 = gtk_image_new_from_file("/media/pi/SD CARD/cam-1_0_b_back.jpg");
//gtk_container_add(GTK_CONTAINER(window), image2);
//SHOW GUI
gtk_widget_show_all(window);
gtk_main();
}
void set_image_scaled(GtkWidget* img, const char* path) {
pixbuf = gdk_pixbuf_new_from_file(path, &err);
g_assert_no_error(err);
pxbscaled = gdk_pixbuf_scale_simple(pixbuf, 500, 300, GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(img), pxbscaled);
g_object_unref(pixbuf);
g_object_unref(pxbscaled);
}
void* cam_main() {
while (running == 1) {
if (digitalRead(4) == 0)
{
printf("taking pics of %s item %i\n", (front == 1) ? "front" : "back", pic);
for (int i = 0; i < count; i++)
{
int fd, retval;
CameraFile *file;
CameraFilePath cfPath;
strcpy(cfPath.folder, "/");
strcpy(cfPath.name, "foo1.jpg");
printf("Capturing cam%i...\n", i + 1);
int res = gp_camera_capture(cams[i], GP_CAPTURE_IMAGE, &cfPath, context);
//printf(gp_port_result_as_string(res));
printf("capture result: %i\n", res);
//Camera won't take pic if busy and will continue to program end
char buf[256];
snprintf(buf, sizeof(buf), "%s/cam-%i_%i_%s.jpg", workingDir, i + 1, pic, (front == 1) ? "a_front" : "b_back"); //a_ to make front come before back otherwise systems will order incorrectly
fd = open(buf, O_CREAT | O_WRONLY, 0644);
retval = gp_file_new_from_fd(&file, fd);
retval = gp_camera_file_get(cams[i], cfPath.folder, cfPath.name, GP_FILE_TYPE_NORMAL, file, context);
retval = gp_camera_file_delete(cams[i], cfPath.folder, cfPath.name, context);
gp_file_free(file);
//debug
//if (front == 1 && i == 0)
//set_image_scaled(front_cams[0], buf);
if (front == 1)
set_image_scaled(front_cams[i], buf);
else
set_image_scaled(back_cams[i], buf);
}
if (front == 1)
front = 0;
else
{
front = 1;
pic += 1;
}
printf("pics taken...\n");
}
}
}
int main(int argc, char **argv)
{
//Kill any processes using cams
system("pkill -f gphoto2");
//main_gtk();
gtk_init(&argc, &argv);
//Wiring pi init
wiringPiSetupGpio();
//Init
context = gp_context_new();
detect_cams();
pthread_t logic_thread_handle, gui_thread_handle;
pthread_create(&logic_thread_handle, NULL, cam_main, NULL);
pthread_create(&gui_thread_handle, NULL, main_gtk, NULL);
pthread_join(gui_thread_handle, 0);
pthread_join(logic_thread_handle, 0);
//Deinit
for (int i = 0; i < count; i++)
{
gp_camera_exit(cams[i], context);
gp_camera_free(cams[i]);
}
return 0;
}
void detect_cams() {
//Detecting all cameras and loading them into mem
//Detecting all cameras
ret = gp_list_new(&list);
if (ret < GP_OK) return 1;
gp_list_reset(list);
count = gp_camera_autodetect(list, context);
if (count < 1)
{
printf("No cameras detected.\n");
return 1;
}
//Open all cameras
printf("Number of cameras: %d\n", count);
cams = calloc(sizeof (Camera*), count);
for (int i = 0; i < count; i++)
{
gp_list_get_name(list, i, &name);
gp_list_get_value(list, i, &value);
ret = open_cam(&cams[i], name, value, context);
if (ret < GP_OK)
fprintf(stderr, "Camera %s on port %s failed to open\n", name, value);
}
}
int open_cam(Camera ** camera, const char *model, const char *port, GPContext *context) {
GPPortInfoList *portinfolist = NULL;
CameraAbilitiesList *abilities = NULL;
int ret, m, p;
CameraAbilities a;
GPPortInfo pi;
ret = gp_camera_new (camera);
if (ret < GP_OK) return ret;
if (!abilities) {
/* Load all the camera drivers we have... */
ret = gp_abilities_list_new (&abilities);
if (ret < GP_OK) return ret;
ret = gp_abilities_list_load (abilities, context);
if (ret < GP_OK) return ret;
}
/* First lookup the model / driver */
m = gp_abilities_list_lookup_model (abilities, model);
if (m < GP_OK) return ret;
ret = gp_abilities_list_get_abilities (abilities, m, &a);
if (ret < GP_OK) return ret;
ret = gp_camera_set_abilities (*camera, a);
if (ret < GP_OK) return ret;
if (!portinfolist) {
/* Load all the port drivers we have... */
ret = gp_port_info_list_new (&portinfolist);
if (ret < GP_OK) return ret;
ret = gp_port_info_list_load (portinfolist);
if (ret < 0) return ret;
ret = gp_port_info_list_count (portinfolist);
if (ret < 0) return ret;
}
/* Then associate the camera with the specified port */
p = gp_port_info_list_lookup_path (portinfolist, port);
switch (p) {
case GP_ERROR_UNKNOWN_PORT:
fprintf (stderr, "The port you specified "
"('%s') can not be found. Please "
"specify one of the ports found by "
"'gphoto2 --list-ports' and make "
"sure the spelling is correct "
"(i.e. with prefix 'serial:' or 'usb:').",
port);
break;
default:
break;
}
if (p < GP_OK) return p;
ret = gp_port_info_list_get_info (portinfolist, p, &pi);
if (ret < GP_OK) return ret;
ret = gp_camera_set_port_info (*camera, pi);
if (ret < GP_OK) return ret;
return GP_OK;
}
You cannot use GTK from different threads; only the thread that calls gtk_init() and gtk_main() can operate on GTK (and GDK) API.
The appropriate way to use threads within a GTK application is to use a worker thread for your long-running, blocking code, and whenever you need to update the GUI, use g_main_context_invoke() with a NULL main context.
Alternatively, you can use GTask to create your own asynchronous task running in a thread, and update the GUI once the task terminates.
In your example code, you should only ever call set_image_scaled() through g_main_context_invoke(), and you should not put the GUI inside its own thread.
I have written a kernel, which should be doing nothing, except from adding an one to each component of a float3:
__kernel void GetCellIndex(__global Particle* particles) {
int globalID = get_global_id(0);
particles[globalID].position.x += 1;
particles[globalID].position.y += 1;
particles[globalID].position.z += 1;
};
with following struct (in the kernel)
typedef struct _Particle
{
cl_float3 position;
}Particle;
my problem is, that when i write my array of particles to the GPU, every component is zero. here is the neccassary code:
(Particle*) particles = new Particle[200];
for (int i = 0; i < 200; i++)
{
particles[i].position.x = 5f;
}
cl_Particles = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(Particle)*200, NULL, &err);
if (err != 0)
{
std::cout << "CreateBuffer does not work!" << std::endl;
system("Pause");
}
clEnqueueWriteBuffer(queue, cl_Particles, CL_TRUE, 0, sizeof(Particle) * 200, &particles, 0, NULL, NULL);
//init of kernel etc.
err = clSetKernelArg(kernel, 0, sizeof(Particle) * 200, &cl_Particles);
if (err != 0) {
std::cout << "Error: setKernelArg 0 does not work!" << std::endl;
system("Pause");
}
and this is my struct on the CPU:
typedef struct _Particle
{
cl_float4 position;
}Particle;
can someone help me with this problem?
(any clue is worth to discuss...)
Thanks
Your code snippet contains some typical C programming errors. At first,
(Particle*) particles = new Particle[200];
does not declare a new variable particle as a pointer to Particle. It must be:
Particle *particles = new Particle[200];
As next, in your call of
clEnqueueWriteBuffer(queue, cl_Particles, CL_TRUE, 0, sizeof(Particle) * 200, &particles, 0, NULL, NULL);
you passed a pointer to the particles pointer as the 6th parameter (ptr). But, here you must pass a pointer to the region on the host containing the data. Thus, change &particles to particles:
clEnqueueWriteBuffer(queue, cl_Particles, CL_TRUE, 0, sizeof(Particle) * 200, particles, 0, NULL, NULL);
The setup of the kernel arguments is also wrong. Here, you must pass the OpenCL buffer created with clCreateBuffer. Thus, replace
err = clSetKernelArg(kernel, 0, sizeof(Particle) * 200, &cl_Particles);
with:
err = clSetKernelArg(kernel, 0, sizeof(cl_Particle), &cl_Particles);
As clCreateBuffer returns a value of type cl_mem, the expression sizeof(cl_Particle) evaluates to the same as sizeof(cl_mem). I recommend to always call sizeof() on the variable, so you need to change the data-type only in one place: the variable declaration.
On my platform, cl_float3 is the same as cl_float4. This might not be true on your/every platform, so you should always use the same type in the host code and in the kernel code. Also, in your kernel code you should/must use the type float4 instead of cl_float4.
I hope, I got the C calls right because I actually tested it with this C++ code. This code snippet contains the fixed C calls as comments:
Particle *particles = new Particle[200];
for (int i = 0; i < 200; i++)
{
//particles[i].position.x = 5f;
particles[i].position.s[0] = 0x5f; // due to VC++ compiler
}
//cl_mem cl_Particles = cl_createBuffer(context, CL_MEM_READ_WRITE, sizeof(Particle)*200, NULL, &err); // FIXED
cl::Buffer cl_Particles(context, CL_MEM_READ_WRITE, sizeof(Particle)*200, NULL, &err);
checkErr(err, "Buffer::Buffer()");
//err = clEnqueueWriteBuffer(queue, cl_Particles, CL_TRUE, 0, sizeof(Particle) * 200, particles, 0, NULL, NULL); // FIXED
queue.enqueueWriteBuffer(cl_Particles, CL_TRUE, 0, sizeof(Particle) * 200, particles, NULL, NULL);
checkErr(err, "ComamndQueue::enqueueWriteBuffer()");
//init of kernel
cl::Kernel kernel(program, "GetCellIndex", &err);
checkErr(err, "Kernel::Kernel()");
//err = clSetKernelArg(kernel, 0, sizeof(cl_Particle), &cl_Particles); // FIXED
err = kernel.setArg(0, sizeof(cl_Particles), &cl_Particles);
checkErr(err, "Kernel::setArg()");
I have a code written in Allegro 4 and am converting it to Allegro 5, but I'm having serious problems with functions that no longer exist in allegro 5, one of them is "blit"
BITMAP *bTile; // Bitmap do tile;
BITMAP *bMapa; // Bitmap do mapa;
BITMAP *bTexturas[20]; // Vetor de bitmap das texturas;
BITMAP *bChar; // Bitmap do personagem;
BITMAP *bMario[3];
// ##########################################################
int main()
{
int vResolucao_x = 640;
int vResolucao_y = 480;
init(vResolucao_x, vResolucao_y);
BITMAP *buffer = create_bitmap(vResolucao_x, vResolucao_y);
bTile = load_bitmap("Tiles/tile.bmp", NULL);
bChar = load_bitmap("Tiles/mario.bmp", NULL);
bMapa = create_bitmap(vResolucao_x, vResolucao_y);
// ############# - Carrega imagens do mario - ############
bMario[0] = create_bitmap(vTile,vTile);
bMario[1] = create_bitmap(vTile,vTile);
bMario[2] = create_bitmap(vTile,vTile);
blit(bChar, bMario[0], 0, 0, 0, 0, vTile, vTile);
blit(bChar, bMario[1], 33, 0, 0, 0, vTile, vTile);
blit(bChar, bMario[2], 66, 0, 0, 0, vTile, vTile);
// #######################################################
install_int(fGravidade,5);
fCarrega_Sprites_Mapa();
fCarrega_Mapa();
fMostra_Mapa();
draw_sprite(screen, bMapa, 0, 0 );
while (!key[KEY_ESC]) {
Virtually nothing of this my old code works with Allegro 5, so I'm doing the conversion as I can learn, but this blit function, can not find its like in allegro 5, another function that I can not find your simulate is: install_int (...)
Allegro 5 is essentially a completely new library. Here's a line-by-line translation, as best as can be done. Note that Allegro 5 uses a fundamentally different concept (event loop) and you should really take the time to learn how that works before diving into trying to convert your code.
ALLEGRO_BITMAP *bMario[3];
// ##########################################################
int main()
{
int vResolucao_x = 640;
int vResolucao_y = 480;
init(vResolucao_x, vResolucao_y);
ALLEGRO_BITMAP *buffer = al_create_bitmap(vResolucao_x, vResolucao_y);
bTile = al_load_bitmap("Tiles/tile.bmp");
bChar = al_load_bitmap("Tiles/mario.bmp");
bMapa = al_create_bitmap(vResolucao_x, vResolucao_y);
// ############# - Carrega imagens do mario - ############
bMario[0] = al_create_bitmap(vTile,vTile);
bMario[1] = al_create_bitmap(vTile,vTile);
bMario[2] = al_create_bitmap(vTile,vTile);
al_set_target_bitmap(bMario[0]);
al_draw_bitmap(bchar, 0, 0, 0);
al_set_target_bitmap(bMario[1]);
al_draw_bitmap(bchar, 33, 0, 0);
al_set_target_bitmap(bMario[2]);
al_draw_bitmap(bchar, 66, 0, 0);
al_set_target_backbuffer(display);
// #######################################################
fGravidade = al_create_timer(5 / 1000.0); // ALLEGRO_TIMER*
ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue();
al_register_event_source(queue, al_get_timer_event_source(gfGravidade));
al_register_event_source(queue, al_get_keyboard_event_source());
fCarrega_Sprites_Mapa();
fCarrega_Mapa();
fMostra_Mapa();
al_draw_bitmap(bMapa, 0, 0, 0);
al_flip_display();
while (true) {
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_TIMER) {
// fGravidade ticked
}
else if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
break;
}
}
}
I'm trying to create server-side RGBA pixmap from client side buffer. CreatePixmap & CreateImage work ok for 32 and 24 bit, but XPutImage result in Match Error returned by server
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 72 (X_PutImage)
Serial number of failed request: 8
Current serial number in output stream: 8
server does support 32 bit pixmaps (xdpyinfo output: https://gist.github.com/2582961). Same behaviour on ubuntu 12.04 (X.Org version: 1.11.3) and OSX with X.app (X.Org version: 1.10.3)
Why following code fails?
#include <stdlib.h>
#include <X11/Xlib.h>
int main(int argc, char **argv)
{
int width = 100;
int height = 100;
int depth = 32; // works fine with depth = 24
int bitmap_pad = 32; // 32 for 24 and 32 bpp, 16, for 15&16
int bytes_per_line = 0; // number of bytes in the client image between the start of one scanline and the start of the next
Display *display=XOpenDisplay(0);
unsigned char *image32=(unsigned char *)malloc(width*height*4);
XImage *img = XCreateImage(display, CopyFromParent, depth, ZPixmap, 0, image32, width, height, bitmap_pad, bytes_per_line);
Pixmap p = XCreatePixmap(display, XDefaultRootWindow(display), width, height, depth);
XPutImage(display, p, DefaultGC(display, 0), img, 0, 0, 0, 0, width, height); // 0, 0, 0, 0 are src x,y and dst x,y
XEvent ev;
while (1) {
XNextEvent(display, &ev);
}
}
Update: It looks like I finally got answer: use GC associated with pixmap instead of DefaultGC (which has depth of root window)
#include <stdlib.h>
#include <X11/Xlib.h>
int main(int argc, char **argv)
{
int width = 100;
int height = 100;
int depth = 32; // works fine with depth = 24
int bitmap_pad = 32; // 32 for 24 and 32 bpp, 16, for 15&16
int bytes_per_line = 0; // number of bytes in the client image between the start of one scanline and the start of the next
Display *display=XOpenDisplay(0);
unsigned char *image32=(unsigned char *)malloc(width*height*4);
XImage *img = XCreateImage(display, CopyFromParent, depth, ZPixmap, 0, image32, width, height, bitmap_pad, bytes_per_line);
Pixmap p = XCreatePixmap(display, XDefaultRootWindow(display), width, height, depth);
XGCValues gcvalues;
GC gc = XCreateGC(display, p, 0, &gcvalues);
XPutImage(display, p, gc, img, 0, 0, 0, 0, width, height); // 0, 0, 0, 0 are src x,y and dst x,y
XEvent ev;
while (1) {
XNextEvent(display, &ev);
}
}
The problem is with DefaultGC() which return a GC with bit depth of system default screen. If you look at line 53 of your gist paste you see that this is 24:
depth of root window: 24 planes
On line 63 you see that it uses 0x22 as default which is shown in more detail in line 64 to 70:
visual:
visual id: 0x22
class: TrueColor
depth: 24 planes
available colormap entries: 256 per subfield
red, green, blue masks: 0xff0000, 0xff00, 0xff
significant bits in color specification: 8 bits
You could probably do this a bit nicer but as a start you can try this:
Note: This uses system visuals so most probably only support depth of 24 or 32.
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef DEBUG
int dbg = 1;
#else
int dbg = 0;
#endif
/* Return a GC based on depth */
int gc_depth(int depth, Display *dpy, Window scr, Window root, GC *gc)
{
Window win;
Visual *visual;
XVisualInfo vis_info;
XSetWindowAttributes win_attr;
unsigned long win_mask;
if(!XMatchVisualInfo(dpy, scr, depth, TrueColor, &vis_info)) {
fprintf(stderr,
" * ERR: %d depth not supported\n",
depth
);
return 1;
}
visual = vis_info.visual;
win_attr.colormap = XCreateColormap(dpy, root, visual, AllocNone);
win_attr.background_pixel = 0;
win_attr.border_pixel = 0;
win_mask = CWBackPixel | CWColormap | CWBorderPixel;
win = XCreateWindow(
dpy, root,
0, 0,
100, 100, /* dummy size */
0, depth,
InputOutput, visual,
win_mask, &win_attr);
/* To flush out any errors */
if (dbg) XSync(dpy, True);
*gc = XCreateGC(dpy, win, 0, 0);
if (dbg) XSync(dpy, True);
XDestroyWindow(dpy, win);
if (dbg) XSync(dpy, True);
return 0;
}
int main(void)
{
int w = 100;
int h = 100;
int depth = 32;
int bitmap_pad = 32;
int bpl = 0;
Display *dpy;
Window root;
Window scr;
GC gc;
int root_depth;
Pixmap pm;
XImage *img;
unsigned char *buf_img;
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr,
" * ERR: Failed to open display.\n");
return 1;
}
#ifdef DEBUG
/* To get errors in order, slows down
* One can also define int _Xdebug = 1;
* */
XSynchronize(dpy, True);
#endif
root = XDefaultRootWindow(dpy);
scr = XDefaultScreen(dpy);
if ((buf_img = malloc(w * h * 4)) == NULL) {
fprintf(stderr,
" * ERR: Unable to alloacte %d bytes\n",
w * h * 4);
return 1;
}
root_depth = DefaultDepth(dpy, scr);
fprintf(stderr,
"Default depth: %d\n",
root_depth);
/* This should be doen more nice */
if (depth != root_depth) {
if (gc_depth(depth, dpy, scr, root, &gc) != 0)
return 1;
} else {
gc = DefaultGC(dpy, 0);
}
img = XCreateImage(
dpy, CopyFromParent,
depth, ZPixmap,
0, (char *)buf_img,
w, h,
bitmap_pad, bpl);
/* To flush out any errors */
if (dbg) XSync(dpy, True);
pm = XCreatePixmap(
dpy, root,
w, h,
depth);
if (dbg) XSync(dpy, True);
XPutImage(
dpy, pm,
gc, img,
0, 0,
0, 0,
w, h);
if (dbg) XSync(dpy, True);
XFreePixmap(dpy, pm);
XDestroyImage(img);
XFreeGC(dpy, gc);
if (dbg) XSync(dpy, True);
fprintf(stderr,
"OK!\n");
return 0;
}
Well, your code works for 32 bits images if you just create a GC passing a drawable on argument which is 32 bits. XCreateGC(dpy, drawable, 0, 0), where drawable can be a pixmap with 32 bits depth. It works perfect with me.