Grayscale image: Difference between revisions

Content added Content deleted
(→‎{{header|Julia}}: A new entry for Julia)
Line 848:
</script></head><body></body></html>
</lang>
 
=={{header|Julia}}==
'''Adhering to the Task Description'''
<lang Julia>
using Color, Images, FixedPointNumbers
 
const M_RGB_Y = reshape(Color.M_RGB_XYZ[2,:], 3)
 
function rgb2gray(img::Image)
g = red(img)*M_RGB_Y[1] + green(img)*M_RGB_Y[2] + blue(img)*M_RGB_Y[3]
g = clamp(g, 0.0, 1.0)
return grayim(g)
end
 
function gray2rgb(img::Image)
colorspace(img) == "Gray" || return img
g = map((x)->RGB{Ufixed8}(x, x, x), img.data)
return Image(g, spatialorder=spatialorder(img))
end
ima = imread("grayscale_image_color.png")
imb = rgb2gray(ima)
imc = gray2rgb(imb)
imwrite(imc, "grayscale_image_rc.png")
</lang>
Rounding errors are unlikely to be an issue for <code>rgb2gray</code>. The calculation of <code>g</code> promotes it to the literal float type (typically <code>Float64</code>).
 
'''A More Idiomatic Approach'''
<lang Julia>
using Color, Images, FixedPointNumbers
 
ima = imread("grayscale_image_color.png")
imb = convert(Image{Gray{Ufixed8}}, ima)
imwrite(imb, "grayscale_image_julia.png")
</lang>
 
{{output}}
I didn't find a colorful image that I was comfortable modifying and sharing, so I'm omitting the image files from my solution to this task. Try out these [http://r0k.us/graphics/kodak/ images] for something to work with. Although these images are intended for image processing testing and development and are said to be available for unrestricted use, I could find no clear and definitive statement of their usage rights.
 
The results of the two approaches (according to task, ''rc'', and idiomatic, ''julia'') are indistinguishable except perhaps by close examination. The ''julia'' file is native grayscale, and the ''rc'' file is RGB that shows only grays.
 
The task description is silent on the issue of companded sRGB versus linear RGB. Most images are actually sRGB, and strictly speaking, the transformation to get Y from RGB is applicable to linear RGB. I imagine that, unlike the ''rc'' version, the ''julia'' version reverses compansion prior to applying the CIE transformation to extract luminance from RGB.
 
=={{header|Lua}}==