Jump to content
  • Advertisement
Sign in to follow this  
shotgunnutter

ZOMG this is confusing...

This topic is 3982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, this is the messiest bit of code I've tried to write in a while. I have a large image, which loads successfully. I then want to break it up into several smaller images. Here is my code to take section (x,y) from source data and return its image. The result looks like a mash up of the image, the data is definitely from the source image [ edit: no it isn't. Its junk data.] , but the pixels are in no particular order.
void get_section(
	unsigned char *source_data, int source_x, int source_y,
	unsigned char * dest_data, int dest_x, int dest_y, 
	int sec_x, int sec_y
)
{
	printf("Source size: %dx%d\n dest size: %dx%d\n",source_x,source_y,dest_x,dest_y);
	 
	for(int line_number = 0; line_number < dest_y; line_number++){
		int line_start = (sec_x * dest_x) + (source_y * dest_y) * line_number;
		for(int x = 0; x < dest_x; x++){
			dest_data[x]=source_data[line_start + x];
		};
	};
	
};



I'm calling the function like this:
		get_section(
			data, width * 4, height,
			cell_data, 64*4, 64, 
			x,y
		);


Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by shotgunnutter
dest_data[x]=source_data[line_start + x];

Where does the first pixel of the first line get copied to? Where does the first pixel of the second line get copied to? Where does the first pixel of the third line get copied to? etc.

Σnigma

Share this post


Link to post
Share on other sites
This code assumes the the image is in 8-bit format. Is that correct? If not, this code will not work correctly because you are copying 1 byte for each pixel. If the image is in 24-bit format, for example, you need to copy 3 bytes for each pixel.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
This code assumes the the image is in 8-bit format. Is that correct? If not, this code will not work correctly because you are copying 1 byte for each pixel. If the image is in 24-bit format, for example, you need to copy 3 bytes for each pixel.


This is correct, at loading the image (a 32 bit tga) was turned into an array of unsigned chars.

[update]

Here is a screen shot:



Each section should be one part of the image, and when the gaps are removed they should look like this:



the white is alpha in the original file.

Share this post


Link to post
Share on other sites
Something like this?


// Assumes that destImage has dimensions of copyWidth X copyHeight
void CopyRect(BYTE *srcImage, BYTE *destImage, int sourceRectLeft, int sourceRectTop, int copyHeight, int copyWidth)
{
for (int row = 0; row < copyHeight; row++)
{
for (int col = 0; col < copyWidth; col++)
{
unsigned byte *destPixel = destImage + (row*copyWidth + col)*BPP;
unsigned byte *srcPixel = srcImage + ((sourceRectTop + row)*sourceImg->width + (sourceRectLeft + col))*BPP;
for (int i = 0; i < BPP; i++)
destPixel = srcPixel;
}
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
Something like this?

*** Source Snippet Removed ***


I have something similar, except my source image is of a different size to the dest image. Would you like a copy of the code?

Share this post


Link to post
Share on other sites
I think what I'll do is to run some tests on a simpler sample of data, small enough to check visually, then adapt it for the image data. Thankz for your help.

[update] I got something working, just need to break it into usable functions. Here it is...


void test_copy_data()
{
int source_width = 26;
int source_height = 26;

int dest_width = 6;
int dest_height = 6;

int section_x = 1;
int section_y = 1;


unsigned int * source = (unsigned int *)malloc(sizeof(unsigned int)*source_width * source_height);
unsigned int * dest = (unsigned int *)malloc(sizeof(unsigned int)*dest_width * dest_height);

unsigned start_column,start_row;
unsigned end_column,end_row;


start_row = section_x * dest_width;
start_column = section_y * dest_height;

end_column = start_column + dest_width;
end_row = start_row + dest_height;

printf("Source = %d,%d\n",source_width,source_height);
printf("Dest = %d,%d\n",dest_width,dest_height);

printf("Source image starts in column %d,%d and ends in %d,%d\n",start_column,start_row,end_column,end_row);

//some initial values for source image
for(unsigned int index=0; index < source_width * source_height; index++){
source[index]=index;
};

//some initial values (clearly wrong) in dest image
for(unsigned int index=0; index < dest_width * dest_height; index++){
dest[index]=255;
};

//do the funky stuff
int start_pixel = start_column * source_height + start_row;
int dest_index = 0;
int source_index = start_pixel;

for(int y = 0; y < dest_height; y++){
for(int x = start_pixel; x < start_pixel + dest_width; x++){
dest[dest_index++] = source[source_index++];
}
source_index = start_pixel + (source_width * (y+1));
};
//results:
printf("Source image was:\n");
unsigned index=0;
for(unsigned int x=0; x < source_width; x++){
for(unsigned int y=0; y < source_height; y++){
printf("%3d ",source[index]);
index++;
}
printf("\n");
};

printf("\nDest image = \n");
index=0;
for(unsigned int y=0; y < dest_height; y++){
for(unsigned int x=0; x < dest_width; x++){
printf("%3d ",dest[index]);
index++;
}
printf("\n");
};
};



I can sleep again.

[Edited by - shotgunnutter on January 14, 2008 11:45:35 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!