(XSetWindowBackground) how to convert "RGB" <-> "unsigned long" - xlib

I want to set the background of a window with color, but XSetWindowBackground has an unisgned long to set the color, how can I link an unsigned long for a rgb value ?

If you have RGB values as integers then you can use XAllocColor with an XColor structure as follows:
XColor col;
col.red = 65535; // value is 0-65535
col.green = 32767;
col.blue = 0;
XAllocColor(dis, DefaultColormap(display,0), &col);
the value you want is in
col.pixel
if you have a hex string then you convert it to an XColor with:
XParseColor(display, DefaultColormap(display,0), "#FF7F00", &col);;

Related

how to decode an hexacidemal values to decimal ones with springboot?

i don't know how to decode hexacidemal values to decimal ones with springboot
Is There a default fonctions That can help me or should i develop functions by myself
thank You
You just need JAVA APIs,
From Hexadecimal to Decimal
String hexNumber = ...
int decimal = Integer.parseInt(hexNumber, 16);
System.out.println("Hex value is " + decimal);
From Decimal to Hex
If you have the value that you want to convert in an int variable, then you can simply call:
int i = ...
String hex = Integer.toHexString(i);
System.out.println("Hex value is " + hex);
If you have the decimal number in a String, then you first call Integer.parseInt() but this time you don't need any second parameter— decimal is the default:
String string = ...
int no = Integer.parseInt(string);
String hex = Integer.toHexString(no);
System.out.println("Hex value is " + hex);
Check out the full paper here.

How do I Read and Write to an image with uint format?

I want to read and write from an image that stores unsigned integers. How can I read and write? The standard way to read and write to an image is using imageLoad/imageStore, but when using the format qualifier r32ui, the compiler errors with no matching overloaded function found.
This fails to compile:
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(set = 0, binding = 0, r32ui) uniform writeonly uimage3D img;
void main() {
imageStore(img, ivec3(1,2,3), uint(4));
}
This compiles fine:
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(set = 0, binding = 0, rgba8ui) uniform writeonly uimage3D img;
void main() {
imageStore(img, ivec3(1,2,3), uvec4(4,5,6,7));
}
I have tried using uvec3 for coordinates instead of ivec3, and uvec4 for the data to write in case I am misunderstanding what the format is storing. Using 2 dimensional images also made no difference.
The error message you get is correct, there simply is no overloaded version of imageStore that takes a single unsigned integer (see specs).
So when using the r32ui qualifier, you still need to pass a 4-component unsigned vector just like in your second example, but instead construct it from a single value:
void main()
{
imageStore(img, ivec3(1,2,3), uvec4(4));
}

How to fix 'constant x oveflows byte' error in go?

Hello I am trying to make a byte slice with constants but I get the constant x overflows byte error.
Here are my constants:
const(
Starttrame1 = 0x10A
Starttrame2 = 0x10B
Starttrame3 = 0X10C
Starttrame4 = 0X10D
Starttrame5 = 0X10E
Starttrame6 = 0x10F
)
and here is how I declare my slice:
var startValues = [6]byte{Starttrame1,Starttrame2,Startrame3,Starttrame4,Starttrame5,Starttrame6}
Everytime I build I get the constant 266 overflows byte. How should I declare my constants in order to fix this?
In Go, byte is an alias for uint8, which is the set of all unsigned 8-bit integers (0..255, both inclusive), see Spec: Numeric types. Which means a value of 0x10A = 266 cannot be stored in a value of type byte.
If you need to store those constants, use a different type, e.g. uint16:
const (
Starttrame1 = 0x10A
Starttrame2 = 0x10B
Starttrame3 = 0X10C
Starttrame4 = 0X10D
Starttrame5 = 0X10E
Starttrame6 = 0x10F
)
var data = [...]uint16{
Starttrame1, Starttrame2, Starttrame3, Starttrame4, Starttrame5, Starttrame6,
}
Try it on the Go Playground.

show random image on Processing

I'm really noob on processing and programing and I can't figure it out how to show my images at random.
I'm loading the images in setup with the PImage name img0, img1, img2 and then
image("img" + random(3), 0, 0);
But it does't work, coz processing wait for a PImage argument, and the string plus a number isn't.
And I know for shure there must be some better way than:
int randomNumber = random(3);
if(randomNumber == 0 ){
image(img0,0,0);
}
if(randomNumber == 1 ){
image(img1,0,0);
}
if(randomNumber == 2 ){
image(img2,0,0);
}
But I haven't found it.
Any thoughts?
thanks!
In addition to Kevin's great answer you can also use a an array to store the loaded PImages.
Here's a rough example (you'll need to adjust path to images of course):
// total number of images
int numImages = 3;
// an array of images
PImage[] images = new PImage[num];
int randomNumber;
void setup(){
//TODO correct sketch size
size(300,300);
// initialize images array (loading each one)
for(int i = 0 ; i < numImages; i++){
// TODO correct path to images
images[i] = loadImage("img"+(i)+".png");
}
}
void draw(){
background(0);
//render the most recently selected random index image
image(images[randomNumber]);
//instructions
text("click to randomize",10,15);
}
// change the random number on click (draw() would look chaotic/hard to debug)
void mousePressed(){
// pick a random number and cast the floating point value return to integer needed as in images array index
randomNumber = (int)random(numImages);
}
You could use a HashMap to create a map from String keys to PImage values. Something like this:
HashMap<String, PImage> imageMap = new HashMap<String, PImage>();
imageMap.put("image1", image1);
imageMap.put("image2", image2);
Then to get a PImage from a String key, you'd call the get() function:
PImage image1 = imageMap.get("image1");
You can find more info in the reference.
By the way, this line won't compile:
int randomNumber = random(3);
The random() function returns a float value. You can't store a float value in an int variable. You have to convert it using the int() function:
int randomNumber = int(random(3));
If you still can't get it working, please post a MCVE that demonstrates the problem. Good luck.

UINT16 monochrome image to 8bit monochrome Qimage using freeImage

I want to convert a UINT16 monochrome image to a 8 bits image, in C++.
I have that image in a
char *buffer;
I'd like to give the new converted buffer to a QImage (Qt).
I'm trying with freeImagePlus
fipImage fimage;
if (fimage.loadfromMemory(...) == false)
//error
loadfromMemory needs a fipMemoryIO adress:
loadfromMemory(fipMemoryIO &memIO, int flag = 0)
So I do
fipImage fimage;
BYTE *buf = (BYTE*)malloc(gimage.GetBufferLength() * sizeof(BYTE));
// 'buf' is empty, I have to fill it with 'buffer' content
// how can I do it?
fipMemoryIO memIO(buf, gimage.GetBufferLength());
fimage.loadFromMemory(memIO);
if (fimage.convertTo8Bits() == true)
cout << "Good";
Then I would do something like
fimage.saveToMemory(...
or
fimage.saveToHandle(...
I don't understand what is a FREE_IMAGE_FORMAT, which is the first argument to any of those two functions. I can't find information of those types in the freeImage documentation.
Then I'd finish with
imageQt = new QImage(destiny, dimX, dimY, QImage::Format_Indexed8);
How can I fill 'buf' with the content of the initial buffer?
And get the data from the fipImage to a uchar* data for a QImage?
Thanks.
The conversion is simple to do in plain old C++, no need for external libraries unless they are significantly faster and you care about such a speedup. Below is how I'd do the conversion, at least as a first cut. The data is converted inside of the input buffer, since the output is smaller than the input.
QImage from16Bit(void * buffer, int width, int height) {
int size = width*height*2; // length of data in buffer, in bytes
quint8 * output = reinterpret_cast<quint8*>(buffer);
const quint16 * input = reinterpret_cast<const quint16*>(buffer);
if (!size) return QImage;
do {
*output++ = *input++ >> 8;
} while (size -= 2);
return QImage(output, width, height, QImage::Format_Indexed8);
}

Resources