1 registered members (M_D),
1,501
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[SOLVED] now I have problem with pixel_to_bmap()
#348751
11/30/10 06:17
11/30/10 06:17
|
Joined: Aug 2005
Posts: 238
Caermundh
OP
Member
|
OP
Member
Joined: Aug 2005
Posts: 238
|
Now i am attempting to read the pixel color from one bmap and write a blue pixel to another bmap if the pixel color read from the first bmap is the right color.
I have the following function:
function color_bmap(COLOR pixel_color) { var count_x; var count_y; var pixel; COLOR temp_color; BMAP* temp_bmap = bmap_createblack(744,600,32); bmap_lock(temp_bmap,0); bmap_lock(my_bmap,0); count_y = 0; while(count_y<600) { count_x = 0; while(count_x<744) { pixel = pixel_for_bmap(my_bmap,count_x,count_y); pixel_to_vec(temp_color,NULL,8888,pixel); if(temp_color.blue==pixel_color.blue||temp_color.red==pixel_color.red||temp_color.green==pixel_color.green) { temp_color.red = 0; temp_color.green = 0; temp_color.blue = 250; pixel = pixel_for_vec(temp_color,100,8888); pixel_to_bmap(temp_bmap,count_x,count_y,pixel); } count_x += 1; } count_y += 1; } bmap_unlock(temp_bmap); bmap_unlock(my_bmap); my_panel.bmap = temp_bmap; set(my_panel,VISIBLE); }
This whole routine is working as it should, except that when i write a pixel to temp_bmap - no pixel is written. The instruction, pixel_to_bmap() is being executed - it just fails to actually write a pixel to temp_bmap. Why is this happening?
Last edited by Caermundh; 12/01/10 17:37.
|
|
|
Re: now I have problem with pixel_to_bmap()
[Re: Caermundh]
#348752
11/30/10 06:57
11/30/10 06:57
|
Joined: Feb 2010
Posts: 320 TANA/Madagascar
3dgs_snake
Senior Member
|
Senior Member
Joined: Feb 2010
Posts: 320
TANA/Madagascar
|
Hi, I don't really know what you want to achieve . Anyway, bellow is your code with a test program In the manual, it is said that "Structs can not be used for parameters, but struct pointers can."
#include <acknex.h>
#include <default.c>
BMAP *bmp_nature = "NATURE.JPG";
PANEL *pan_bmp =
{
bmap = bmp_nature ;
flags = SHOW ;
scale_x = 0.5 ;
scale_y = 0.5 ;
}
PANEL *my_panel =
{
scale_x = 0.5 ;
scale_y = 0.5 ;
pos_x = 400;
}
BMAP *my_bmap;
// Color bmap
function color_bmap(COLOR *pixel_color)
{
var count_x;
var count_y;
var pixel;
COLOR temp_color;
var bmp_width = bmap_width(my_bmap);
var bmp_height = bmap_height(my_bmap);
BMAP* temp_bmap = bmap_createblack(bmp_width, bmp_height, 32);
bmap_lock(temp_bmap,0);
bmap_lock(my_bmap,0);
count_y = 0;
while(count_y<bmp_height)
{
count_x = 0;
while(count_x<bmp_width)
{
pixel = pixel_for_bmap(my_bmap,count_x,count_y);
pixel_to_vec(&temp_color,NULL,8888,pixel);
if(temp_color.blue==pixel_color->blue||temp_color.red==pixel_color->red||temp_color.green==pixel_color->green)
{
temp_color.red = 0;
temp_color.green = 0;
temp_color.blue = 250;
pixel = pixel_for_vec(&temp_color,100,8888);
pixel_to_bmap(temp_bmap,count_x,count_y,pixel);
}
count_x += 1;
}
count_y += 1;
}
bmap_unlock(temp_bmap);
bmap_unlock(my_bmap);
my_panel.bmap = temp_bmap;
set(my_panel,VISIBLE);
}
void main()
{
wait(1);
my_bmap = pan_bmp->bmap;
color_bmap(vector(0, 150, 0));
}
This is a sample code to transform a bmap to a gray scale
#include <acknex.h>
#include <default.c>
BMAP *bmp_nature = "NATURE.JPG";
BMAP *FIREFOX_3_bmap = "FIREFOX_3.JPG";
BMAP *bmp_box = "box.png" ;
PANEL *pan_bmp =
{
bmap = bmp_nature ;
flags = SHOW ;
//scale_x = 0.2 ;
//scale_y = 0.2 ;
}
void main()
{
// Wait for video functions to be ready
wait (1) ;
BMAP *bmp_to = pan_bmp->bmap ;
pan_bmp->pos_x = ( screen_size.x - bmap_width ( bmp_to ) ) * 0.5 ;
pan_bmp->pos_y = ( screen_size.y - bmap_height ( bmp_to ) ) * 0.5 ;
var format = bmap_lock ( bmp_to, 0 ) ;
if ( format > 565 )
{
int width = bmap_width ( bmp_to ) ;
int height = bmap_height ( bmp_to ) ;
int i = 0 ;
int j = 0 ;
COLOR vec_color ;
var pixel ;
var pixel_transparency;
for ( i = 0; i<width ; i++ )
{
for ( j = 0; j < height ; j++ )
{
pixel = pixel_for_bmap ( bmp_to, i, j ) ;
pixel_to_vec ( &vec_color, &pixel_transparency, format, pixel ) ;
vec_mul( &vec_color, vector ( 0.0721, 0.7154, 0.2125 ) ) ;
//vec_mul( &vec_color, vector ( 0.5, 0.587, 0.299 ) ) ;
//vec_mul( &vec_color, vector ( 0.3, 0.59, 0.11 ) ) ;
int gray = vec_color.red + vec_color.blue + vec_color.green ;
int red = (vec_color.red * .393) + (vec_color.green *.769) + (vec_color.blue * .189) ;
int green = (vec_color.red * .349) + (vec_color.green *.686) + (vec_color.blue * .168) ;
int blue = (vec_color.red * .272) + (vec_color.green *.534) + (vec_color.blue * .131) ;
pixel = pixel_for_vec ( vector( gray, gray, gray ), pixel_transparency, format ) ;
pixel_to_bmap ( bmp_to, i, j, pixel ) ;
}
}
}
bmap_unlock ( bmp_to ) ;
}
|
|
|
Re: now I have problem with pixel_to_bmap()
[Re: 3dgs_snake]
#348789
11/30/10 18:32
11/30/10 18:32
|
Joined: Aug 2005
Posts: 238
Caermundh
OP
Member
|
OP
Member
Joined: Aug 2005
Posts: 238
|
What I am trying to accomplish is to "color in" sections of a blank bmap based on the color values in another bmap. For example, for every red pixel (R=250, G=0, B=0) in the primary bitmap, write a blue pixel (R=0, G=0, B=250) to the second bmap at the same location in the bmap. (Both bmaps are the same resolution, so im drawing a blue pixel in the second bmap everywhere there is a red pixel in the first bmap, essentially.)
Good catch on passing the parameter. I had forgotten about passing a pointer instead of the struct itself. I corrected that, but the code still does not write a pixel to temp_bmap. As I said, I know that the pixel_to_bmap() instruction is being reached and that it is being executed, but it does not write a pixel to the second bmap. At runtime, the second bmap is completely blank.
You state that when calling pixel_to_vec() you have to pass the address of the color variable instead of the variable itself. (IE "pixel_to_vec(&vec_color, &pixel_transparency, format, pixel);" instead of "pixel_to_vec(vec_color, ppixel_transparency, format, pixel);". I know for a fact that I can pass vec_color and not &vec_color - I have another routine that is working just fine using the variable instead of the address.
In your code, when you call pixel_to_bmap(), you are passing the bmap pointer, the x & y co-ordinates, and a pixel value converted by pixel_for_vec(). Im essentially doing the exact same thing in my code, yet yours works and mine doesnt? The only thing you are doing differently is passing a vector() to pixel_for_vec() instead of a COLOR value. a COLOR value is still a vector, im pretty sure thats not the problem.
I'm sorry, I think im just missing your point. What is it that the greyscale code you showed me does differently from the way im doing things?
|
|
|
Re: now I have problem with pixel_to_bmap()
[Re: Caermundh]
#348811
11/30/10 21:13
11/30/10 21:13
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
have a look at this, hopefully will get you working along the right tracks
#include <acknex.h>
#include <default.c>
PANEL* pnlFrom;
PANEL* pnlTo;
void main(){
wait(1);
//create panel to read from
pnlFrom = pan_create("", 1);
pnlFrom.bmap = bmap_createblack(200, 100, 32);
bmap_fill(pnlFrom.bmap, vector(0, 0, 250), 100);
set(pnlFrom, SHOW);
var alpha, formatFrom, formatTo, pixel;
COLOR col;
int x, y, x_max = bmap_width(pnlFrom.bmap), y_max = bmap_height(pnlFrom.bmap);
//create random noise to find
formatFrom = bmap_lock(pnlFrom.bmap, 0);
for(x = 0; x < x_max; x++){
for(y = 0; y < y_max; y++){
vec_set(col, vector(integer(random(255)), integer(random(255)), integer(random(255))));
pixel = pixel_for_vec(col, 100, formatFrom);
pixel_to_bmap(pnlFrom.bmap, x, y, pixel);
}
}
bmap_unlock(pnlFrom.bmap);
//create panel to write to
pnlTo = pan_create("", 1);
pnlTo.bmap = bmap_createblack(200, 100, 32);
bmap_fill(pnlTo.bmap, vector(0, 0, 250), 100);
set(pnlTo, SHOW);
pnlTo.pos_x = 400;
//read all pixel and write only blue to new panel
formatTo = bmap_lock(pnlTo.bmap, 0);
formatFrom = bmap_lock(pnlFrom.bmap, 0);
for(x = 0; x < x_max; x++){
for(y = 0; y < y_max; y++){
pixel = pixel_for_bmap(pnlFrom.bmap, x, y);
pixel_to_vec(col, alpha, formatFrom, pixel);
if(col.blue > 127){
//only draw blue... comment out next 4 lines if not needed
col.blue = 255;
col.green = 0;
col.red = 0;
pixel = pixel_for_vec(col, 100, formatTo);
pixel_to_bmap(pnlTo.bmap, x, y, pixel);
}
}
}
bmap_unlock(pnlTo.bmap);
bmap_unlock(pnlFrom.bmap);
wait(1);
while(!key_enter){ wait(1); }
// while(key_enter){ wait(1); }
pan_remove(pnlFrom);
pan_remove(pnlTo);
main();
}
hold enter to see the impact it has on performance
|
|
|
Re: now I have problem with pixel_to_bmap()
[Re: Caermundh]
#348821
11/30/10 21:45
11/30/10 21:45
|
Joined: Apr 2010
Posts: 172
wdlmaster
Member
|
Member
Joined: Apr 2010
Posts: 172
|
OK, tried this: ... The result is an engine crash. (The bmaps are the same size - both are 744x600)
you must replace the p*4 with p*3 if you use 24 bit bitmaps. The code is tested and works fine. see here
|
|
|
Re: now I have problem with pixel_to_bmap()
[Re: Caermundh]
#348957
12/02/10 07:15
12/02/10 07:15
|
Joined: Oct 2002
Posts: 2,256 Oz
Locoweed
Expert
|
Expert
Joined: Oct 2002
Posts: 2,256
Oz
|
Glad you figured it out Caermundh, and happy you described what happened to get it to work. Everyone should do that.
Professional A8.30 Spoils of War - East Coast Games
|
|
|
|