Q:

Why demosaic introduce pixel value greater than the max pixel in raw image?

I read the raw image and check the max value of the pixel(max(rawImage(:))). The max value is 4095. But, after I convert it to color image and check the max value, I get max values greater than 4095 in all 3 channels . Why after using demosaic I get values greater that 4095? 
rawImage = uint16(fread(rawFile,[cols rows],'uint16'));
colorImage = demosaic(rawImage,'rggb');
red = colorImage(:,:,1);
green = colorImage(:,:,2);
blue = colorImage(:,:,3);

A:

Since demosaic internally performs so many filters; it's natural for the resultant pixel to be greater than the max input value. For some pixels you can have 5001, for others you can get 5023 etc which is quite normal.
In MATLAB when you have a 8 bit data in 8-bit container, it automatically saturates all values greater than 255 to 255. Similarly for a 16-bit data in a 16 bit container it saturates the values to 65535. So it's the container/data type that determines to clip off value and not your data.
Since you have 12 bit data in a 16 bit container, so MATLAB will only saturate a value if it's greater than 65535 and not 4095. What can do is manually saturate all values greater than 4095 to 4095. It won't effect the result. 

