You Are Here:

Community: Developer Discussion Boards

Reply « Previous Thread | Next Thread »

#1 Old drawPixels and getPixels - 2004-02-03, 01:22

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Hi all!
Can someone please give me a code example of using getPixels and drawPixels for, lets say, displaying the red pixels in a png image as blue.

Thanks a lot!
Itsik
Reply With Quote

#2 Old 2004-02-03, 10:07

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
This assumes the TYPE_USHORT_4444_ARGB and will only convert "pure red". But here goes.
You just get the pixels into an array, scan the array for red pixels and change them to blue. Note that you have to compare the values taking the format into consideration.
Code:
Image convertRedToBlue(Image src) {
  short red = (short)(0xff00); // red in TYPE_USHORT_4444_ARGB format
  short blue = (short)(0xf00f); // blue in TYPE_USHORT_4444_ARGB format
		
  // get width and height and source DirectGraphics
  int w = src.getWidth();
  int h = src.getHeight();
  DirectGraphics dgSrc = DirectUtils.getDirectGraphics(src.getGraphics());
	  
  // create an array and populate it with the pixels
  short[] pixels = new short[w * h];
  dgSrc.getPixels(pixels,0,w,0,0,w,h,DirectGraphics.TYPE_USHORT_4444_ARGB);
	  
  // transverse the array and change pure red into pure blue
  for (int i = 0; i < w * h; i++) {
    System.out.print("Before : " + pixels[i]);
    if (pixels[i] == red) {
      pixels[i] = blue;
    }
    System.out.println(" After : " + pixels[i]);
  }
  
  // create a new image (you could just use the source if you like) 
  // and draw the converted pixels onto it.
  Image dest = Image.createImage(w,h);
  DirectGraphics dgDest = DirectUtils.getDirectGraphics(dest.getGraphics());
  dgDest.drawPixels(pixels,true,0,w,0,0,w,h,0,DirectGraphics.TYPE_USHORT_4444_ARGB);
  
  return dest;
}
The biggest downfall here is that you are comparing the pixels to pure red. So any "reddish" pixels that also have some value in the other channels or don't have the red channel at it's max won't be converted. To create a proper conversion, you have to break the pixels into their channels (ARGB) and do a "softer comparison" on each channel. That means you change any pixels where the red channel is bigger than a certain threshhold value, and the blue and green channels are close to 0.

shmoove
Reply With Quote

#3 Old 2004-02-03, 10:13

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
OK, I don't know why I was too lazy to do this in the first place, but here is the for loop that converts a more general red into blue:
Code:
  // transverse the array and change "reddish" into pure blue
  for (int i = 0; i < w * h; i++) {
    r = (pixels[i] & 0x0f00) >> 8; // red channel
    g = (pixels[i] & 0x00f0) >> 4; // green channel
    b = (pixels[i] & 0x000f);  // blue channel
    if (r > 0xd && b < 2 && g < 2) { // very red and little blue and green
      pixels[i] = blue;
    }
  }
shmoove
Reply With Quote

#4 Old YOU ARE GREAT! - 2004-02-03, 11:11

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Thanks a lot man!!!
I always knew that the Israeli people are the nicest in the world ;)

Itsik
Reply With Quote

#5 Old 2004-02-03, 11:39

Join Date: Mar 2003
Posts: 118
Location: Rotterdam, Holland
remonvv
Offline
Regular Contributor
lol, how political. A perhaps wiser approach would be to increase blue more of the red component is stronger. I've used this before and it works nicely. So (pseudo) :

color = r, g, b;

newr = 0;
newb = Math.min(15, b + r);

newcolor = newr, g, newb;

Give it a go, it doesnt use tresholds and therefore will generate a completely smooth transition.
Reply With Quote

#6 Old 2004-02-03, 12:16

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Nice one. You definitely know your CG :-)
Though it depends on what the purpose of the conversion is.
Your algorithm moves the whole red channel into the blue channel, and doesn't give a second look to the green channel, so it will also change colors that are not even close to red. For example, yellow (r=15,g=15,b=0) will be transformed into turquoise (r=0,g=15,b=15). I'm not sure that was the OP's intention.

shmoove
Reply With Quote

#7 Old 2004-02-03, 15:53

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Thanks a lot to both of you (Ok ok, the dutch people are nice too! :) )

But I have another question. I saw that series 60 support the 4444 type, and serieses 30 and 40 support 444 (am I correct?)

Does 444 mean that I will lose the transperancy when manipulating the pixels (No alpha component...) ?

Thanks again,
Itsik
Reply With Quote

#8 Old 2004-02-03, 16:55

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
I think it does, but all the Series 40 phones I've used also supported the 4444 type. It might not be native format (DirectGraphics.getNativePixelFormat()), but it doesn't mean it's not supported. I tried my example on a 3300, a 3105 and a 3510i (Series 30), and it worked OK.

shmoove
Reply With Quote

#9 Old 2004-02-03, 17:06

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Oh, that's good to know, I though that "native format" is the best supported format... :)

Thanks again,
Itsik
Reply With Quote

#10 Old 2004-02-03, 17:12

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
The native format is the best supported format. Like the name says, it's the phone's native format, ie, it's the format that will be saved into the video buffer. That means that you'll get the best performance out of those DirectGraphics methods if you use the native format. But other formats are supported, the phone will just have to do a conversion at runtime to actually read and write the pixels.
So if this conversion is something that you need to perform very fast then implementing in native format might be a good idea.

shmoove
Reply With Quote

#11 Old 2004-02-03, 17:13

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Oh, I see, thanks for the clarification!
Where do you work? What do you do?
Reply With Quote

#12 Old 2004-02-03, 17:37

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
It's in my profile.

shmoove
Reply With Quote

#13 Old 2004-02-05, 19:43

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
Hi again,
I tried this method on the 7210 emulator, and it generates a non-transparent image, although I'm creating it from a transparent source... Any ideas how I can avoid it?

Thanks!
Reply With Quote

#14 Old 2004-02-05, 20:49

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
What do you mean? What parts are losing transparency?
Try debugging the code. Print out the values of all the channels (including alpha) before and after the conversion, and see what's getting screwed up there:
Code:
  // transverse the array and change "reddish" into pure blue
  for (int i = 0; i < w * h; i++) {
    r = (pixels[i] & 0x0f00) >> 8; // red channel
    g = (pixels[i] & 0x00f0) >> 4; // green channel
    b = (pixels[i] & 0x000f);  // blue channel
    a = (pixels[i] & 0xf000) >> 12; // alpha channel
    // print out the channels before the transformation
    System.out.println("pixel " + i + " :\tred=" + r + "\tblue=" + b + "\tgreen=" + g + "\talpha=" + a);
    if (r > 0xd && b < 2 && g < 2) { // very red and little blue and green
      pixels[i] = blue;
    }
    r = (pixels[i] & 0x0f00) >> 8; // red channel
    g = (pixels[i] & 0x00f0) >> 4; // green channel
    b = (pixels[i] & 0x000f);  // blue channel
    a = (pixels[i] & 0xf000) >> 12; // alpha channel
    // print out the channels after the transformation
    System.out.println("pixel " + i + " :\tred=" + r + "\tblue=" + b + "\tgreen=" + g + "\talpha=" + a);
  }
shmoove
Reply With Quote

#15 Old 2004-02-05, 22:21

Join Date: Sep 2003
Posts: 37
itsik78
Offline
Registered User
The entire image loses transperancy... It comes out as a white image...
I'll try to print it.
Thanks
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