Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a possibility to copy a non-rectangular (pie slice-shaped in my case) part between Images? I've looked on Clone method in Bitmap class, but it only accepts Rectangles. Per-pixel analysis doesn't give satisfactory performance unfortunately.
Posted
Comments
YvesDaoust 7-Oct-13 11:39am    
How do you perform "per-pixel analysis" precisely ? There are ways to access the bitmap pixels efficiently.
rafallus 7-Oct-13 12:00pm    
Currently:

for (int i = 0; i < image_src.Width; i++)
for (int j = 0; j < image_src.Height; j++)
{
image_dest.SetPixel(i, j, image_src.GetPixel(i, j));
}

For now all it does is copying image pixel per pixel. What I intend to do is sth like (in pseudocode):

foreach(pixel in pie_slice_region)
image_dest.SetPixel(pixel.X, pixel.Y, image_src.GetPixel(pixel.X, pixel.Y));

Then I will need to implement transformations to that region, like rotation and symmetry, to fill rest of the circle with that region.
YvesDaoust 7-Oct-13 12:13pm    
SetPixel/GetPixel are a severe bottleneck. In your case it is much better to "LockBits" the bitmap so that you can access the image buffer directly (you will find posts on this topic elsewhere in CP). If in addition you can generate a run-length-coded representation of the pie (not an easy task), you can memcpy whole runs at a time. This can be quite efficient for large areas.
rafallus 7-Oct-13 13:40pm    
Thank you for suggestion, I will see, how LockBits way works.
YvesDaoust 7-Oct-13 12:26pm    
Extra hint about run-length-coding the shape: to fill an arbitrary polygon, the sweepline approach is well known (http://www.cs.princeton.edu/courses/archive/fall99/cs426/lectures/scan/sld028.htm). It amounts to decomposing the polygon into trapezia (some of which degenerate into triangles). You can adapt it to the case of a Pie, provided you take the curvature of the circle into account and split the arc at the top and bottom if needed to ensure that an horizontal line crosses once only.

SetPixel/GetPixel are a severe bottleneck. In your case it is much better to "LockBits" the bitmap so that you can access the image buffer directly (you will find posts on this topic elsewhere in CP). If in addition you can generate a run-length-coded representation of the pie (not an easy task), you can memcpy whole runs at a time. This can be quite efficient for large areas.

Extra hint about run-length-coding the shape: to fill an arbitrary polygon, the sweepline approach is well known (http://www.cecs.csulb.edu/~pnguyen/cecs449/lectures/fillalgorithm.pdf). It amounts to decomposing the polygon into trapezia (some of which degenerate into triangles). You can adapt it to the case of a Pie, provided you take the curvature of the circle into account and split the arc at the top and bottom if needed to ensure that an horizontal line crosses once only.
 
Share this answer
 
v4
First - consider that rectangles are chosen deliberately because the involved pixels can be "analyzed" quickly. Consider that circle-drawing routines define their content by the enclosing rectangle. The general case would be one you've already explored, pixel-by-pixel, and would cause a lot of pain solving each problem specifically.

BUT, there is a place for you to look for help:

Gimp 2 - an open source photo manipulation application - has two features (at least) you may find useful to consider:
Free Select Tool : selects a region defined by an arbitrary number of points defining a closed boundary
Scissor Select Tool: Used like Free Select Tool but it locates edges when connection adjacent points.

Since this is "Open Source", you can check out the source code used for both of these and see if it helps.


Gimp 2 Home Page^


 
Share this answer
 
Thank you again for input, I have managed to solve the problem using suggested approach of LockBits. As for pie slice, I've not dealt with it explicitly. Instead I've made a mapping using transformations between log-polar and Cartesian coordinates, which mapped pixels of source image onto destination image accordingly by calculations done in log-polar domain.

Performance is satisfactory even pre-optimization (in current state none of the trigonometric values are precached in lookup table).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900