function y = TV_idftgramvec(TV,IDFT,x)

arg = TV.arg;
idftarg = IDFT.arg;

ntvdims = length(arg.tv_dims);

x = reshape(x,arg.im_dims);

y = 0;

dims0 = arrayfun(@(x) 1:x,arg.im_dims,'UniformOutput',false);
for id = 1:ntvdims
    dim = arg.tv_dims(id);
    if any(idftarg.dftdims == dim) % dimension would be processed by IDFT
        tv_fft = col(abs(arg.tv_ffts{id}).^2);
        if idftarg.dftshift, tv_fft = fftshift(tv_fft); end
        tv_fft = shiftdim(tv_fft,1-dim);
        tv_fft = repmat(tv_fft,[arg.im_dims(1:dim-1),1,arg.im_dims(dim+1:end)]);
        y = y + tv_fft.*x;
    else % dimension would not be processed
        dims1 = dims0;
        dims1{arg.tv_dims(id)} = circshift(dims1{arg.tv_dims(id)},[0,-1]);
        yval = col(x(dims1{:}) - x(dims0{:}));
        dims2 = dims0;
        dims2{arg.tv_dims(id)} = circshift(dims2{arg.tv_dims(id)},[0,1]);
        y = y + (yval(dims2{:}) - yval(dims0{:}));
    end
end

% normalize by IDFT scale factor
if ~idftarg.unitary, y = y.*idftarg.scale; end

% vectorize output
y = y(:);

end
