0

我正在使用 OpenGL 将图像绘制到屏幕上。由于缩放能力,我的图像有时会通过 z 轴平移。例如,用户有尺寸为 3200x2000 的图像,他将其缩小,因为它不适合屏幕。缩小后,他想裁剪部分图像。我注册选择矩形。然后使用左下角坐标和右上角和左下角之间的距离。坐标是这样使用的:

//I get one coordinate from mouse down, and then last point from moueDragged events. 
float firstx = mouseDownFirst.x * (zoomSlider.floatValue * (-10));
float firsty = mouseDownFirst.y * (zoomSlider.floatValue * (-10));
float lastx = mouseDragLast.x * (zoomSlider.floatValue * (-10));
float lasty = mouseDragLast.y * (zoomSlider.floatValue * (-10));

//Then I draw selection rectangle to screen.
glBegin(GL_QUADS);
glVertex2f(firstx, firsty);
glVertex2f(firstx, lasty);
glVertex2f(lastx, lasty);
glVertex2f(lastx, firsty);
glEnd();
glFlush();

//and do crop. Because image is drawn to framebuffer, I will read frame buffer info and make image from it. Coordinates are being translated back to normal
firstx = firstx / (zoomSlider.floatValue * (-10));
firsty = firsty / (zoomSlider.floatValue * (-10))
lastx = lastx / (zoomSlider.floatValue * (-10));
lasty = lasty / (zoomSlider.floatValue * (-10));


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, [tempRep bitmapData]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

CIImage *firstImage = [[CIImage alloc] initWithBitmapImageRep:tempRep];

//cropping image to rect and getting result image
float distanceWidth = lastx - firstx;
float distanceHeight = lasty - firsty;

CIImage* secondImage = [firstImage imageByCroppingToRect:CGRectMake(firstx, firsty, distanceWidth, distanceHeight)];

CIFilter* transform = [CIFilter filterWithName:@"CIAffineTransform"];
NSAffineTransform* affineTransform = [NSAffineTransform transform];
[affineTransform translateXBy:-firstx yBy:-firsty];
[transform setValue:affineTransform forKey:@"inputTransform"];
[transform setValue:secondImage forKey:@"inputImage"];
CIImage* lastImage = [transform valueForKey:@"outputImage"];

但它返回不良形象。不是我选择的。例如:

选择 在此处输入图像描述

裁剪图像
在此处输入图像描述

4

0 回答 0