[Index for tmp_for_tar/imgio] [Return to Master Index]

getmagicnum

(tmp_for_tar/imgio/getmagicnum.m)


Function Synopsis

[fid, magicnum] = getmagicnum(fname)

Help text



 getmagicnum    Get the magic number of an image file.  If the magic
                no. is of a gzipped image, ungzip the image, and get the
                magic ID of the image.

                [fid, magicnum] = getmagicnum(fname)
                fname    - image file name.
                fid      - file ID from which further reading should take
                           place.
                magicnum - magic number of the file.

                [fid, magicnum] = getmagicnum(imfid)
                imfid    - image file ID.
                fid      - file ID from which further reading should take
                           place.  This is not necessarily `imfid'.
                magicnum - magic number of the file.

 Created: 7.8.98.
 Version: 1.0



Listing of function file tmp_for_tar/imgio/getmagicnum.m

## Copyright (C) 1998 Ariel Tankus
## 
## This program is free software.
## This file is part of the Image Processing Toolbox for Octave
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##

## getmagicnum    Get the magic number of an image file.  If the magic
##                no. is of a gzipped image, ungzip the image, and get the
##                magic ID of the image.
##
##
##                [fid, magicnum] = getmagicnum(fname)
##                fname    - image file name.
##                fid      - file ID from which further reading should take
##                           place.
##                magicnum - magic number of the file.
##
##                [fid, magicnum] = getmagicnum(imfid)
##                imfid    - image file ID.
##                fid      - file ID from which further reading should take
##                           place.  This is not necessarily `imfid'.
##                magicnum - magic number of the file.
##

## Author: Ariel Tankus <arielt@math.tau.ac.il>
## Created: 7.8.98.
## Version: 1.0

function [fid, magicnum] = getmagicnum(fname)

  ## constants:
  gzipMagic     = [31; 139];
  compressMagic = [31; 157];
  zcatCommand   = 'zcat';

  inputAsName = isstr(fname);	# is input given as filename or file ID?
  if (inputAsName)
    fid = fopen(fname, 'r');
    if (fid == -1)
      error(['unable to open file: ', fname]);
    end
  else
    fid = fname;
  end

  ## read magic number
  [magicnum, count]=fread(fid, 2, 'uchar');
  if (count ~= 2)
    fclose(fid);
    error('Unable to read magic number (1st attempt)!\n');
  end

  if ((magicnum == gzipMagic) | (magicnum == compressMagic))
    ## gzipped/compressed image

    if (inputAsName)
      fclose(fid);
      out = popen([zcatCommand, ' ', fname], 'r');
      
    else
      [in, out, pid] = popen2(zcatCommand);

      ## write magic no. to `zcat' process
      if (fwrite(in, magicnum, 'uchar') ~= 2)
        error('Unable to write magic no. to zcat process.');
      end

      ## write compressed rest of file to zcat
      while (~feof(fid))
        [val, readCount] = fread(fid, Inf, 'uchar');
        writeCount = fwrite(in, val, 'uchar');
        if (writeCount ~= readCount)
          error('Unable to write data to zcat process.');
        end
      end
      fclose(fid);
      fclose(in);
    end

    ## read magic no. from decompressed file
    [magicnum, count] = fread(out, 2, 'uchar');
    if (count ~= 2)
      fclose(out);
      error('Unable to read magic number (2nd attempt)!\n');
    end
    fid = out;
  end

  magicnum = setstr(magicnum');

endfunction

Produced by oct2html on Sat Sep 14 9:47:03 2002
Cross-Directory links are: ON