Convert image byte [] from CMYK to RGB?

I need to convert CYMK image bytes to bytes for an RGB image.
I think you can skip header bytes and convert the other bytes to RGB, and then change the header bytes for the RGB format.
What header bytes should be changed for RGB ?
What is the formula for converting color to bits without an ICC profile?

Can someone help me fill out this code?

 //Decode with inSampleSize Bitmap Resultbitmap; string path = "imageFileCmyk.jpg"; int scale=4; BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inPurgeable = true; o2.inSampleSize=scale; o2.inDither = false; Resultbitmap = BitmapFactory.decodeStream(new FileInputStream(path), null, o2); if (Resultbitmap==null) // Warning!! unsupported color conversion request { File tmpfile = new File(path); FileInputStream is = new FileInputStream(tmpfile.getPath()); byte[] cmykBytes= new byte[(int)tmpfile.length()]; byte[] rgbBytes= new byte[(int)tmpfile.length()]; is.read(cmykBytes); for (int i = 0; cmykBytes.length>i; ++i) { if (i>11) // skip header bytes, is it correct ?? { rgbBytes[i] = cmykBytes[i]?? // How ?? } } // new header bytes for RGB format rgbBytes[??]= ?? // How ?? Resultbitmap = BitmapFactory.decodeByteArray(rgbBytes, 0, rgbBytes.length, o2); } return Resultbitmap; 

Thanks,
Alberto

+2
source share
2 answers

I did it! I found a good tool for correctly processing * .jpg files on the Android platform with unusual color spaces such as CMYK, YCCK and so on. Use https://github.com/puelocesar/android-lib-magick , it is free and easy to configure Android library. Here is a snippet for converting CMYK images to RGB color space:

 ImageInfo info = new ImageInfo(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cmyk.jpg"); MagickImage imageCMYK = new MagickImage(info); Log.d(TAG, "ColorSpace BEFORE => " + imageCMYK.getColorspace()); boolean status = imageCMYK.transformRgbImage(ColorspaceType.CMYKColorspace); Log.d(TAG, "ColorSpace AFTER => " + imageCMYK.getColorspace() + ", success = " + status); imageCMYK.setFileName(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cmyk_new.jpg"); imageCMYK.writeImage(info); Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Docs/cmyk_new.jpg"); if (bitmap == null) { //if decoding fails, create empty image bitmap = Bitmap.createBitmap(imageCMYK.getWidth(), imageCMYK.getHeight(), Config.ARGB_8888); } ImageView imageView1 = (ImageView) findViewById(R.id.imageView1); imageView1.setImageBitmap(bitmap); 
0
source

By simply importing android-lib-magick , the code to convert its color space is pretty simple:

 ImageInfo info = new ImageInfo(path); // path where the CMYK image is your device MagickImage imageCMYK = new MagickImage(info); imageCMYK.transformRgbImage(ColorspaceType.CMYKColorspace); Bitmap bitmap = MagickBitmap.ToBitmap(imageCMYK); 

Kord, you donโ€™t even need to save the converted image again, you can just create a bitmap with it. If the image is not on your device or on your SD card, you need to download it first.

I implemented two simple methods using android-lib-magick called "getCMYKImageFromPath" and "getCMYKImageFromURL". You can see the code here :

https://github.com/Mariovc/GetCMYKImage

0
source

Source: https://habr.com/ru/post/916088/


All Articles