function [ipr, pimg] = rgbipr_residual(ipr, pimg, img_res_ary)
    % evaluate image space residual associated with reconstruction of each
    % particle
    %% initialise
    n_cameras   = length(img_res_ary);
    n_part      = size(ipr.re_part,2);
    ipr.img_res = zeros(1, n_part);
    ipr.img_norm= zeros(1, n_part);
    ipr.n_views = zeros(1, n_part);
    
    %% map residual back to particle images
    for ic = 1 : n_cameras
        %% image size
        n_i					= size(img_res_ary{ic},1);
        n_j					= size(img_res_ary{ic},2);

        %% perform mapping for each timestep
        for rgb = 1 : 3
            % row and column coordinates of particle images
            row_map				= pimg(rgb,ic).map(:,:,1);
            col_map				= pimg(rgb,ic).map(:,:,2);
            % take care of out of bounds elements in map
            % dep_map is always in bounds by construction
            map_ok				= row_map >= 1 & row_map <= n_i ...
                                & col_map >= 1 & col_map <= n_j;
            % create map of indices where map is valid
            n_support_px        = size(row_map, 1);
            ind_map				= zeros([n_support_px n_part]);
            ind_map(map_ok)     = sub2ind([n_i n_j], row_map(map_ok), col_map(map_ok));

            % look up residual on particle images, ignoring masked regions
            pimg(rgb,ic).res			= zeros([n_support_px n_part]);
            pimg(rgb,ic).res(map_ok)    = img_res_ary{ic}(ind_map(map_ok));
            % norm of residual
            pimg(rgb,ic).res_norm       = sum(pimg(rgb,ic).res.^2, 1); % 1 x length(pimg(ic,rgb).ind)
            % accumulate
            ind                 = pimg(rgb,ic).ind;
            ipr.n_views(1,ind)  = ipr.n_views(1,ind) + 1/3;
            ipr.img_res(1,ind)  = ipr.img_res(1,ind) + pimg(rgb,ic).res_norm;
            ipr.img_norm(1,ind) = ipr.img_norm(1,ind)+ sum(pimg(rgb,ic).img.^2,1);
        end
    end
    
    %% normalise residual by number of views particle is visible in
    ipr.img_res     = ipr.img_res ./ ipr.n_views;
    ipr.img_norm    = ipr.img_norm./ ipr.n_views;
end
