An image having few colors can be losslessly encoded at a higher
compression ratio. For this purpose, pixel data are input in raster order
and temporarily stored in a buffer. A neighborhood matching determination
unit generates first information representing whether a pixel having the
same color as that of the pixel of interest exists in neighboring pixels
and second information specifying whether a pixel having the same color
as that of the pixel of interest exists, and if the pixel having the same
color exists, specifying the neighboring pixel. A pixel matching
detection unit counts the number of colors contained in the neighboring
pixels and generates information representing whether the number of
colors is two or less, or three or less. On the basis of the information,
a code generation unit outputs one or both of encoded data from a
matched-pixel position encoding unit and a prediction error encoding
unit.