Cocos2d CCSprite Resize Image - cocos2d-iphone

I have 2 points A and B. The distance is 100 and my sprite image is 50. My question is can I resize the sprite from the center of the image in order to keep the quality and if its possible how can I do that? I tried with this code, but it just scale the image width and look awful.
-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
sprite.scaleX = width / sprite.contentSize.width;
sprite.scaleY = height / sprite.contentSize.height;

Not sure if this is what you're after - but if you just want to scale the sprite to twice the size without artefacts you can use linear filtering on the sprite texture
[sprite.texture setAliasTexParameters];
Then just scale it to whatever you like
[sprite setScale: 2.f];

Related

How can I tile an image within a given area of a page in my document, using the Mako SDK?

I'm looking to use an image for tilling, to fill and area of my page, in my document.
I've seen that there is an IDOMImage and IDOMImageBrush, but I'm not sure how to use them in order to scale and tile my source image.
How can I do this with the Mako SDK?
Mako can tile an image into a given area, and also flip alternate tiles to create a pattern. Use a scaling transform to control its size. This code shows you how.
// Declare an output pointer
IOutputPtr output;
// Create new assembly, document and page
IDocumentAssemblyPtr assembly = IDocumentAssembly::create(jawsMako);
IDocumentPtr document = IDocument::create(jawsMako);
IPagePtr page = IPage::create(jawsMako);
// Add the page to the document, and the document to the assembly
document->appendPage(page);
assembly->appendDocument(document);
// Create a fixed page to work with
double pageWidth = 10 * 96.0;
double pageHeight = 20 * 96.0;
IDOMFixedPagePtr fixedPage = IDOMFixedPage::create(jawsMako, pageWidth, pageHeight);
// Load the image file into an image
IDOMImagePtr image = IDOMJPEGImage::create(jawsMako, IInputStream::createFromFile(jawsMako, imageFilePath));
// Find its dimensions
IImageFramePtr frame;
image->getFirstImageFrame(jawsMako, frame);
double imageWidth = frame->getWidth();
double imageHeight = frame->getHeight();
// Create a rect to hold the image
FRect printBounds(0.0, 0.0, pageWidth, pageHeight);
// Create a transformation matrix to scale the image, taking into account the page proportions
// Scaling factor is a float ranging from 0.0 to 1.0
double pageWidthHeightRatio = pageWidth / pageHeight;
FMatrix transform;
transform.scale(scalingFactor, scalingFactor * pageWidthHeightRatio);
// Stick the image in a brush
IDOMBrushPtr imageBrush = IDOMImageBrush::create(jawsMako, image, FRect(), printBounds, transform, 1.0, eFlipXY);
// And now create a path using the image brush
IDOMPathNodePtr path = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, printBounds), imageBrush);
// Add the path to the fixed page
fixedPage->appendChild(path);
// This becomes the page contents
page->setContent(fixedPage);
// Write to the output
output = IPDFOutput::create(jawsMako);
output->writeAssembly(assembly, outputFilePath);
Using this code, with this image:
Produced this tilled image:
The code uses an enum, eTileXY. These are the available tiling options:
eTilingMode
Tiling mode type enumeration.
eNoTile
No tiling. If the area to be painted is larger than the image, just paint the image once (in the location specified by the brush's viewport), and leave the remaining area transparent.
eTile
Tile image without any flipping or rotating of the image. A square image consisting of a single diagonal line between opposite corners would produce diagonal lines when tiled in this mode.
eFlipX
Tile image such that alternate columns of tiles are flipped horizontally. A square image consisting of a single diagonal line between opposite corners would produce chevrons running horizontally across the area when tiled in this mode.
eFlipY
Tile image such that alternate rows of tiles are flipped vertically. A square image consisting of a single diagonal line between opposite corners would produce chevrons running vertically across the area when tiled in this mode.
eFlipXY
Tile image such that alternate columns of tiles are flipped horizontally AND alternate rows of tiles are flipped vertically. A square image consisting of a single diagonal line between opposite corners would produce a grid of squares balanced on their points when tiled in this mode.

OpenCV measure rectangular image size

I have an app that finds an object in a frame and uses warpPerspective to correct the image to be square. In the course of doing so you specify an output image size. However, I want to know how to do so without harming its apparent size. How can I unwarp the 4-corners of the image without changing the size of the image? I don't need the image itself, I just want to measure its height and width in pixels within the original image.
Get a transform matrix that will square up the corners.
std::vector<cv::Point2f> transformedPoints;
cv::Mat M = cv::getPerspectiveTransform(points, objectCorners);
cv::perspectiveTransform(points, transformedPoints, M);
This will square up the image, but in terms of the objectCorners coordinate system. Which is -0.5f to 0.5f not the original image plane.
BoundingRect almost does what I want.
cv::Rect boundingRectangle = cv::boundingRect(points);
But as the documentation states
The function calculates and returns the minimal up-right bounding rectangle for the specified point set.
And what I want is the bounding rectangle after it has been squared-up, not without squaring it up.
According to my understanding to your post, here is something which should help you.
OpenCV perspective transform example.
Update if it still doesn't help you out in finding the height and width within the image
Minimum bounding rect of the points
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
As the minAreaRect reference on OpenCV's website states
Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
You can call box.size and get the width and height.

Cocos2d-x setScaleX()

So I was programming a game in cocos2d-x and I need one of the sprites to get wider for a certain amount of time, so I tried with the method setScaleX(). The problem is that the content size of the sprite does not actually change, and since my collision system is based on the content size of the sprite, they do not properly work. Here is the code I use for scaling:
bar = Sprite::create( "Bar.png" );
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);
bar->setScaleX(1.5);
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);
The output is exactly the same on both cases. Is there any way of fixing this?
ContentSize represent the original texture size unless you set it using setContentSize method.
You can either multiply size with scale factor or use boundingBox().size to know about current size of scaled sprite(if not rotated or skewed).
cocos2d::Size size1 = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size1.width/768;
scaleY=size1.height/1024;
Sprite *sp1=Sprite::create("01.png");
sp1->setScaleX(scaleX);
sp1->setScaleY(scaleY);
this->addChild(sp1);
Scale for Landscape:
cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/1024;
scaleY=size.height/768;
Scale for Portrait:
cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/768;
scaleY=size.height/1024;
Sprite *sp=Sprite::create("01.png");
sp->setScaleX(scaleX);
sp->setScaleY(scaleY);
this->addChild(sp);

CCSprite children coordinates transform fails when using CCLayerPanZoom and CCRenderTexture?

Thanks for reading.
I'm working on a setup in Cocos2D 1.x where I have a huge CCLayerPanZoom in a scene with free panning and zooming.
Every frame, I have to additionally draw a CCRenderTexture on top to create "darkness" (I'm cutting out the light). That works well.
Now I've added single sprites to the surface, and they are managed by Box2D. That works as well. I can translate to the RenderTexture where the light sources ought to be, and they render fine.
And then I wanted to add a HUD layer on top, by adding a CCLayer to the scene. That layer needs to contain several sprites stacked on top of each other, as user interface elements.
Only, all of these elements fail to draw where I need them to be: Exactly in the center of screen. The Sprites added onto the HUD layer are all off, and I have iterated through pretty much every variation "convertToWorldSpace", "convertToNodeSpace", etc.
It is as if the constant scaling by the CCPanZoomLayer in the background throws off anchor points in the layer above each frame, and resetting them doesn't help. They all seem to default into one of the corners of the node bounding box they are attached to, as if their transform is blocked or set to zero when it comes to the drawing.
Has anyone run into this problem? Is this a known issue when using CCLayerPanZoom and drawing a custom CCRenderTexture on top each frame?
Ha! I found the culprit! There's a bug in Cocos2D' way of using Zwoptex data. (I'm using Cocos2D v 1.0.1).
It seems that when loading in Zwoptex v3 data, sprite frames' trim offset data is ignored when the actual sprite frame anchor point is computed. The effect is that no anchor point on a sprite with trim offset in its definition (eg in the plist) has its anchor point correctly set. Really strange... I wonder whether this has occurred to anybody else? It's a glaring issue.
Here's how to reproduce:
Create any data for a sprite frame in zwoptex v3 format (the one that uses the trim data). Make sure you actually have a trimmed sprite, i.e. offset must be larger than zero, and image size must be larger than source.
Load in sprite, and try to position it at center of screen. You'll see it's off. Here's how to compute your anchor point correctly:
CCSprite *floor = [CCSprite spriteWithSpriteFrameName:#"Menu_OmeFloor.png"]; //create a sprite
CCSpriteFrame *frame=[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"Menu_OmeFloor.png"]; //get its frame to access frame data
[floor setTextureRectInPixels:frame.rect rotated:frame.rotated untrimmedSize:frame.originalSizeInPixels]; //re-set its texture rect
//Ensure that the coordinates are right: Texture frame offset is not counted in when determining normal anchor point:
xa = 0.5 + (frame.offsetInPixels.x / frame.originalSizeInPixels.width);
ya = 0.5 + (frame.offsetInPixels.y / frame.originalSizeInPixels.height);
[floor setAnchorPoint:ccp(xa,ya)];
floor.position=(where you need it);
Replace the 0.5 in the xa/ya formula with your required anchor point values.

Zoom image inside preview

I am writing a GUI application that works on Mac and Win and there is one little problem, which i do not know how to solve.
In my application I have a small (250 x 250 px) preview window (let' call it SW) in which placed the image. Image can be much bigger, than SW. Somewhere I have a slider which implements zoom function of image inside SW. My main problem is implement zoom function on this image.
On enter I have:
source image and it's width and height;
view image - it is zoomed copy of source image;
position of zoomed image
size of viewport is 250 x 250 px
It should works like zoom in image processing programs. When we changing our zoom value image becomes smaller or bigger relative to viewport center and position of image inside it. We can move image inside of viewport.
For correct implementation of that problem we need to calculate images size and position inside our view. I'm already wrote some "algo" that implements image size modification.
It is looks like:
float one = (source_original_size - thumbnail_size) / 100;
int bigger_side_size = qRound((100-value) * one) + thumbnail_size;
But I can not imagine how I can calculate position on scene of that zoomed image.
Can anybody help me with ideas?
If it is important I am using Qt framework and QGraphicsView, QGraphicsScene and QGraphicsPixmapItem.
Take a look at the Image Viewer Example, it has some features that you are looking for.