function [AVG,MASK,support,rings] = blobs2im(CC,RGB,bd,Xdim,Ydim)
%  = blobs2im(bd,Xdim,Ydim)
% bd is a blob descriptor data structure
%
% are blobs sorted by size?  they should be, so that the little guys
% don't get smothered.
% 

res=1;

Xdim=res*Xdim;
Ydim=res*Ydim;

[x,y]=meshgrid(1:Xdim,1:Ydim);
RGB=reshape(RGB,Xdim*Ydim,3);

N=max(CC(:));
finalMASK=ones(Ydim*Xdim,3)*192/255;
finalAVG=ones(Ydim*Xdim,3)*192/255;
support=zeros(Ydim,Xdim);
rings=logical(zeros(Ydim,Xdim,N));
   
for n=1:N
   blobmask=bwmorph(CC==n,'erode');
   ring=bwmorph(blobmask,'remove');

   finalMASK(find(blobmask),:)=RGB(find(blobmask),:);
   finalMASK(find(ring),:)=0;
   finalAVG(find(blobmask),:)=...
       ones(sum(blobmask(:)),1)*lab2rgb(bd(n).mean_color);
   finalAVG(find(ring),:)=0;
   
   support=double(~blobmask).*support + double(blobmask)*n;
   support=double(~ring).*support;
   
   binary=zeros(Ydim,Xdim);
   [rowS,colS,dl,dp]=trace_shape(CC==n,0);
   for i=1:length(rowS)
      ytmp=min(max(Ydim+1-rowS(i),1),Ydim);
      xtmp=min(max(colS(i),1),Xdim);
      binary(ytmp,xtmp)=1;
   end
   %%blobmask=bwmorph(~bwfill(binary,1,1),'dilate',1);
   blobmask=~bwfill(binary,1,1);
   blobmask=flipud(blobmask);
   rings(:,:,n)=logical(bwmorph(blobmask,'remove'));
   %%keyboard
end

%RGBN=final + (sum(final,2)==0)*ones(1,3)*192/255;
MASK=finalMASK;
MASK=reshape(MASK,Ydim,Xdim,3);
MASK=max(MASK,0);
MASK=min(MASK,1);

AVG=finalAVG;
AVG=reshape(AVG,Ydim,Xdim,3);
AVG=max(AVG,0);
AVG=min(AVG,1);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997-1999. The Regents of the University
% of California (Regents). All Rights Reserved.
% 
% Permission to use, copy, modify, and distribute this software and its
% documentation for educational, research, and not-for-profit purposes,
% without fee and without a signed licensing agreement, is hereby
% granted, provided that the above copyright notice, this paragraph and
% the following two paragraphs appear in all copies, modifications, and
% distributions. Contact The Office of Technology Licensing, UC Berkeley,
% 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620, (510)
% 643-7201, for commercial licensing opportunities. Created by
% Chad Carson, Serge Belongie, and Jitendra Malik, Department of Electrical
% Engineering and Computer Sciences, University of California, Berkeley.
% For technical information, contact carson@eecs.berkeley.edu.
% 
% IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
% ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
% REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% 
% REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
% ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
% TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
% MODIFICATIONS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

