Code reworked
I've written a new code using the ideas given in the responses and tried it on a smaller sample, and still I can't figure out why I have the "heads" not erased on the final result.
The new code look like this :
void drawBodyPart(PImage body, PImage bodyPart, String name){
body.loadPixels();
bodyPart.loadPixels();
for(int i = 0; i < body.width; i++){
for(int j = 0; j < body.height; j++){
int position = i + body.width*j;
if(alpha(bodyPart.pixels[position]) > 25){
body.pixels[position] = bodyPart.pixels[position];
}
}
}
body.updatePixels();
body.save(folderName + name + ".png");
}
And the calling method look like this:
for(int i = 0; i < humanBodyList.size(); i++){
for(int j = 0; j < humanHairsList.size(); j++){
humanBodyList.get(i).loadPixels();
humanBodyList.get(i).updatePixels();
drawBodyPart(humanBodyList.get(i), humanHairsList.get(j), "(" + i + "," + j+")");
}
At the end, i've got the first head correcly drawn over the first set of body. Then i've got the first head, merged with the second one merged with the body. I even SAVED the intermediary product of body merging... The more I try the less I understand PImages manipulation in processing.
----- Old description below -----
I am working on a little "Processing 3" project where I try to construct some character images basically by merging different body part together. I have :
- a set of "body" images : body1.png, body2.png...
- a set of "head" images : head1.png, head2.png...
- a set of hairs (idem)
Basically what I do is merging "body1 + head1 + hairs1" together to obtain 1 entire body of character.
Each sub-part picture has the same height/width and is mostly transparent except for the part i'm actually interested. So I compare the pixels of 2 said sub-picture, and depending of transparency I keep on of them in the resulting picture. Proceed again for the next body part until every single body part is included in your final picture, then save it.
I do this for every possible combinaison of head, body and hairs. The loop is not the problem (i have the correct number of pictures at the end). My problem is, some of the picture are just wrong. it's like the buffer of precedent pictures is not empty. As a result you can see the head of the character is in FRONT of some remenants of the hairs of the previous one (see the pictures at the end)
My code look like this :
void humanAssembly(PImage head, PImage body, PImage hairs, String name){
PImage result= body;
head.loadPixels();
body.loadPixels();
hairs.loadPixels();
corpsAssemble.loadPixels();
//Merging body and head
for(int i = 0; i < body.width; i++){
for(int j = 0; j < body.height; j++){
int position = i + body.width*j;
if(alpha(hairs.pixels[position]) > 25){
result.pixels[position] = hairs.pixels[position];
}else if(alpha(head.pixels[position]) > 25){
result.pixels[position] = head.pixels[position];
}
}
}
body.updatePixels();
result.save(imagesRepository + "/generes/" + name+ ".png");
}
(Obviously my code is not very efficient, but I dont intend to work on more than a few hundred possibility of very small images, and this code is supposed to run once)
The calling method looks like this: (inside a triple "for")
humanAssembly(listHumanHead.get(i), listHumanBody.get(j), listHumanHairs.get(k), "(" + i + "," + j + "," + k + ")");
where "listHumanX" are ArrayList successfully loaded before.
It's probably some silly mistake but I didnt find where it is after quite long time, so i'd appreciate if anyone can help me. Thanks in advance. anything that works and is understandable by the poor newbie I am is good enough :)
A correct result : body 1, head 1, hairs 1
A correct result : body 1, head 1, hairs 2
A correct result : body 1, head 2, hairs 2
An icorrect result : body 1, head 2, hairs 1, you still can see the hairs 1 (bald)