File: Codecs.hh

package info (click to toggle)
exactimage 0.9.1-16
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,264 kB
  • ctags: 6,386
  • sloc: cpp: 33,621; ansic: 1,730; xml: 1,590; makefile: 333; perl: 138; sh: 109; python: 45; php: 37
file content (126 lines) | stat: -rw-r--r-- 4,546 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2006 - 2010 René Rebe
 * 
 * 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; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */

/* The Image decoder and coder collection.
 *
 * The class itself has static methods to perform the de- and
 * encoding. These methods search thru a loader list to match the
 * file magick (on decoding) or the specified codec / file extension
 * on encoding.
 *
 * The codec might attach a freestanding instance of itself onto the
 * image on decode to allow on-the-fly decoding and reuse of coded
 * data while re-encoding an image where nothing or just meta data was
 * touched while the pixel data remains unmodified.
 *
 * Also some specialized methods are available to optimize some
 * operations that sometimes can work on encoded data.
 */

#ifndef IMAGELOADER_HH
#define IMAGELOADER_HH

#include <stdio.h>

#include <list>
#include <algorithm>
#include <iosfwd>

#include "Image.hh"

// just forward
class Image;

class ImageCodec
{
public:
  ImageCodec ();
  ImageCodec (Image* __image);
  virtual ~ImageCodec ();
  virtual std::string getID () = 0;

  // helpers for parsing the codec spec and filename extension
  static std::string getCodec (std::string& filename);
  static std::string getExtension (const std::string& filename);

  // NEW API, allowing the use of any STL i/o stream derived source. The index i
  // is the page, image, index number within the file (TIFF, GIF, ICO, etc.)
  static int Read (std::istream* stream, Image& image,
		   std::string codec = "", const std::string& decompress = "", int index = 0);
  static bool Write (std::ostream* stream, Image& image,
		     std::string codec, std::string ext = "",
		     int quality = 75, const std::string& compress = "");

  static ImageCodec* MultiWrite (std::ostream* stream,
				 std::string codec, std::string ext = "");
  
  // OLD API, only left for compatibility.
  // Not const string& because the filename is parsed and the copy is changed intern.
  // 
  // Removed soon!
  static int Read (std::string file, Image& image, const std::string& decompress = "", int index = 0);
  static bool Write (std::string file, Image& image,
		     int quality = 75, const std::string& compress = "");
  
  // Per codec methods, only one set needs to be implemented, the one with
  // index invoke the one without by default (compatibility, ease of implemntation
  // fallback), the ones without return error (as in "not implemented").
  virtual int readImage (std::istream* stream, Image& image,
			 const std::string& decompress);
  virtual int readImage (std::istream* stream, Image& image,
			 const std::string& decompress, int index);

  virtual bool writeImage (std::ostream* stream, Image& image,
			   int quality, const std::string& compress) = 0;
  virtual ImageCodec* instanciateForWrite (std::ostream* stream);
  // slightly named differently to match the public factory name
  virtual bool Write (Image& image,
		      int quality = 75, const std::string& compress = "", int index = 0);
  
  // not pure-virtual so not every codec needs a NOP
  virtual /*bool*/ void decodeNow (Image* image);
  
  // optional optimizing and/or lossless implementations (JPEG, et al.)
  virtual bool flipX (Image& image);
  virtual bool flipY (Image& image);
  virtual bool rotate (Image& image, double angle);
  virtual bool crop (Image& image, unsigned int x, unsigned int y, unsigned int w, unsigned int h);
  virtual bool toGray (Image& image);
  virtual bool scale (Image& image, double xscale, double yscale);
  
protected:
  
  struct loader_ref {
    const char* ext;
    ImageCodec* loader;
    bool primary_entry;
    bool via_codec_only;
  };
  
  static std::list<loader_ref>* loader;
  
  static void registerCodec (const char* _ext,
			     ImageCodec* _loader,
			     bool _via_codec_only = false,
			     bool push_back = false);
  static void unregisterCodec (ImageCodec* _loader);
  
  // freestanding instance, attached to an image
  const Image* _image;
};

#endif