Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
coredump
Visitor

roBitmap Scale

I have a HD image that I want to scale to fit in a given region, is there a way to do this ? Currently, it is cropped to the region.


img = CreateObject("roBitmap", "pkg:/images/logo.png")
' Scale to 254x82

logoRegion=CreateObject("roRegion", img, 61, 34, 254, 82)

screen.DrawObject(61, 34, logoRegion)
0 Kudos
14 REPLIES 14
RokuJoel
Binge Watcher

Re: roBitmap Scale

Here's a little function I wrote a while ago to center and scale a source region to a destination region:

function scale(scalefactor as float,destregion as object,sourceregion as object) as object
sourceregion.setalphaenable(true)
destregion.setalphaenable(true)
destregion.setscalemode(1)
destregion.clear(&h00000000)

ww=sourceregion.getwidth()*scalefactor
w=destregion.getwidth()
hh=sourceregion.getheight()*scalefactor
h=destregion.getheight()

xOffset=(w/2)-(ww/2)
yoffset=(h/2)-(hh/2)
?"Yoffset:";yoffset;" xOffset:";xoffset
destregion.drawscaledobject(xOffset,yOffset,scalefactor,scalefactor,sourceregion)
destregion.finish()
end function
0 Kudos
coredump
Visitor

Re: roBitmap Scale

What would the scale factor be ?
0 Kudos
RokuJoel
Binge Watcher

Re: roBitmap Scale

Well, if your source is 2x the size of your dest then the scalefactor would be 0.5

- Joel
0 Kudos
coredump
Visitor

Re: roBitmap Scale

I am getting a runtime error using that function:


img = CreateObject("roBitmap", "pkg:/images/logo.png")
logoOrginRegion = CreateObject("roRegion", img, 61, 34, img.GetWidth(), img.GetHeight())
logoDestRegion = CreateObject("roRegion", img, 61, 34, 254, 82)
logoScaledRegion = scale(0.5, logoDestRegion, logoOrginRegion)

screen.DrawObject(61, 34, logoScaledRegion)


Error:

Member function not found in BrightScript Component or interface. (runtime error &hf4) in pkg:/source/utils.brs(24)
024: sourceregion.SetAlphaEnable(true)
0 Kudos
NewManLiving
Visitor

Re: roBitmap Scale

you should always check to see if anything returned is invalid. In the 2d api this is important for all bitmaps and regions. For example if a region cannot be created then invalid is returned. interface methods are not found in Invalid thus the error. Regions cannot begin and/or extend beyond the limits of the bitmap. So if you have a bitmap the width of 500 and you start your region at 50 to getwidth then the region width would extend to ( 50 + 500, 550) in the bitmap which does not exist, so invalid would be returned. it would be getwidth - 50
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
RokuJoel
Binge Watcher

Re: roBitmap Scale

"coredump" wrote:
I am getting a runtime error using that function:


Your regions should start at 0,0 or they will clip the image. The destination region in your code is pointing to the same bitmap as the source region, I don't think that will work, or not exactly the way you wanted it to.

The error though is caused by one of the regions being invalid, probably because you specified the bounds of the region outside the bounds of the bitmap itself.

- Joel
0 Kudos
psatish
Visitor

Re: roBitmap Scale

I am trying to set a background image on an roScreen from a jpg file url
First I get the file to tmp directory using Http Async get to file. Then create a bit map from the image

background = createObject("roBitmap", "tmp:/background.jpg" )

Then draw the bitmap on the screen. This works fine as long as the image size is less than 2048 x 2048 which apparently is the maximum size for a bimap. Is there any way to prescale the image before getting it into a bit map ?

Tried something like

background = createObject("roBitmap", "tmp:/background.jpg",{width:1280, height:720, AlphaEnable: true} ) However is gives a "wrong no. of parameters" error

Is there any solution or work around (other than pre-scaling it at the server)
0 Kudos
squirreltown
Roku Guru

Re: roBitmap Scale

"psatish" wrote:
I am trying to set a background image on an roScreen from a jpg file url
First I get the file to tmp directory using Http Async get to file. Then create a bit map from the image

background = createObject("roBitmap", "tmp:/background.jpg" )
Is there any solution or work around (other than pre-scaling it at the server)



If you use Texture Manager to grab the bitmap you can specify a size in pixels, but not percentage unfortunately. Otherwise, you'll need to create a blank bitmap at the desired size and draw the background into that using scaling.
Kinetics Screensavers
0 Kudos
psatish
Visitor

Re: roBitmap Scale

Hi

I assume you mean using
DrawScaledObject(x as Integer, y as Integer, scaleX as Float, scaleY as Float, src as Object) as Boolean
But this function takes either a roBitmap or roRegion and source, it doesn' t take a file as source. In order to use this we need to get the file into bitmap first. So this wont work
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.