You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ColorType enum is used to represent the meaning of subcomponents of Pixels and Decoders. However it has a few issues:
Many values of it are completely ridiculous and almost surely never used in practice. Why would anyone have an image with 157 bits per pixel BGR?
Parts of the library are unimplemented for certain color types. At very least it should be possible to convert between any pair of color types (filling any missing values with some reasonable default).
Adding enum variants would be a breaking change to the library, yet some relatively useful/common formats (like floating point RGBA) are missing. This needs to be resolved before a 1.0 release.
One option is to replace the enum with the something similar to the following (based on the supported modes in the Pillow library):
enumColorType{// 8-bits per channelPalette,Luminance,LuminanceAlpha,RGB,RGBA,// Reversed channel orderBGR,BGRA,// Exotic single channel bit depthsU1,I32,F32,// High bit depth color imagesRGBA16,RGBA32F,}
Questions
[ ] Do we want to include BGR / BGRA formats?
[ ] Should we include U1 (packed 1 bit per pixel, 8 pixels per byte)?
[ ] Should we define a smaller set of "core" types, say Luminance + RGB + RGBA + RGBA32F, and then focus on conversions to and between them?
Could it be a possibility to differentiate between the format of a pixel, the available color types, and the color space. For example, both RGBA(8) and BGRA(8) offer 8-bit deep rgba data. Similarly, a planar image with these four color channels could offer the same. Therefore, it should be possible to trivially convert between these buffer layouts while preserving–and independently of– the color space. However, a YCbCr color type would require knowing both color spaces for accurate conversion, Palette is somewhere in between if the underlying color type and color space is the same but we should still check color space.
In any case, reinterpretation of an image under a different color space with the same color type and pixel format should also always be a trivial operation. Such that decoders can supply color space information independently and the programmer can overwrite this without effort.
I am in favor of keeping color space information separate from the pixel format and color type. At the same time I'm reluctant to try to divide pixel format and color type. I think it would end up being more trouble than it was worth. This way we have only one enum and all possible variants make sense. It would take some archane type trickery to prevent nonsensical formats like 2 channel color types in BGR order to avoid having to litter the code with unimplemented!() branches everywhere.
Thinking about it more, the Palette type might also be out of place. All of the other color types allow direct interpretation of the color data, but that one needs additional metadata and would very difficult to operate on without conversion. Doing that conversion could be (and maybe already is?) part of the decoding process when loading an image.
Doing that conversion could be (and maybe already is?) part of the decoding process when loading an image.
An interesting point. Doing the conversion during decoding is more troublesome to implement for decoders (and currently they decide their output format) while doing it strictly after reading image has less coupling but more resource consumption (memory, cpu) because we store the full intermediate result.
(minor questions.. does anything use the xbox360's 7e3 rgb HDR these days, how about packed 16bits per pixel RGBA as 1555 _565 4444, i think theres some formats like 10bit per channel r,g,b aswell these days for modern monitors. is the rgb16 fixed or floating; there is a nice 16bit float format
these might be unusual formats but supported by various peices of hardware.
palette: would you support 4bit i.e.16 color aswell .. hardly used today, but there's retro emulators.
Sounds from the original comment like the previous system might have allowed general purpose encoding of the RGB fields.. given all the options that have appeared maybe that isn't such a bad idea)
@dobkeratops The current design isn't that general. It is just layout + a single u8 to indicate bit depth. Thus channels are assumed to be unsigned ints (no floating point formats), and all channels have to be the same depth (so RGB10A2, RGB565, etc are not supported).
Currently, the main use for ColorType is to describe what the raw bytes produced by a decoder mean. Thus, the more variants we have the more different cases that users of the library have to handle. Currently that's so much of a hassle that our own image loading code gives up when encountering anything but the most common color types.
I think the biggest current flaw or constrait to overcome would be to allow pixels to not be packed in memory. This is required by Pixel::from_slice etc. The constraint on our own encoder is due to DynamicImage having this static set of enum variants mostly. Imho, instead of interpreting the raw bytes by the Decoder ourselves it might be better to make conversion to DynamicImage easier for external code and then request DynamicImage from the decoder. Making sure that the color types in the header and the color type in the output are consistent is trivially done afterwards and a bug in decoders (i.e. we could cast the dynamic image down to the actual buffer through as_rgba().expect(…) where necessary)
What do you mean by pixels not being packed? Are you taking about planar images that store the channels separately? We could probably accommodate those by adding PlanarXXX variants to ColorType. Converting to DynamicImage should also definitely be easier. I'd like there to be an infallible from_raw function for that which took image dimensions, a ColorType, and the raw bytes.
We could probably accommodate those by adding PlanarXXX variants to ColorType.
Not without more changes. These could not implement Pixel and that is a strong requirement of GenericImage. Because of...
I think we're talking about different things. ColorType is an enum that isn't related to the Pixel trait. Are you thinking of the Rgb, Rgba, etc. structs?
Yes, ColorType is not related through code. But there is one struct for each type, and converters for each variant at DynamicImage for compatibility reasons. I would not see great use in a ColorType variant without a strictly typed corresponding struct for access.
ColorType::Gray(1) has no corresponding struct, yet it can be produced by some decoders. Even though image cannot manipulate such images, it is useful to have because it means you don't have to roll your own file format parsing code to open 1-bit images in your program. An alternative strategy would be to have the decoder immediately unpack the image, but that would increase memory use 8x.
I think we should have a large set of color types that decoders can produce and then a smaller set of "core" types that can be represented by DynamicImage (supporting all of them would be too much implementation effort).
My idea was more that there wouldn't really be any conversion support to begin with. You would probably have an ExtendedColorType::to_colortype(&self) -> ImageResult<Colortype>, but the main reason for the type would be to accommodate the ones that would like to use the decoders directly and work with rarer colortypes without coming in the way of the rest of the library.
I guess that if you want to use the decoders directly one of the reasons would be to get access to the arbitrary byte data, in that case it may be useful to have a larger set of possible colortypes. If you need full library support for your colortype you would simply call to_colortype and maybe get some sort of UnsuportedColortype error.
This may also help with all of the unimplemented! that are scattered around the current ColorType enum.
What should the library do if it is asked to open an image file with a color type that isn't one of the core formats? It sounds like you are suggesting that we just return an error rather than try to convert it, but I'm not sure I understand why. There are tons of formats like 4-bit RGBA or RGBX that can very easily and lossessly be converted to core color types, so why shouldn't we just do that?
Of course we should convert if we can. But for some formats adding conversion may be harder than others. So I was thinking you could add the enum now and then implement conversion progressively. Given all of the different pixel formats out there, adding support for everything at once is a huge undertaking so you want some sort of simple backwards-compatible way to implement conversion which I think this is.
Exactly this. We know about some exotic color formats existing, but offering all in the same api suggests they are supported equally well, or at least comparably. Returning one of the exotic color types doesn't invalidate the usability of the decoder itself, but the library can not necessarily map all operations, sometimes returning a Err(Error::UnsupportedColor). While in the core set of color types every operation should work without error.
I started trying to convert image to have two separate color type enums. I very quickly noticed that not just decoders, but also encoders needed to operate on ExoticColorTypes to avoid loss of functionality (some encoders support 1 bit black/white images but the rest of the library doesn't). The only other uses of ColorType in the library are for our "poor man's RTTI" on Pixel, and to indicate what type of data is stored in a DynamicImage -- which is itself already an enum over the color types it supports...
@birktj @HeroicKatora I tried out implementing a split color type enum, but didn't find the result all that compelling. What are your thoughts?
Through this process, I'm leaning more towards defining a non-exhaustive enum of color types with some programmatic way to see what functionality is supported for each color type + a required canonical form for to convert each color type to/from from the set: {L, LA, RGB, RGBA} x {u8, u16, f32, ...}.
I understand what you mean, CoreColorType isn't really used that much.
I think a non-exhaustive enum is good, but I would like some more variants,
personally I would really like cmyk.
Could something like this be used to convert between representations? It should
allow to convert between representations that are not memory backed while still
having to potential to be reasonably performant.
@fintelia I think the reason why it's not compelling enough is the lack of other color representations that do not fit in the core type's list. Add rgb555 to the non core type etc, and I think the picture is much clearer. For a (too) long list, maybe take the opengl/vulkan buffer colors.
@HeroicKatora I like that design though it would have to be in addition to a dynamically typed version like the one @birktj described. (You frequently don't know what type an image will be until you open it...)
Regarding CoreColorType, I'm concerned that anywhere we use it in the library will only constrain functionality rather than expand it. Decoders and encoders can't be limited to only taking core color types. Similarly, making implementations of Pixel return a CoreColorType limits which implementations of Pixel we can have, an issue DynamicImage also runs into. Perhaps all of those cases should actually use a normal ColorType, and the CoreColorType should be reserved only when doing conversions?
Another question: I've been assuming that the CoreColorType enum is exhaustive, meaning that users can match over all its variants, but it can't be expanded once we hit 1.0. Is this consistent with your thinking?
Decoders and encoders can't be limited to only taking core color types.
Not at all, but that would be a possibly clean boundary for ImageError::UnsupportedColor(_). Meaning, the non-abstracted version of the decoder can return a buffer with BroadColorType but the DynamicImage representation will return an Err(ImageError::UnsupportedColor(_)) when trying to import the image with the generic method, presumably with the error message hinting at this restriction. And also only when automatic conversion into one of the supported color types is infeasible.
Another question: I've been assuming that the CoreColorType enum is exhaustive, meaning that users can match over all its variants, but it can't be expanded once we hit 1.0. Is this consistent with your thinking?
Yes, more or less. CoreColorType is exhaustive while the other is not. Note that, of course, this doesn't make it impossible to add mutable buffers for other types, just that those would not be part of the 1.0 equivalent of DynamicImage.
So I understand, you are imagining that CoreColorType is really an enumeration over the color types supported by DynamicImage? And that Pixel and by extension GenericImage would allow color types outside the core ones (constrained by which ones could provide the full interface required by the Pixel trait)?
Activity
197g commentedon Jan 20, 2019
Could it be a possibility to differentiate between the format of a pixel, the available color types, and the color space. For example, both
RGBA(8)andBGRA(8)offer 8-bit deep rgba data. Similarly, a planar image with these four color channels could offer the same. Therefore, it should be possible to trivially convert between these buffer layouts while preserving–and independently of– the color space. However, aYCbCrcolor type would require knowing both color spaces for accurate conversion,Paletteis somewhere in between if the underlying color type and color space is the same but we should still check color space.In any case, reinterpretation of an image under a different color space with the same color type and pixel format should also always be a trivial operation. Such that decoders can supply color space information independently and the programmer can overwrite this without effort.
fintelia commentedon Jan 20, 2019
I am in favor of keeping color space information separate from the pixel format and color type. At the same time I'm reluctant to try to divide pixel format and color type. I think it would end up being more trouble than it was worth. This way we have only one enum and all possible variants make sense. It would take some archane type trickery to prevent nonsensical formats like 2 channel color types in BGR order to avoid having to litter the code with
unimplemented!()branches everywhere.fintelia commentedon Jan 20, 2019
Thinking about it more, the Palette type might also be out of place. All of the other color types allow direct interpretation of the color data, but that one needs additional metadata and would very difficult to operate on without conversion. Doing that conversion could be (and maybe already is?) part of the decoding process when loading an image.
197g commentedon Jan 20, 2019
An interesting point. Doing the conversion during decoding is more troublesome to implement for decoders (and currently they decide their output format) while doing it strictly after reading image has less coupling but more resource consumption (memory, cpu) because we store the full intermediate result.
dobkeratops commentedon Jan 20, 2019
(minor questions.. does anything use the xbox360's 7e3 rgb HDR these days, how about packed 16bits per pixel RGBA as 1555 _565 4444, i think theres some formats like 10bit per channel r,g,b aswell these days for modern monitors. is the rgb16 fixed or floating; there is a nice 16bit float format
these might be unusual formats but supported by various peices of hardware.
palette: would you support 4bit i.e.16 color aswell .. hardly used today, but there's retro emulators.
Sounds from the original comment like the previous system might have allowed general purpose encoding of the RGB fields.. given all the options that have appeared maybe that isn't such a bad idea)
fintelia commentedon Jan 20, 2019
@dobkeratops The current design isn't that general. It is just layout + a single u8 to indicate bit depth. Thus channels are assumed to be unsigned ints (no floating point formats), and all channels have to be the same depth (so RGB10A2, RGB565, etc are not supported).
Currently, the main use for ColorType is to describe what the raw bytes produced by a decoder mean. Thus, the more variants we have the more different cases that users of the library have to handle. Currently that's so much of a hassle that our own image loading code gives up when encountering anything but the most common color types.
197g commentedon Jan 20, 2019
I think the biggest current flaw or constrait to overcome would be to allow pixels to not be packed in memory. This is required by
Pixel::from_sliceetc. The constraint on our own encoder is due toDynamicImagehaving this static set of enum variants mostly. Imho, instead of interpreting the raw bytes by theDecoderourselves it might be better to make conversion toDynamicImageeasier for external code and then requestDynamicImagefrom the decoder. Making sure that the color types in the header and the color type in the output are consistent is trivially done afterwards and a bug in decoders (i.e. we could cast the dynamic image down to the actual buffer throughas_rgba().expect(…)where necessary)fintelia commentedon Jan 20, 2019
What do you mean by pixels not being packed? Are you taking about planar images that store the channels separately? We could probably accommodate those by adding PlanarXXX variants to ColorType. Converting to DynamicImage should also definitely be easier. I'd like there to be an infallible from_raw function for that which took image dimensions, a ColorType, and the raw bytes.
197g commentedon Jan 20, 2019
Not without more changes. These could not implement
Pixeland that is a strong requirement ofGenericImage. Because of https://github.com/PistonDevelopers/image/blob/c3c7d5c9e72d51c7983d65d69d27d3e5c61edeb3/src/buffer.rs#L26 and the requirement to return pixels by&mut. And https://github.com/PistonDevelopers/image/blob/c3c7d5c9e72d51c7983d65d69d27d3e5c61edeb3/src/buffer.rs#L72 also assumes pixels to be present as very strictly typed packed slices. Not speaking of the pixel iteratorPixelsMut...Sounds like recipe for disaster, but some (not necessarily internal)
unsafefunction may be permissible.fintelia commentedon Jan 20, 2019
I think we're talking about different things. ColorType is an enum that isn't related to the Pixel trait. Are you thinking of the Rgb, Rgba, etc. structs?
197g commentedon Jan 20, 2019
Yes,
ColorTypeis not related through code. But there is one struct for each type, and converters for each variant atDynamicImagefor compatibility reasons. I would not see great use in aColorTypevariant without a strictly typed corresponding struct for access.fintelia commentedon Jan 20, 2019
ColorType::Gray(1)has no corresponding struct, yet it can be produced by some decoders. Even though image cannot manipulate such images, it is useful to have because it means you don't have to roll your own file format parsing code to open 1-bit images in your program. An alternative strategy would be to have the decoder immediately unpack the image, but that would increase memory use 8x.I think we should have a large set of color types that decoders can produce and then a smaller set of "core" types that can be represented by DynamicImage (supporting all of them would be too much implementation effort).
4 remaining items
birktj commentedon Feb 11, 2019
My idea was more that there wouldn't really be any conversion support to begin with. You would probably have an
ExtendedColorType::to_colortype(&self) -> ImageResult<Colortype>, but the main reason for the type would be to accommodate the ones that would like to use the decoders directly and work with rarer colortypes without coming in the way of the rest of the library.I guess that if you want to use the decoders directly one of the reasons would be to get access to the arbitrary byte data, in that case it may be useful to have a larger set of possible colortypes. If you need full library support for your colortype you would simply call
to_colortypeand maybe get some sort ofUnsuportedColortypeerror.This may also help with all of the
unimplemented!that are scattered around the currentColorTypeenum.fintelia commentedon Feb 11, 2019
What should the library do if it is asked to open an image file with a color type that isn't one of the core formats? It sounds like you are suggesting that we just return an error rather than try to convert it, but I'm not sure I understand why. There are tons of formats like 4-bit RGBA or RGBX that can very easily and lossessly be converted to core color types, so why shouldn't we just do that?
birktj commentedon Feb 11, 2019
Of course we should convert if we can. But for some formats adding conversion may be harder than others. So I was thinking you could add the enum now and then implement conversion progressively. Given all of the different pixel formats out there, adding support for everything at once is a huge undertaking so you want some sort of simple backwards-compatible way to implement conversion which I think this is.
197g commentedon Feb 11, 2019
Exactly this. We know about some exotic color formats existing, but offering all in the same api suggests they are supported equally well, or at least comparably. Returning one of the exotic color types doesn't invalidate the usability of the decoder itself, but the library can not necessarily map all operations, sometimes returning a
Err(Error::UnsupportedColor). While in the core set of color types every operation should work without error.fintelia commentedon Feb 12, 2019
I started trying to convert image to have two separate color type enums. I very quickly noticed that not just decoders, but also encoders needed to operate on
ExoticColorTypes to avoid loss of functionality (some encoders support 1 bit black/white images but the rest of the library doesn't). The only other uses of ColorType in the library are for our "poor man's RTTI" on Pixel, and to indicate what type of data is stored in a DynamicImage -- which is itself already an enum over the color types it supports...fintelia commentedon Feb 14, 2019
@birktj @HeroicKatora I tried out implementing a split color type enum, but didn't find the result all that compelling. What are your thoughts?
Through this process, I'm leaning more towards defining a non-exhaustive enum of color types with some programmatic way to see what functionality is supported for each color type + a required canonical form for to convert each color type to/from from the set: {L, LA, RGB, RGBA} x {u8, u16, f32, ...}.
birktj commentedon Feb 14, 2019
I understand what you mean,
CoreColorTypeisn't really used that much.I think a non-exhaustive enum is good, but I would like some more variants,
personally I would really like cmyk.
Could something like this be used to convert between representations? It should
allow to convert between representations that are not memory backed while still
having to potential to be reasonably performant.
197g commentedon Feb 15, 2019
@fintelia I think the reason why it's not compelling enough is the lack of other color representations that do not fit in the core type's list. Add
rgb555to the non core type etc, and I think the picture is much clearer. For a (too) long list, maybe take the opengl/vulkan buffer colors.197g commentedon Feb 15, 2019
Regarding converters, I would have built the api with some ideas from ffmpeg.
fintelia commentedon Feb 15, 2019
@HeroicKatora I like that design though it would have to be in addition to a dynamically typed version like the one @birktj described. (You frequently don't know what type an image will be until you open it...)
Regarding CoreColorType, I'm concerned that anywhere we use it in the library will only constrain functionality rather than expand it. Decoders and encoders can't be limited to only taking core color types. Similarly, making implementations of Pixel return a CoreColorType limits which implementations of Pixel we can have, an issue DynamicImage also runs into. Perhaps all of those cases should actually use a normal ColorType, and the CoreColorType should be reserved only when doing conversions?
Another question: I've been assuming that the CoreColorType enum is exhaustive, meaning that users can match over all its variants, but it can't be expanded once we hit 1.0. Is this consistent with your thinking?
197g commentedon Feb 15, 2019
Not at all, but that would be a possibly clean boundary for
ImageError::UnsupportedColor(_). Meaning, the non-abstracted version of the decoder can return a buffer withBroadColorTypebut theDynamicImagerepresentation will return anErr(ImageError::UnsupportedColor(_))when trying to import the image with the generic method, presumably with the error message hinting at this restriction. And also only when automatic conversion into one of the supported color types is infeasible.Yes, more or less.
CoreColorTypeis exhaustive while the other is not. Note that, of course, this doesn't make it impossible to add mutable buffers for other types, just that those would not be part of the1.0equivalent ofDynamicImage.fintelia commentedon Feb 15, 2019
So I understand, you are imagining that
CoreColorTypeis really an enumeration over the color types supported byDynamicImage? And that Pixel and by extension GenericImage would allow color types outside the core ones (constrained by which ones could provide the full interface required by the Pixel trait)?197g commentedon Jun 20, 2020
Oh right, making it
non_exhaustiveis a form of finalization ^^