Changing Colorspace from Grayscale to RGB

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
mact

Changing Colorspace from Grayscale to RGB

Post by mact » 2006-06-29T06:19:00-07:00

Moin,

I just tried this with IM 6.2.8.1 - setting the colorspace (SetImageColorSpace(image,RGBColorspace); ) does not work as expected. Trying to do a scaling from the result gives an error, I think the two missing color channels are not generated.

Is there an easy way of creating a RGB image from a Grayscale image? I could simply copy all pixels to R, G and B - but that would be very slow with larger images, so I'd prefer a way of just telling IM to save the image with three channels instead of one.

BTW "convert" also does not create RGB from Grayscale when used this way:

> convert -colorspace RGB "gray.jpg" "rgb-gray.jpg"

rgb-gray.jpg will NOT be in RGB colorspace but still in grayscale (8 Bit per pixel).

Any hints/suggestions would be highly welcome!

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-15T08:30:09-07:00

Hi, i'm trying to do the same, this code does not change the colorspace for given image....

image_info->sampling_factor = "2x2,1x1,1x1";
status = SetImageType(resize_image, TrueColorType);
if (status == MagickFalse){
MagickError(exception.severity, exception.reason, exception.description);
}
status = SetImageColorspace(resize_image, RGBColorspace);
if (status == MagickFalse){
MagickError(exception.severity, exception.reason, exception.description);
{

i get OK as return code, but no the type or colorspace are not changed . . . ?

User avatar
magick
Site Admin
Posts: 10907
Joined: 2003-05-31T11:32:55-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by magick » 2007-11-15T10:25:59-07:00

  • image_info->sampling_factor = "2x2,1x1,1x1";
Should be
  • image_info->sampling_factor = AcquireString("2x2,1x1,1x1");
Or bad things can occur if you destroy the image_info structure.

When you call "SetImageType(resize_image, TrueColorType);" it automatically converts your image to the RGB colorspace.

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-16T00:55:29-07:00

Thanks for the reply, yes i have now changed to
image_info->sampling_factor = AcquireString("2x2,1x1,1x1");

But, the problem remains, i can't change Grayscale to RGB

my code:
--------------------------------------------------------------------------
image_info->sampling_factor = AcquireString("2x2,1x1,1x1");
status = SetImageType(resize_image, TrueColorType);
if (status == MagickFalse){
fprintf(stderr,"IMAR:ERROR:%s TrueColorType (SetImageType==MagickFalse)",File);
}else{
fprintf(stdout,"IMAR:%s TrueColorType (SetImageType==OK)",File);
}
status = SetImageColorspace(resize_image, RGBColorspace);
if (status == MagickFalse){
fprintf(stderr,"IMAR:ERROR:%s RGBColorspace (SetImageColorspace==MagickFalse)",File);
}else{
fprintf(stdout,"IMAR:%s RGBColorspace (SetImageColorspace==OK)",File);
}
if (image_type = GetImageType(resize_image, &exception)){
fprintf(stdout,"IMAR:%s Type %s (%d)",File,img_types[image_type],image_type);
}else{
description = (char *) exception.description ? (char *)
GetLocaleExceptionMessage(exception.severity, exception.description) : "";
fprintf(stdout,"IMAR:ERROR:%s can't get image type %s",File,description);
}


gives this output:
---------------------------------------------------------------------------------
IMAR:ar-8570-1000002456.org.jpg TrueColorType (SetImageType==OK)
IMAR:ar-8570-1000002456.org.jpg RGBColorspace (SetImageColorspace==OK)
IMAR:ar-8570-1000002456.org.jpg Type GrayscaleType (2)


Do i need to set something else to change the colorspace ?

using : ImageMagick 6.2.3 on SunOS Generic_118558-14 sun4u sparc SUNW,Sun-Blade-100

mact

Re: Changing Colorspace from Grayscale to RGB

Post by mact » 2007-11-16T01:02:59-07:00

see above what I wrote about one and a half year ago, quoting:

> I think the two missing color channels are not generated.

You might want to do that (create the additional channels yourself).

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-16T01:44:42-07:00

OK, understand what you are writing...

But, in magick Core API, how do i do that, add "missing" channels ?

Is the order of these things of great matter ?
have tried to do like this
- Strip profiles
- Add profile (AdobeRGB1998.icc)
- Change Imagetype
- Change Colorspace

and like this

- Strip profiles
- Change Imagetype
- Change Colorspace
- Add profile (AdobeRGB1998.icc)

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-16T06:53:26-07:00

It seams that this can't be done with convert or mogrify,
so maybe it can't be done with imagemagick API ?

nisse@ass001 14:42 ~/local_image_magick >bin/mogrify +profile grey.jpg
nisse@ass001 14:42 ~/local_image_magick >bin/mogrify -profile AdobeRGB1998.icc grey.jpg
nisse@ass001 14:43 ~/local_image_magick >bin/mogrify -type TrueColor grey.jpg
nisse@ass001 14:43 ~/local_image_magick >bin/identify grey.jpg |grep Type
nisse@ass001 14:43 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >bin/mogrify -sampling-factor 1 -type TrueColor grey.jpg
nisse@ass001 14:45 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icc -type TrueColor grey.jpg
nisse@ass001 14:45 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:45 ~/local_image_magick >mv AdobeRGB1998.icc AdobeRGB1998.icm
nisse@ass001 14:46 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icm -type TrueColor grey.jpg
nisse@ass001 14:46 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:47 ~/local_image_magick >bin/mogrify -sampling-factor 1 -profile AdobeRGB1998.icm -colorspace RGB -type TrueColor grey.jpg
nisse@ass001 14:47 ~/local_image_magick >bin/identify -verbose grey.jpg | grep Type
Type: Grayscale
nisse@ass001 14:47 ~/local_image_magick >bin/mogrify -version
Version: ImageMagick 6.3.6 11/14/07 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2007 ImageMagick Studio LLC
nisse@ass001 14:47 ~/local_image_magick >

?

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-16T08:25:14-07:00

I don't think it is possible to get another return type than GrayscaleType
returned from function GetImageType because it uses
IsGrayImage to determine what type of image it is, and that function check red,green and blue channel...

so am i right when i say that SetImageType(image,TrueColorType)
don't changes the type or colorspace of an greyscale image ?

is that right ? or is that wrong ?

let's begin allover from there...

nsh
Posts: 27
Joined: 2006-05-16T01:52:23-07:00

Re: Changing Colorspace from Grayscale to RGB

Post by nsh » 2007-11-16T08:38:30-07:00

Crap !

When asking identify what kind of image grey.jpg is, it says Greyscale and Gray

nisse@ass001 16:35 ~/local_image_magick >bin/identify -verbose grey.jpg
Image: grey.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 150x99+0+0
Type: Grayscale
Endianess: Undefined
Colorspace: Gray


but opening that image in photoshop, tada !
Photoshop understands that image now is RGB...

so, it seams that convert/mogrify really can change the type, but unfortunally dont understand that because of the function isGrayImage that is to clever for me (us?)...

Image

User avatar
anthony
Posts: 8877
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Changing Colorspace from Grayscale to RGB

Post by anthony » 2007-11-22T18:07:31-07:00

The colerspace setting Grey and RGB are actually the same thing in memory.
The only difference is that one specificed that the three channels may be different.

An Image Type is however the way the image is stored in the image file.
When writing an image, that could have a 'grey' type, IM will test the in-memory image for 'greyness' and set the type as appropriate, UNLESS you force it to be
a different type using -type or the approprate API setting.

When identify read the image is saw the type as grey, so marked the in-memory RGB, as being grey. The statistics is thus also returning a single 'gray' channel even though in-memory it is still saved as RGB.

see IM examples Basics...
http://imagemagick.org/Usage/basics/#colorspace
and http://imagemagick.org/Usage/basics/#type
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply