RGBImageToTransparentPNG

by

 

This functions gets a RGB image amd create a .png transparent image.

RGBImageToTransparentPNG( sImage, sNamePng, nColorNoTras )
function [nReturn, sMex, mMatImage, mMatTransparent] = RGBImageToTransparentPNG( sImage, sNamePng, nColorNoTras )

%**************************************************************************
%
%   function RGBImageToTransparentPNG
%
%       This functions gets an image name which should be RGB, load it and 
%       create a .png with transparent color where the RGB image is white. 
%       If the third parameter is setted the function will color in black 
%       the non-transparent areas.
%
%       -- INPUT --
%       sImage: name (with extention and path) of RGB image file to load
%       sNamePng: name (with extention and path) of the png image that will 
%                 be saved.
%       nColorNoTras: if it is setted to 1 the function will color in black
%                     the non-trasnparent areas.
%      
%       -- OUTPUT --
%       nReturn: if it is equal to 1 the function worked properly,
%                otherwise its value is 0
%       sMex: message to give a response of the function behavour
%       sMatImage: RGB matrix of the writed image
%       sMatTransparente: matrix with the value 1 one where the ouput image
%                         is not transparent and 0 where it is
%                         transparent.
%       
%       --> SILVANO BERTOLDO 2011 <--
%       --> silvanobertoldo@alice.it <--
%**************************************************************************

% Initial operations
nReturn         = 1;
sMex            = 'The function worked properly!';
mMatImage       = [];
mMatTransparent = [];

% Checking input parameters
if ( nargin < 2 )
    nReturn = 0;
    sMex    = 'Incorrect number of input parameters.';
    return;
elseif ( (nColorNoTras ~= 1) || (nColorNoTras ~= 1) )
    nReturn = 0;
    sMex    = 'Incorrect third parameter value. It must be 0 or 1.';
    return;
end

% If third parameters is not setted it will be setted to 0
if ( nargin == 2 )
    nColorNoTras = 0;
end

try

    % Load input image
    mMatImage  = imread( sImage );
    vImageSize = size ( mMatImage );
    nRow       = vImageSize(1);
    nCol       = vImageSize(2);

    % Create starting mMatTrasnparent
    mMatTransparent = zeros( size(mMatImage) );

    % Set to 1 the mMatTrasparent where the image is not white

     for nIndR = 1:nRow
        for nIndC = 1:nCol

            if ( (mMatImage(nIndR, nIndC, 1)<255) && (mMatImage(nIndR, nIndC, 2)<255) && (mMatImage(nIndR, nIndC, 3)<255) )
                mMatTransparent(nIndR, nIndC) = 1;
            end

            if ( nColorNoTras )
                mMatImage(nIndR, nIndC, 1) = 0;
                mMatImage(nIndR, nIndC, 2) = 0;
                mMatImage(nIndR, nIndC, 3) = 0;
            end
        end
     end

    % Write correct png file with trasparent color instead of white
    if ( ~isempty(mMatTransparent) && ~isempty(mMatImage) )
         imwrite( mMatImage, sNamePng, 'PNG', 'Alpha', mMatTransparent );
    end

catch e
    sMex = 'The fnction does NOT worked properly. There are some errors.';
    nReturn = 0;
    return;

end

return;


























Contact us