image_io_raw.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:6k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: image_io_raw.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:41:39  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: image_io_raw.cpp,v 1000.1 2004/06/01 19:41:39 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE
  13.  *               National Center for Biotechnology Information
  14.  *
  15.  *  This software/database is a "United States Government Work" under the
  16.  *  terms of the United States Copyright Act.  It was written as part of
  17.  *  the author's official duties as a United States Government employee and
  18.  *  thus cannot be copyrighted.  This software/database is freely available
  19.  *  to the public for use. The National Library of Medicine and the U.S.
  20.  *  Government have not placed any restriction on its use or reproduction.
  21.  *
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy
  23.  *  and reliability of the software and data, the NLM and the U.S.
  24.  *  Government do not and cannot warrant the performance or results that
  25.  *  may be obtained by using this software or data. The NLM and the U.S.
  26.  *  Government disclaim all warranties, express or implied, including
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.
  29.  *
  30.  *  Please cite the author in any work or product based on this material.
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Authors:  Mike DiCuccio
  35.  *
  36.  * File Description:
  37.  *    CImageIORaw -- interface class for reading/writing Windows RAW files
  38.  */
  39. #include <ncbi_pch.hpp>
  40. #include "image_io_raw.hpp"
  41. #include <util/image/image.hpp>
  42. #include <util/image/image_exception.hpp>
  43. BEGIN_NCBI_SCOPE
  44. // header signature
  45. static const char* sc_Header = "RAW";
  46. CImage* CImageIORaw::ReadImage(CNcbiIstream& istr)
  47. {
  48.     // read header
  49.     char header[4];
  50.     istr.read(reinterpret_cast<char*>(&header), 4);
  51.     // read dimensions
  52.     size_t width;
  53.     size_t height;
  54.     size_t depth;
  55.     istr.read(reinterpret_cast<char*>(&width),  sizeof(size_t));
  56.     istr.read(reinterpret_cast<char*>(&height), sizeof(size_t));
  57.     istr.read(reinterpret_cast<char*>(&depth),  sizeof(size_t));
  58.     CRef<CImage> image(new CImage(width, height, depth));
  59.     if ( !image ) {
  60.         NCBI_THROW(CImageException, eReadError,
  61.                    "CImageIORaw::ReadImage(): failed to allocate image");
  62.     }
  63.     istr.read(reinterpret_cast<char*>(image->SetData()),
  64.                 width * height * depth);
  65.     return image.Release();
  66. }
  67. CImage* CImageIORaw::ReadImage(CNcbiIstream& istr,
  68.                                size_t x, size_t y, size_t w, size_t h)
  69. {
  70.     // read header
  71.     char header[4];
  72.     istr.read(reinterpret_cast<char*>(&header), 4);
  73.     // read dimensions
  74.     size_t width;
  75.     size_t height;
  76.     size_t depth;
  77.     istr.read(reinterpret_cast<char*>(&width),  sizeof(size_t));
  78.     istr.read(reinterpret_cast<char*>(&height), sizeof(size_t));
  79.     istr.read(reinterpret_cast<char*>(&depth),  sizeof(size_t));
  80.     // create our sub-image
  81.     CRef<CImage> image(new CImage(w, h, depth));
  82.     if ( !image ) {
  83.         NCBI_THROW(CImageException, eReadError,
  84.                    "CImageIORaw::ReadImage(): failed to allocate image");
  85.     }
  86.     // calculate the bytes per line for our sub-image anf dor the input image
  87.     const size_t input_bpl  = width * depth;
  88.     const size_t output_bpl = w * depth;
  89.     // start reading
  90.     unsigned char* data = image->SetData();
  91.     istr.seekg(input_bpl * y + x * depth, ios::beg);
  92.     for (size_t i = 0;  i < h;  ++i, data += output_bpl) {
  93.         istr.read(reinterpret_cast<char*>(data), output_bpl);
  94.         istr.seekg(input_bpl - output_bpl, ios::cur);
  95.     }
  96.     return image.Release();
  97. }
  98. void CImageIORaw::WriteImage(const CImage& image, CNcbiOstream& ostr,
  99.                              CImageIO::ECompress)
  100. {
  101.     // write the header
  102.     ostr.write(reinterpret_cast<const char*>(sc_Header), 4);
  103.     // write dimensions
  104.     size_t width  = image.GetWidth();
  105.     size_t height = image.GetHeight();
  106.     size_t depth  = image.GetDepth();
  107.     ostr.write(reinterpret_cast<const char*>(&width),  sizeof(size_t));
  108.     ostr.write(reinterpret_cast<const char*>(&height), sizeof(size_t));
  109.     ostr.write(reinterpret_cast<const char*>(&depth),  sizeof(size_t));
  110.     // write the image data
  111.     ostr.write(reinterpret_cast<const char*>(image.GetData()),
  112.                  width * height * depth);
  113. }
  114. void CImageIORaw::WriteImage(const CImage& image, CNcbiOstream& ostr,
  115.                              size_t x, size_t y, size_t width, size_t height,
  116.                              CImageIO::ECompress)
  117. {
  118.     // write the header
  119.     ostr.write(reinterpret_cast<const char*>(sc_Header), 4);
  120.     // write dimensions
  121.     size_t depth  = image.GetDepth();
  122.     ostr.write(reinterpret_cast<const char*>(&width),  sizeof(size_t));
  123.     ostr.write(reinterpret_cast<const char*>(&height), sizeof(size_t));
  124.     ostr.write(reinterpret_cast<const char*>(&depth),  sizeof(size_t));
  125.     // calculate the bytes per line for our sub-image anf dor the input image
  126.     const size_t input_bpl  = image.GetWidth() * depth;
  127.     const size_t output_bpl = width * depth;
  128.     // write the image data
  129.     const unsigned char* data = image.GetData();
  130.     data += input_bpl * y;
  131.     for (size_t i = 0;  i < height;  ++i, data += input_bpl) {
  132.         ostr.write(reinterpret_cast<const char*>(data), output_bpl);
  133.     }
  134. }
  135. END_NCBI_SCOPE
  136. /*
  137.  * ===========================================================================
  138.  * $Log: image_io_raw.cpp,v $
  139.  * Revision 1000.1  2004/06/01 19:41:39  gouriano
  140.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2
  141.  *
  142.  * Revision 1.2  2004/05/17 21:07:58  gorelenk
  143.  * Added include of PCH ncbi_pch.hpp
  144.  *
  145.  * Revision 1.1  2003/12/16 15:48:11  dicuccio
  146.  * Added support for RAW image files
  147.  *
  148.  * Revision 1.2  2003/11/03 15:19:57  dicuccio
  149.  * Added optional compression parameter
  150.  *
  151.  * Revision 1.1  2003/06/03 15:17:13  dicuccio
  152.  * Initial revision of image library
  153.  *
  154.  * ===========================================================================
  155.  */