function saveasppm(x, filename, K) %saveasppm encodes an image as PPM and saves it to the disk % Usage saveasppm(x, filename, K), where: % Inputs % - x is the input image % - filename is the path and filename of the file where the resulting % PPM image is going to be saved % - K is the maximum value of brightness file = fopen(filename, 'w'); fwrite(file, 'P6'); fwrite(file, ' '); fwrite(file, int2str(size(x.red, 1))); fwrite(file, ' '); fwrite(file, int2str(size(x.red, 2))); fwrite(file, ' '); fwrite(file, int2str(K)); fwrite(file, newline); if K < 256 precision = 'integer*1'; precisionBytes = 1; machinefmt = 'l'; else precision = 'integer*2'; precisionBytes = 2; machinefmt = 'b'; end for row = 1:size(x.red, 1) for column = 1:size(x.red, 2) fwrite(file, x.blue(row, column), precision, machinefmt); fwrite(file, x.green(row, column), precision, machinefmt); fwrite(file, x.red(row, column), precision, machinefmt); end end fclose(file); end