473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Canvas->CopyRect shifts 1 pixel by zoomed image

7
I'm building a bitmapmanipulation-component in C++Builder6

After loading a bitmap in my component, I then can make a selection, which can be moved around.

The problem I have is in my OnPaint-event. I paint the topmost bitmap first(the selection), then find the parts that not have been painted, and paint those parts from the original bitmap.
If the bitmaps are zoomed-out (smaller on the screen) the original bitmap right to the selection-and selection itself sometimes are distorted.
I tested this with a Zoom of 4 and found distortion only occurs if the rect.left is odd. If it's even all is well. I tried to shift the bitmaps by odd numbers but then the errors occur when zoomed different.

This also happen when no rounding errors by zooming occur.

Can anyone help??
Oct 10 '12 #1
3 2155
Rabbit
12,516 Expert Mod 8TB
I don't know anything about image manipulation but I do know that it will be difficult to help without seeing the code that's causing the issue.
Oct 10 '12 #2
WRoos
7
Expand|Select|Wrap|Line Numbers
  1. //I'm using struct: FObject containing: 
  2. //    bitmap
  3. //    srect: position on screen
  4. //    brect: position in bitmap (bitmap's in bitmap's)
  5. //
  6. // mylist start with the ClipRect (the invalidated rect for my control)
  7.  OnPaint()
  8. {
  9.  TRect rmr[5], *todo;
  10.  
  11.  Canvas->CopyMode = cmSrcCopy;
  12.  for (int n=FObjects->Count-1; n>=0; n--) {  // last bitmap first
  13.    have = (TBBObject*) FObjects->Items[n];
  14.  
  15.    for (int m=mylist->Count-1; m>=0; m--) {   
  16.       todo = (TRect*) mylist->Items[m];
  17.       count = RectMinusRect(*todo, shift, rmr);
  18.     // RectMinusRect return 1-5 rects. 
  19.     // first one is the one to paint now (the vissible part of todo)
  20.     // the other contain max 4 left over peaces
  21.       TRect tobmp(rmr[0]);
  22.     // now I shift to bmp to its place
  23.       OffsetRect(&tobmp, have->srect.left, have->srect.top);
  24.     // for a zoom of -4 the bitmap-points are all multiplied by 4
  25.       ImgageToBitmap(&tobmp);
  26.  
  27.        Canvas->CopyRect(rmr[0], have->bitmap->Canvas, tobmp); //(dst,canvas, src)
  28.  
  29.       delete todo;          // this rect is painted and can be deleted
  30.       mylist->Delete(m);
  31.       for (int t=1; t<=count; t++) {   // left over rect's to be painted 
  32.          todo = new TRect(rmr[t]);     // with next have->bitmap
  33.          mylist->Add(todo);
  34.       }
  35.    }
  36. }
Oct 11 '12 #3
WRoos
7
I made a compleet program with my problem.
If someone is willing to help, he/she can take this code in a new project,
1)define the RMinR function in the .h file
2)define the FormPaint-event in the Form
3)run.

It paints a bitmap in 5 parts. If you look closely you can see the center and the part right to the center are shifted 1 pixel.
For some bitmaps this is more disturbing then for others.
if you change thisbmp(21,21,201,201) to even numbers
all goes well.

Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. void __fastcall TForm1::FormPaint(TObject *Sender)
  3. {
  4. Graphics::TBitmap* abmp = new Graphics::TBitmap;
  5.  // a bitmap at least 850x850 pixels
  6.  abmp->LoadFromFile("D:\\CBuilder6\\large.bmp");
  7.  abmp->HandleType = bmDDB; // best quality-bitmap
  8. TList* rectlist = new TList;
  9. TRect* todo = new TRect;
  10. TRect rmr[5], bmprct, thisbmp;
  11. int count;
  12.  *todo = TRect(0,0,abmp->Width/4,abmp->Height/4);
  13.  rectlist->Add(todo);
  14.  
  15.  Canvas->CopyMode = cmSrcCopy;
  16.  for(int n=0; n<2; n++) {      // 2 bitmap's
  17.    if (n==0) thisbmp = TRect(21,21,201,201);
  18.    else      thisbmp = TRect(0,0,abmp->Width/4,abmp->Height/4);
  19.    for (int m=rectlist->Count-1; m>=0; m--) {
  20.       todo = (TRect*) rectlist->Items[m];
  21.       count = RMinR(*todo, thisbmp, rmr);
  22.       bmprct = rmr[0];
  23.       bmprct.left   *= 4; // Asume zoom = 4
  24.       bmprct.top    *= 4;
  25.       bmprct.right  *= 4;
  26.       bmprct.bottom *= 4;
  27.       // Stretching and copying at the same time
  28.       Canvas->CopyRect(rmr[0], abmp->Canvas, bmprct);
  29.  
  30.       delete todo;
  31.       rectlist->Delete(m);
  32.       for (int t=1; t<=count; t++) {
  33.          todo = new TRect(rmr[t]);
  34.          rectlist->Add(todo);
  35.       }
  36.    }
  37.  }
  38.  delete abmp;
  39.  delete rectlist;
  40.  abmp = NULL;
  41. }
  42. //---------------------------------------------------------------------------
  43. int __fastcall TForm1::RMinR(TRect& ToDo, TRect& Have, TRect* Remain)
  44. {
  45. int ret = 0;
  46.  if (Have.top > ToDo.top) {
  47.    Remain[++ret] = TRect(ToDo.left, ToDo.top, ToDo.right, Have.top);
  48.    Remain[0].top = Have.top;
  49.  } else Remain[0].top = ToDo.top;
  50.  if (Have.bottom < ToDo.bottom) {
  51.    Remain[++ret] = TRect(ToDo.left, Have.bottom, ToDo.right, ToDo.bottom);
  52.    Remain[0].bottom = Have.bottom;
  53.  } else Remain[0].bottom = ToDo.bottom;
  54.  if (Have.left > ToDo.left) {
  55.    Remain[++ret] = TRect(ToDo.left, Remain[0].top, Have.left, Remain[0].bottom);
  56.    Remain[0].left = Have.left;
  57.  } else Remain[0].left = ToDo.left;
  58.  if (Have.right < ToDo.right) {
  59.    Remain[++ret] = TRect(Have.right, Remain[0].top, ToDo.right, Remain[0].bottom);
  60.    Remain[0].right = Have.right;
  61.  } else Remain[0].right = ToDo.right;
  62. // if (Remain[0].left > 0 && Remain[0].left%2 == 1) {Remain[0].left--; Remain[0].right--;}
  63.  return ret;
  64. }
  65.  
Oct 11 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Troy | last post by:
Hi all, Im a little new to ASP.NET. I'm looking to create an image viewer that zooms in, pretty common. But I need to draw a box on the 1:1 scale image and 'zoom in' on another picture box. ...
3
by: Larry Serflaten | last post by:
I am taking a 256 color bitmap from a file and scaling it up X 16 to a 32bppPARGB bitmap in memory. I copy that image to the screen. After scaling, the edges of all the lines and colors are...
2
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect...
2
by: allanon76 | last post by:
I need to create an Image with more then 256 gray scale. this is my code, i've some problem setting correctly the pixel value. any idea? There is another way to create an image with more then...
1
by: Hardy Wang | last post by:
Hi, I found a piece of code to add drop shadow to a photo like below, after I save the image, it is actually a BMP file even though I specify a JPG file extension (see...
0
by: gerstla | last post by:
hi I am building a simple control to view images with zooming capabilities. i want the image to zoom when it is clicked on and try to center the point clicked .the code I am using know for centering...
0
by: Gregory Piñero | last post by:
I want to be able to randomly change pixels in an image and view the results. I can use whatever format of image makes this easiest, e.g., gray scale, bit tonal, etc. Ideally I'd like to keep...
0
by: pk2007 | last post by:
Hi everybody, Here we've got a small program smthing like an image viewer. We have used javax.print for printing. When we zoom out to a scale smaller than an A4 media, it prints out the zoomed...
10
by: Arnaud Diederen | last post by:
Hello everyone, and happy new year to all c.l.j'ers! No bad code to post, just a question. I wrote a JavaScript application that lets users view an image, and zoom on it. The way I "zoom"...
2
by: Noorain | last post by:
Hi, another problem. i upload width=800 pixels image in database through. this image resize by thumb image & bis image. thumb image width is 100 pixel & big image width is 400 pixel. 800 pixel image...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.