home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / source / imagetyp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-09  |  2.1 KB  |  96 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. //  Title:  Image Type Functions
  15. //
  16. //  Author:  John M. Miano miano@colosseumbuilders.com
  17. //
  18. #include <stdlib.h>
  19.  
  20. #include "imagetyp.h"
  21.  
  22. #include "jpegdeco.h"
  23. #include "pngdecod.h"
  24. #include "bmpdecod.h"
  25.  
  26. //
  27. //  Description:
  28. //
  29. //    This function determines the type of image stored in a stream.
  30. //
  31. //  Parameters:
  32. //    strm:  The image stream
  33. //
  34. //  Return Value:
  35. //    An enumeration that identifies the stream type
  36. //
  37. ImageType GetStreamImageType (std::istream &strm)
  38. {
  39.   long startpos = strm.tellg () ;
  40.   
  41.   ImageType result = UnknownImage ;
  42.  
  43.   char buffer [10] ;
  44.   strm.read (buffer, sizeof (buffer)) ;
  45.   if (strm.gcount () == sizeof (buffer))
  46.   {
  47.     if (memcmp (buffer, "\211PNG\r\n\032\n", 8) == 0)
  48.     {
  49.       result = PngImage ;
  50.     }
  51.     else if (memcmp (buffer, "\xFF\xD8\xFF\xE0", 4) == 0
  52.              && memcmp (&buffer [6], "JFIF", 4) == 0)
  53.     {
  54.       result = JpegImage ;
  55.     }
  56.     else if (memcmp (buffer, "BM", 2) == 0)
  57.     {
  58.       result = BmpImage ;
  59.     }
  60.   }
  61.   strm.seekg (startpos) ;
  62.  
  63.   return result ;
  64. }
  65.  
  66. ImageType ReadImage (std::istream &strm,
  67.                 BitmapImage &image,
  68.                 PROGRESSFUNCTION function,
  69.                 void *data)
  70. {
  71.   ImageType type = GetStreamImageType (strm) ;
  72.   if (type == UnknownImage)
  73.     return type ;
  74.  
  75.   BmpDecoder bmpdecoder ;
  76.   JpegDecoder jpegdecoder ;
  77.   PngDecoder pngdecoder ;
  78.   BitmapImageDecoder *decoder ;
  79.   switch (type)
  80.   {
  81.   case BmpImage:
  82.     decoder = &bmpdecoder ;
  83.     break ;
  84.   case PngImage:
  85.     decoder = &pngdecoder ;
  86.     break ;
  87.   case JpegImage:
  88.     decoder = &jpegdecoder ;
  89.     break ;
  90.   }
  91.  
  92.   decoder->SetProgressFunction (function, data) ;
  93.   decoder->ReadImage (strm, image) ;
  94.   return type ;
  95. }
  96.