function dom = evalmxdom(dom,imagePrefix,outputDir,options)
%EVALMXDOM   Evaluate cellscript Document Object Model, generating inline images.
%   dom = evaldom(dom,imagePrefix,outputDir,options)
%   imagePrefix is the prefix that will be used for the image files.

% Copyright 1984-2003 The MathWorks, Inc.
% $Revision: 1.1.6.10 $  $Date: 2004/11/29 23:30:35 $

% Ned Gulley, 5 Mar 2001

baseImageName = fullfile(outputDir,imagePrefix);

if options.useNewFigure
    myFigure = figure('color','white');
end

allPictures = {};
cellList = dom.getElementsByTagName('cell');
for n = 1:cellList.getLength
    
    mcodeList = cellList.item(n-1).getElementsByTagName('mcode');
    if (mcodeList.getLength > 0) && (mcodeList.item(0).getLength > 0)
        mcode = char(mcodeList.item(0).getFirstChild.getData);
    else
        mcode = '';
    end
    
    % Run the code, capture the output, and save the graphics files.
    [pictureList, mcodeOutput, isError] = takepicture( ...
        {mcode}, baseImageName, length(allPictures), ...
        options.maxHeight, options.maxWidth, options.figureSnapMethod, ...
        options.imageFormat);
    
    for pictureNumber = 1:length(pictureList)
        % Insert an image tag into the DOM
        imgNode = dom.createElement('img');
        [null,name,ext] = fileparts(pictureList{pictureNumber});
        imgNode.setAttribute('src',[name ext]);
        cellList.item(n-1).appendChild(imgNode);
    end

    allPictures = [allPictures pictureList];
    
    if ~isempty(mcodeOutput),
        % Insert an mcodeoutput tag into the DOM
        mcodeOutputNode = dom.createElement('mcodeoutput');
        mcodeOutputTextNode = dom.createTextNode(mcodeOutput);
        mcodeOutputNode.appendChild(mcodeOutputTextNode);
        cellList.item(n-1).appendChild(mcodeOutputNode);
    end
    
    if isError && options.stopOnError
        break
    end
end

if options.useNewFigure
    close(myFigure(ishandle(myFigure)))
end

if options.createThumbnail && ~isempty(allPictures)
    switch options.imageFormat
        case {'png','jpeg','tiff','gif','bmp','hdf','pcx','xwd','ico','cur','ras','pbm','pgm','ppm'}
            % Read in the last image.
            [X,map] = imread(allPictures{end});

            % Convert to UINT8 RGB if we have to.
            if ~isempty(map)
                X = uint8(ind2rgb(X,map)*255);
            end

            % Limit the size of the thumbnail, but preserve the aspect ratio.
            imHeight = 64;
            imWidth = 85;
            [height,width,null] = size(X);
            if (height > imHeight)
                width = floor(width*(imHeight/height));
                height = imHeight;
            end
            if (width > imWidth)
                height = floor(height*(imWidth/width));
                width = imWidth;
            end
            
            % Resize and write out.
            X = make_thumbnail(X,[height width]);
            imgFilename = [baseImageName '.png'];
            imwrite(X,imgFilename)
    end
end
