A digital image is a pixel matrix, with each pixel storing 32 bits of information. 8 bits for storing a number between 0 and 255 for Alpha (transparency information), 8 bits for Red, 8 for Green, and 8 for Blue. All with a value between 0 and 255. 1

To convert to grayscale, for each pixel, you take these Red, Green and Blue values, calculate the average, and use this average result as the value, exactly the same value, for even Red, Green and Blue. 2

Converting to pure black and white is similar, but you use the average result to decide when to store 0 (black) if the value is greater than 127 (that is the result for 255/2) or to store 255 (white) if less than 127. 3 The result can be suboptimal, particularly if the page background is of uneven darkness. 4

The normalized color coordinate transformations used for the default CMYK color space are defined as follows: 5

Linear RGB to CMYK

K = min{1 - R, 1 - G, 1 - B}
if(K != 1) {
    C = (1 - R - K)/(1 - K)
    M = (1 - G - K)/(1 - K)
    Y = (1 - B - K)/(1 - K)
} else {
    C = M = Y = 0
}

CMYK to linear RGB

R = (1 - K)*(1 - C)
G = (1 - K)*(1 - M)
B = (1 - K)*(1 - Y)

In Java, you can do all of this using ImageIO 6 or a library such as Marvin Image Processing Framework.