function mem_data = img2mem(img, format)
% convert img to mem_data

% Always read input image as RGB
img_rgb = imread(img);

% Format the image data based on requested format
switch format
    case {'YCbCr 4:2:2', 'Y only'}
        % The same for YCbCr 4:2:2 and Y only
        img_444 = rgb2ycbcr(img_rgb);
        Y = img_444(:,:,1);
        switch format 
            case 'YCbCr 4:2:2'
                % 4:2:2 to 32-bit
                % MLB : LSB = Cr | Y1 | Cb | Y0
                H = vision.ChromaResampler('Resampling','4:4:4 to 4:2:2');
                pix0 = Y(:,1:2:end); % Y0
                pix2 = Y(:,2:2:end); % Y1
                [pix1, pix3] = step(H, img_444(:,:,2), img_444(:,:,3)); % Cb, Cr
            case 'Y only'
                % Y Only to 32-bit
                % MLB : LSB = Y3 | Y2 | Y1 | Y0
                pix0 = Y(:,1:4:end); % Y0
                pix1 = Y(:,2:4:end); % Y1
                pix2 = Y(:,3:4:end); % Y2
                pix3 = Y(:,4:4:end); % Y3
        end
    case 'RGB'
        % RGB to 32-bit
        % MLB : LSB = EMPTY | B | G | R
        pix0 = img_rgb(:,:,1); % R
        pix1 = img_rgb(:,:,2); % G
        pix2 = img_rgb(:,:,3); % B
        pix3 = zeros(size(pix0)); % blank pixel
    otherwise
        error(message('soc:msgs:unrecognizeImageFormat', format));
end
formattedData = uint32(pix3)*2^24+uint32(pix2)*2^16+uint32(pix1)*2^8+uint32(pix0);
mem_data = reshape(formattedData.',1,'');