You Are Here:

Community: Developer Discussion Boards

#1 Old Image Clipping and Transparency - 2003-03-24, 10:34

Join Date: Mar 2003
Posts: 2
vikrambk
Offline
Registered User
Hi!

I am working on a J2ME game where I have a lot of images due to which the file size is exceeding 64kb, I tried putting all the images in a single png file and then clipping the required object, but on the phone I get a white patch where the image is transparent. Can someone tell me how to set the transparency while clipping the image, if this is not possible what other method can be used to reduce the image size.

Thanks

Vikram
Reply With Quote

#2 Old 2003-03-24, 17:30

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
I've had the same problem.
My solution wasn't pretty, but it worked:
I keep all the images in the one original png. Then when I want to draw the a frame from this png onto some background whose Graphics object is g, I set the clip region of g to the size of the frame and then I draw the big image onto g with an offset of
(-xPos,-yPos)
where (xPos,yPos) are the coordinates of the frame inside the big image.
For example:
I want to draw a frame that is on (0,10) on the png, and whose size is 8x8 onto a Graphics context "g" on the coordinate (30,50):
<code>
// get current clipping coordinates, so we can reset them when
// we are finished
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
// set the new clip region for g
g.setClip(30,50,8,8);
// let's say the png is in imgGraphics
// we draw the image with an offset of (-0,-10) from the
// desired coordinates
g.drawImage(imgGraphics,30-0,50-10,Graphics.TOP|Graphics.LEFT);
// set the old clip region back
g.setClip(clipX,clipY,clipW,clipH);
</code>
Hope I made myself clear enough, but if there is anything you don't understand just ask...

shmoove
Reply With Quote

#3 Old 2003-03-27, 10:13

Join Date: Mar 2003
Posts: 59
iecomdev
Offline
Regular Contributor
hi shmoove!

thats works fine and solved my transparent problem!

did you know if there is a performance problem to draw a large image using setClip instead of drawing smal images?

i think my font draw routine is now slower than bevor

any experiance

andreas
Reply With Quote

#4 Old 2003-03-27, 23:50

Join Date: Mar 2003
Posts: 2
Chris_23
Offline
Registered User
Hi!

Another, more resource and performance friendly way would be
using Nokias UIAPI (package com.nokia.mid.ui ) - but that will
only work on Nokia devices...

But it provides, besides some other very useful tools for game developement, a way to initialize images with a transparent
background:
( See Nokia UI API Programmer's guide for more details)

image FontImage = Image.createImage("font.png");
image Character[] = new Image[num_characters]
for( int i=0; i<num_characters; i++ ) {
Character[i] =
DirectUtils.createImage( character_width, character_height, 0x00000000 );
Graphics g = Character[i].getGraphics();
g.DrawImage( -character_width * i, 0, Graphics.TOP | Graphics.LEFT );
}


DirectUtils.createImage() takes a color value as third parameter, which is not 'rgb' but 'argb', (0xaarrggbb) - the most significant byte specifies the 'Alpha'-value: 0xFFrrggbb is fully opaque and 0x00rrggbb is fully transparent. (Most phones won't support alpha-blending, so values greater than 0 will be treated as fully opaque on the most devices)

ChrisK
Reply With Quote

#5 Old 2003-03-28, 07:58

Join Date: Mar 2003
Posts: 59
iecomdev
Offline
Regular Contributor
hi chris!

yes, thats true but on the series60 phones it doesnt work
the AA param in AARRGGBB schould be 00 for fully transparent but thist doesnt work - i dont know why
some told me that the alphaparam on series60 is inverted but if i use FF instead of 00 i got the same problem - fully opaque images.

any solutions?

see also tread:
http://discussion.forum.nokia.com/fo...threadid=17567

andreas
Reply With Quote

#6 Old 2003-03-28, 13:30

Join Date: Mar 2003
Posts: 2
Chris_23
Offline
Registered User
Hi andreas!

mmmh - you're right, sorry didn't try that on the s60 Emulator
before...
Tried some variations (used DirectGraphics.DrawImage instead of Graphics.DrawImage etc.) but that didn't solve the problem either...

ChrisK
Reply With Quote

#7 Old 2003-04-03, 13:19

Join Date: Mar 2003
Posts: 2
tobe@tobe.demon.co.uk
Offline
Registered User
Had the same problem this morning.. nice solution, Shmoove.

An alternative approach which comes to mind but only really works for tiled games might be:

create a tile sized image (the cache)

for each tile on the screen:

copy tile to cache

for each sprite on the tile:

copy to cache from strip using -ve x offset to select image

copy tile to screen

if tile (original) not opaque, blank the cache

This approach avoids setting clip regions at the expense of the 1 extra drawImage (the tile to the cache first instead of the screen or the cache to the screen last, however you prefer to look at it) and perhaps the blank at the end if your background tiles are not opaque.

I'll do some tests on the emulators to get a rough idea if either approach is significantly better. Note though that this alternative approach get's a lot more complicated if any sprite can span 2 or more tiles.

t o b e
Reply With Quote

#8 Old 2003-04-07, 15:05

Join Date: Mar 2003
Posts: 2
tobe@tobe.demon.co.uk
Offline
Registered User
Did some timings on the draw routines with clip regions set and they seemed to be about 2/3 as fast as without.. time to do 10,000 redraws went up from 15 to 21 seconds.

So I developed a different approach to this. It essentially involves keeping the images as PNGs, many images to a single PNG, but storing them uncompressed (deflate type 0). JAR compression gives you the same squeeze down when you add them to the archive but you can then extract and build new individual images *with* transparency retained from the original strip.

Loads of great advantages to this. You've got direct pixel access to raw image data, so you can do mirror, flip, change colour etc etc before you generate the new image plus you save on heap space because you don't have to keep the strip hanging around in memory to draw. And you still get simplicity of drawImage().

Source code for sale.. :-) Semi-serious about that, actually...

t o b e
Reply With Quote

#9 Old Re: Image Clipping and Transparency - 2007-11-05, 11:35

Join Date: Oct 2004
Posts: 5
stuaxo
Offline
Registered User
If you didn't manage to sell this source code do you feel like putting it onto a website somewhere? It sounds pretty useful.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d18645X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ