images_document.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: images_document.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <sstream>
00028 #include <iostream>
00029 #include <fstream>
00030 #include <cassert>
00031 #include "images_document.h"
00032 #include "images_scanner.h"
00033 #include "image_page.h"
00034 #include "utils.h"
00035 #include "log.h"
00036 
00037 namespace images
00038 {
00039 
00040 ImagesDocument::ImagesDocument(void) : initial_page_num(1)
00041 {
00042     LOGPRINTF("entry");
00043 }
00044 
00045 ImagesDocument::~ImagesDocument(void)
00046 {
00047     LOGPRINTF("entry");
00048 
00049     close_document();
00050 }
00051 
00052 bool ImagesDocument::open_document(const std::string &path, bool to_scan_dir)
00053 {
00054     using namespace images;
00055 
00056     ImagesScanner scanner;
00057 
00058     initial_page_num = scanner.scan_images(path,
00059                                            to_scan_dir,
00060                                            false,
00061                                            BY_FILEPATH, 
00062                                            true,
00063                                            images) + 1;
00064     LOGPRINTF("%d", initial_page_num);
00065 
00066     return true;
00067 }
00068 
00069 bool ImagesDocument::is_open()
00070 {
00071     LOGPRINTF("%d", images.size());
00072 
00073     return (images.size() > 0);
00074 }
00075 
00076 unsigned int ImagesDocument::page_count()
00077 {
00078     LOGPRINTF("%d", images.size());
00079 
00080     return static_cast<unsigned int>(images.size());
00081 }
00082 
00083 int ImagesDocument::get_initial_page_num()
00084 {
00085     return initial_page_num;
00086 }
00087 
00088 const ImagePtr ImagesDocument::get_page(const unsigned int page_number)
00089 {
00090     assert(page_number >= 1 && page_number <= images.size()); 
00091 
00092     LOGPRINTF("page[%d] %s", 
00093             page_number, 
00094             images[page_number - 1]->path.c_str());
00095     
00096     return images[page_number - 1];
00097 }
00098 
00099 bool ImagesDocument::close_document()
00100 {
00101     LOGPRINTF("entry");
00102 
00103     ImagesIter begin = images.begin();
00104     ImagesIter end   = images.end();
00105     for(ImagesIter iter = begin; iter != end; ++iter)
00106     {
00107         delete *iter;
00108     }
00109     images.clear();
00110     return true;
00111 }
00112 
00113 bool ImagesDocument::get_anchor_of_page(const unsigned int page_number,
00114                                        std::string & anchor)
00115 {
00116     LOGPRINTF("entry %d", page_number);
00117 
00118     if (page_number >= 1 && page_number <= images.size())
00119     {
00120         anchor = images[page_number - 1]->path;
00121 
00122         LOGPRINTF("%s", images[page_number -1]->path.c_str());
00123 
00124         return true;
00125     }
00126     else
00127     {
00128         return false;
00129     }
00130 }
00131 
00132 bool ImagesDocument::has_anchor(const std::string & anchor)
00133 {
00134     LOGPRINTF("%s", anchor.c_str());
00135 
00136     return (get_position(anchor) >= 0);
00137 }
00138 
00139 int ImagesDocument::compare_anchor(const std::string & first,
00140                                   const std::string & second)
00141 {
00142     int first_pos = get_position(first);
00143     int second_pos = get_position(second);
00144 
00145     LOGPRINTF("first_pos[%d] second_pos[%d]", first_pos, second_pos);
00146 
00147     return first_pos - second_pos;
00148 }
00149 
00150 int ImagesDocument::get_position(const std::string & path)
00151 {
00152     ImagesIter begin = images.begin();
00153     ImagesIter end   = images.end();
00154     for(ImagesIter iter = begin; iter != end; ++iter)
00155     {
00156         if ((*iter)->path == path)
00157         {
00158             LOGPRINTF("%d", static_cast<int>((iter - begin) + 1));
00159 
00160             return static_cast<int>((iter - begin) + 1);
00161         }
00162     }
00163     return -1;
00164 }
00165 
00166 bool ImagesDocument::get_page_name(const std::string & anchor,
00167                                    std::string & name)
00168 {
00169     if (has_anchor(anchor))
00170     {
00171         char filename[MAX_PATH_LEN];
00172         if (get_file_name(anchor.c_str(), filename, MAX_PATH_LEN))
00173         {
00174             name = filename;
00175             return true;
00176         }
00177     }
00178     return false;
00179 }
00180 
00181 bool ImagesDocument::get_prev_page(std::string & anchor)
00182 {
00183     ImagesIter begin = images.begin();
00184     ImagesIter end   = images.end();
00185     ImagesIter iter;
00186     for(iter = begin; iter != end; ++iter)
00187     {
00188         if ((*iter)->path == anchor)
00189         {
00190             break;
00191         }
00192     }
00193     
00194     // Check the iter position.
00195     if (iter == end || iter == begin)
00196     {
00197         return false;
00198     }
00199     anchor = (*--iter)->path;
00200 
00201     LOGPRINTF("%s", anchor.c_str());
00202 
00203     return true;
00204 }
00205 
00206 bool ImagesDocument::get_next_page(std::string & anchor)
00207 {
00208     ImagesIter begin = images.begin();
00209     ImagesIter end   = images.end();
00210     ImagesIter iter;
00211     for(iter = begin; iter != end; ++iter)
00212     {
00213         if ((*iter)->path == anchor)
00214         {
00215             break;
00216         }
00217     }
00218     
00219     if (iter == end || ++iter == end)
00220     {
00221         return false;
00222     }
00223     anchor = (*iter)->path;
00224 
00225     LOGPRINTF("%s", anchor.c_str());
00226 
00227     return true;
00228 }
00229 
00230 bool ImagesDocument::get_original_size(const std::string &anchor, 
00231                                        unsigned int & width,
00232                                        unsigned int & height)
00233 {
00234      ImagesIter begin = images.begin();
00235      ImagesIter end   = images.end();
00236      ImagesIter iter;
00237      for(iter = begin; iter != end; ++iter)
00238      {
00239          if ((*iter)->path == anchor)
00240          {
00241              // The image size is not calculated yet, calculate it now.
00242              if (((*iter)->width == -1) || ((*iter)->height == -1))
00243              {
00244                  ImagesScanner::get_size((*iter)->path, 
00245                                          &(*iter)->width, 
00246                                          &(*iter)->height);
00247              }
00248              
00249              // The valid image's size is greater than zero.
00250              if (((*iter)->width > 0) && ((*iter)->height > 0))
00251              {
00252                  width = (*iter)->width;
00253                  height = (*iter)->height;
00254                  
00255                  return true;
00256              }
00257              
00258              return false;
00259          }
00260      }
00261      
00262      return false;
00263 }
00264 
00265 bool ImagesDocument::get_original_rotation(const std::string & anchor,
00266                                            PluginRotationDegree & rotation)
00267 {
00268      ImagesIter begin = images.begin();
00269      ImagesIter end   = images.end();
00270      ImagesIter iter;
00271      for(iter = begin; iter != end; ++iter)
00272      {
00273          if ((*iter)->path == anchor)
00274          {
00275              // The image rotation is not calculated yet, calculate it now.
00276              if ( !((*iter)->is_rotation_calculated) )
00277              {
00278                  ImagesScanner::get_original_rotation((*iter)->path, (*iter)->rotation);
00279                  (*iter)->is_rotation_calculated = true;
00280              }
00281 
00282              rotation = (*iter)->rotation;
00283              
00284              return true;
00285          }
00286      }
00287      return false;
00288 }
00289 
00290 }
00291 
00292 
Generated by  doxygen 1.6.2-20100208