Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungThe Architecture of Averages(20.09.2026 um 05:15 Uhr)
Sichere ProgrammierungHow I Turn a Child's Idea Into a Printable Coloring Page With AI(20.09.2026 um 05:24 Uhr)
Sichere Programmierungagent-usage NEW8: agents can torch a 5-hour window with no estimate(20.09.2026 um 05:46 Uhr)
Sichere ProgrammierungThe Architecture of Averages(20.09.2026 um 05:15 Uhr)
Sichere ProgrammierungHow I Turn a Child's Idea Into a Printable Coloring Page With AI(20.09.2026 um 05:24 Uhr)
Sichere Programmierungagent-usage NEW8: agents can torch a 5-hour window with no estimate(20.09.2026 um 05:46 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Java CMYK to RGB Conversion – Speed Comparison

Introduction

Some optimisations are best left to Java. For example a System.arraycopy is the fastest way to create a new version because it is optimised in hardware inside Java. So in general, any function which might have hardware optimisation is best left to the JVM.

So what about image data conversion? Is it better to delegate it to Java or Do It Yourself? I have been looking at a file which was slow to decode. The bottleneck turned out to be the CMYK to RGB conversion so I decided to have a look…

Version 1: Using ColorConvertOp

My original code used a ColorConvertOp to do the image conversion on the raw CMYK data and then create an RGB image with the converted data. Here is the code.

int[] bands = { 0, 1, 2, 3 };
/**turn it into a BufferedImage so we can filter*/DataBuffer db = new DataBufferByte(data, data.length);
/**create RGB colorspace and model*/ColorSpace rgbCS=GenericColorSpace.getColorSpaceInstance();
/**define the conversion. hints can be replaced with null*/ColorConvertOp CSToRGB = new ColorConvertOp(
   DeviceCMYKColorSpace.getColorSpaceInstance(), rgbCS,
   ColorSpaces.hints);
/**create RGB colorspace and model*/ComponentColorModel rgbModel = new ComponentColorModel(
   rgbCS, new int[] { 8, 8, 8 }, false, false, ColorModel.OPAQUE,
   DataBuffer.TYPE_BYTE);
image =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
WritableRaster rgbRaster=rgbModel.
   createCompatibleWritableRaster(width, height);
CSToRGB.filter(Raster.createInterleavedRaster(db, width,
   height, width*4, 4, bands, null), rgbRaster);
image.setData(rgbRaster);

On the sample page it takes 73 seconds on my fast Mac.

Version 2: Manual Pixel Conversion

Version 2 does the pixel conversion manually and then builds the image from that. A major advantage of this is that I can add optimisations. In this case I cache the last value so I only need to do the conversion if the value changes – on an empty page of 10,000 white pixels, I will get a massive boost as I only need to convert the value once and reuse it.

ColorSpace CMYK=DeviceCMYKColorSpace.getColorSpaceInstance();
BufferedImage image = null;
byte[] new_data = new byte[w * h * 3];
int pixelCount = w * h*4;
float lastC=-1,lastM=-1,lastY=-1,lastK=-1;
float C, M, Y, K;
float[] rgb=new float[3];
/**
* loop through each pixel changing CMYK values to RGB
*/int pixelReached = 0;
for (int i = 0; i < pixelCount; i = i + 4) {
C = (buffer[i]&0xff)/255f;
M = (buffer[i + 1]&0xff)/255f;
Y = (buffer[i + 2]&0xff)/255f;
K = (buffer[i + 3]&0xff)/255f;
if(lastC==C && lastM==M && lastY==Y && lastK==K){
//use existing values if not changed
}else{//work out new
rgb=CMYK.toRGB(new float[]{C,M,Y,K});
//cache values
lastC=C;
lastM=M;
lastY=Y;
lastK=K;
}
new_data[pixelReached++]=(byte)(rgb[0]*255);
new_data[pixelReached++]=(byte)(rgb[1]*255);
new_data[pixelReached++]=(byte) (rgb[2]*255);
}
/**
* turn data into RGB image
*/image =new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Raster raster = createInterleavedRaster(new_data, w, h);
image.setData(raster);

Performance Comparison

The method is longer, but what is the speed difference? On my Mac this takes an astonishing 11 seconds! My next task is to see whether it is the same on other platforms…

I’ve kept the old code (in the hope Oracle one day optimises the filter function), but for the moment you can guess which version I am using…

Have you found any similar cases where a DIY approach is much faster?

As experienced Java developers, we help you work with images in Java and bring over a decade of hands-on experience with many image file formats.

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93987 | rclone versions 1.56.0 through 1.75.0 contain a path traversal vulnerabi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick