2

I need to convert bytes of an CYMK image to bytes for RGB image.
I think it's possible to skip the bytes of the header and convert others bytes in RGB and then change the header bytes for RGB format.
Which are the header bytes to change for RGB?
Which is the formula for the bit color conversion without ICC profile?

Can anybody help me to complete 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's 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

Terabol
  • 21
  • 1
  • 4
  • [This answer](http://stackoverflow.com/a/7636974/321697) may be helpful to you. – Kevin Coppock Aug 22 '12 at 13:55
  • thanks Kcoppock. I have already read it but in android this solution it's not applicable. thank again for your editing. – Terabol Aug 22 '12 at 14:02
  • @Terabol: If I correctly understand your proposed code, then the JPEG decompression fails completely if you encounter a CMYK image. Wouldn't that mean that you need far more than conversion of the CMYK pixels to RGB pixels? Wouldn't you need the complete JPEG decoding as well? – Codo Aug 22 '12 at 14:14
  • @Terabol: BTW. There are simple formulas for converting CMYK to RGB posted all over the net and even published in books. But these formulas are as bad as it gets. The resulting color will only remotely resemble the intended color. There simply is no reasonable CMYK to RGB conversion without the use of color profiles. – Codo Aug 22 '12 at 14:16
  • @Terabol: Other people obviously have compiled libjpeg with the NDK. See http://stackoverflow.com/questions/7608507/in-android-how-to-decode-a-jpeg-in-cmyk-color-format. – Codo Aug 22 '12 at 14:22
  • yes, Codo the JPEG deconding fail with this message "unsupported color conversion request". When i open this image in Photoshop, the image it's correct, and i see that it is CMKY. Android has problem with this kind of image. – Terabol Aug 22 '12 at 14:24
  • I have try to compile libjpeg with NDK for Android but i'm not able to do it. i can't able to find the correct sources and i don't know how to do it. – Terabol Aug 22 '12 at 14:28
  • someone else may send me a project with Android NDK libjpeg ?? thanks – Terabol Aug 22 '12 at 16:19

2 Answers2

0

I made it! I found a nice tool for correct handling *.jpg files on Android platform with uncommon colorspaces like CMYK, YCCK and so on. Use https://github.com/puelocesar/android-lib-magick, it's free and easy to configure android library. Here is a snippet for converting CMYK images to RGB colorspace:

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);
kord
  • 482
  • 3
  • 12
  • 1
    Regarding your flags, the reason your other answers were removed (and this one preserved) was that you simply copied and pasted this answer across multiple questions, even where the answer wasn't relevant. Each answer should address the specific question asked, and several of these didn't. They were being flagged, so we removed the ones that didn't answer the questions they were placed on. – Brad Larson Apr 11 '13 at 20:57
0

Just importing android-lib-magick the code to transform its colorspace is quite 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, even it is not needed to save again the transformed image, you can just create a bitmap with it. If the image is not in your device or on your SD card, you will need to download it first.

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

https://github.com/Mariovc/GetCMYKImage

Mario Velasco
  • 3,336
  • 3
  • 33
  • 50