473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

color format inconsistency?

hi,

maybe i've been looking at this for too long, but i think i found a
strange thing in the JDK. take the color class and use the method
getRGB() on a color, you get an integer with the color samples in ARGB
(high order bits first) format, e.g.

Color.RED.getRGB(); gives 0xffff0000
then take the raster class with say the SinglePixelPackedSampleModel
(the following line gives u a raster with a databuffer holding width x
height x 4 samples while 4 samples represent the color value for a
stored pixel -- so it's SinglePixelPacked since the 4 color bands each
have a depth of 8bit, that makes 32bit/pixel and the databuffer's
integral type (TYPE_INT) matches this lovely)

Raster ras = Raster.createPackedRaster(DataBuffer.TYPE_INT,widt h,height,4,8,null);

the raster methods (setPixel) basically don't care about the color
format, as long as it's some 32bit value they'll eat it, same with the
setSample methods - they just navigate you through the bitmaze, ähm
array
so it's somebody else's task to interpret the values in the raster
right, let me introduce BufferedImage which enables you to command
exactly that, here's the code

BufferedImage bImg = new
BufferedImage(ras.getWidth(),ras.getHeight(),Buffe redImage.TYPE_INT_ARGB);

along TYPE_INT_ARGB there are other types, those of interest here are
just

TYPE_INT_ARGB - MISLEADING, it's really ABGR (high order first), or
RGBA (low order first), like the api help will tell you
TYPE_4BYTE_ABGR - CONSISTENT at once, this actually interprets ABGR
(low order bits first, or RGBA (high order first)

(now low order bits first:)
why can't i just tell a java BufferedImage (colorformats either RGBA,
or ABGR) to take an java Color (colorformat BGRA) and display it
right, this sucks
there are two ways around this, but my guts tell me one shouldn't have
to bother with this and be changed in the api design

1) take the 32 bit value from getRGB(), left shifting it by 8 and
putting the alpha value in the low order 8bits of the integer

int color = Color.RED.geRGB();
color <<= 8;
color |= (optalpha & 0xFF);

now put the value in the raster and tell your buffered image to
interpret the data as TYPE_4BYTE_ABGR - WOW you've done it, thanks
SUN, this is really easy

2) tell the raster to switch things around - u use a different
constructor for that purpose

Raster ras = Raster.createPackedRaster(DataBuffer.TYPE_INT,r.wi dth,r.height,new
int[]{0xFF<<16,0xFF<<8,0xFF<<0,0xFF<<24},null);

this switches your BGRA color class output into RGBA style (both low
order bits first), so that one can safely use TYPE_INT_ARGB for the
BufferedImage (which actually is RGBA as discussed above)

of course if u design your own 4-band integer array, u have to
consider this and do it the way the getRGB() method spits out it's
Color value - u want to put RED at (x,y) (3,3) on the raster, use

ras.getSampleModel().setPixel(3,3,new int[]{BLUEVAL, GREENVAL, REDVAL,
ALPHAVAL}, ras.getDataBuffer());

this doesn't apply to the first solution - there it's lovely ARGB for
arrays, just like the BufferedImageType
================================================== ===========================
next thing: there is a setPixel method in the SampleModelMethods that
takes a float array, just gorgeous u think - it'll propaply behave
like the Color constructor that takes float arguments - and scale your
fractional values between [0.0,1.0] to range [0,255]

of course all it does is casting away the fractional part and putting
that as the value for a color-band-sample
================================================== ===========================
if u know better, please tell me!

is it on purpose or is it just a designflaw?
i also appreciate any better solutions

what i'm doing?
implementing stuff that's already there - like drawing antialiasing
lines for study - works wonderfully, but i realized that this get's
really sloooow (no wonder, there's no setpixel operation in
java.awt.graphics, so one had to cripple drawLine(x1,y1,x2,y2) to
drawLine(x1,y1,x1,y1))

now i'm working offscreen on this raster and then blitting it to the
screen
is there a better way to intersect the (re-)drawing process in java
for such low level algorithms, making it even faster?

i also tried getting SurfaceData surData =
((SunGraphics2D)g).getSurfaceData().getRaster()...
but this feature isn't implemented yet, SUN tells me via Exception
thx in advance
Jul 17 '05 #1
0 2924

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
2
by: Peter Clifton | last post by:
Hello all, I am very new to this subject and learning by example. I have a small inconsistency in an XML file when transformed using different processors. I was wondering if someone could shed...
3
by: Art Krumsee | last post by:
I have a console app that reads and parses the command line. The code originally read something like this: dim CmdLine as String = Command() if CmdLine.indexof("-trace") then 'tracing is...
6
by: August Karlstrom | last post by:
Hi What's the cause of the inconsistency between variable and parameter declarations in C? What's wrong with e.g. void f(int a, b; char c); August
19
by: Neil Cerutti | last post by:
Where can I find documentation of what Python accepts as the filename argument to the builtin function file? As an example, I'm aware (through osmosis?) that I can use '/' as a directory...
10
AdrianH
by: AdrianH | last post by:
I've been using C++ for years, and I thought that I was quite competent with it, but today I just noticed that there is something of an inconsistency with calling a constructor. If you use a...
24
by: Daniel Rudy | last post by:
Hello Group, Consider the following code: /* form 1 */ int main(void) { int i; char *p;
0
by: Neo Geshel | last post by:
I am experiencing an inconsistency with System.Net.Mail. I have a web form on a site. The form gets filled, and one copy gets sent to the recipient (info@domain.com). This works just fine. ...
3
by: Frank Rizzo | last post by:
Hello, I am trying to print out an hour in a format of 3pm or 5am. I've tried the following, but it throws an exception saying invalid input string: string times =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.