function mem2img(mem_data, format, hsize)
% display image of mem_data

% Get individual pixels from mem data
pix3 = uint8(bitshift(mem_data,-24));
pix2 = uint8(bitshift(bitshift(mem_data,8),-24));
pix1 = uint8(bitshift(bitshift(mem_data,16),-24));
pix0 = uint8(bitshift(bitshift(mem_data,24),-24));

% Format the image data based on requested format
switch format
    case {'YCbCr 4:2:2', 'Y only'}
        % Both YCbCr 4:2:2 use same YCbCr 4:4:4 to RGB conversion
        switch format
            case 'YCbCr 4:2:2'
                % 4:2:2 to 32-bit
                % MLB : LSB = Cr | Y1 | Cb | Y0
                
                % Get width of image 
                vsize = length(mem_data)/hsize*2;
                % Get Y Components
                Y = uint8(zeros(1,length(mem_data)*2));
                Y(1:2:end) = pix0;
                Y(2:2:end) = pix2;
                % Get Cb and Cr components
                Cb = reshape(pix1,vsize/2,'')';
                Cr = reshape(pix3,vsize/2,'')';
            case 'Y only'
                % Y Only to 32-bit
                % MLB : LSB = Y3 | Y2 | Y1 | Y0
                
                % Get width of image 
                vsize = length(mem_data)/hsize*4;
                % Get Y Components
                Y = uint8(zeros(1,length(mem_data)*4));
                Y(1:4:end) = pix0;
                Y(2:4:end) = pix1;
                Y(3:4:end) = pix2;
                Y(4:4:end) = pix3;
                % Create Cb and Cr components for conversion to RGB
                Cb = ones(hsize, vsize/2).*128;
                Cr = Cb;
        end
        Y = reshape(Y,vsize,'')';
        H = vision.ChromaResampler('Resampling','4:2:2 to 4:4:4');
        img_444 = uint8(zeros(hsize, vsize,3));
        img_444(:,:,1) = Y;
        [img_444(:,:,2), img_444(:,:,3)] = step(H, Cb, Cr);
        img_rgb = ycbcr2rgb(img_444);
    case 'RGB'
        % RGB to 32-bit
        % MLB : LSB = EMPTY | B | G | R
        
        % Get width of image 
        vsize = length(mem_data)/hsize;
        % Get RGB components
        img_rgb = uint8(zeros(hsize, vsize, 3));
        img_rgb(:,:,1) = reshape(pix0, vsize, '')';
        img_rgb(:,:,2) = reshape(pix1, vsize, '')';
        img_rgb(:,:,3) = reshape(pix2, vsize, '')';
    otherwise
        error(message('soc:msgs:unrecognizeImageFormat', format));
end

% Show image as figure
imshow(img_rgb, 'InitialMagnification', 'fit');
