File: imageSTB.cpp

package info (click to toggle)
gem 1%3A0.94-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,320 kB
  • sloc: cpp: 174,266; ansic: 42,129; makefile: 3,848; sh: 1,096; objc: 389
file content (152 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.at
//
// Implementation file
//
//    Copyright (c) 1997-1999 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include "imageSTB.h"
#include "Gem/RTE.h"
#include "plugins/PluginFactory.h"

#ifndef HAVE_LIBSTB
# define STB_IMAGE_IMPLEMENTATION
# define STB_IMAGE_WRITE_IMPLEMENTATION
#endif
#ifdef HAVE_STB_STB_IMAGE_H
# include "stb/stb_image.h"
# include "stb/stb_image_write.h"
#else
# include "stb_image.h"
# include "stb_image_write.h"
#endif

using namespace gem::plugins;

REGISTER_IMAGELOADERFACTORY("STB", imageSTB);
REGISTER_IMAGESAVERFACTORY ("STB", imageSTB);

/////////////////////////////////////////////////////////
//
// imageSTB
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
imageSTB :: imageSTB(void)
{
}
imageSTB :: ~imageSTB(void)
{
}

/////////////////////////////////////////////////////////
// really open the file ! (OS dependent)
//
/////////////////////////////////////////////////////////
bool imageSTB :: load(std::string filename, imageStruct&result,
                      gem::Properties&props)
{
  int xsize, ysize, csize;
  unsigned char *data = stbi_load(filename.c_str(), &xsize, &ysize, &csize, 4);

  if(!data)
    return(false);

  result.xsize=xsize;
  result.ysize=ysize;
  result.setCsizeByFormat(GEM_RGBA);
  result.reallocate();

#ifdef __APPLE__
  result.fromARGB(data);
  result.swapRedBlue();
#else
  result.fromRGBA(data);
#endif

  stbi_image_free(data);
  return true;
}

bool imageSTB::save(const imageStruct&image, const std::string&filename,
                    const std::string&mimetype, const gem::Properties&props)
{
  int err = 0;
  imageStruct img;
  double fquality=100;
  int quality=fquality;

  if(props.get("quality", fquality)) {
    quality=fquality;
  }

  image.convertTo(&img, GEM_RGBA);
#ifdef __APPLE__
  /* OSX postprocessing to get really RGBA */
  img.fromABGR(img.data);
#endif /* !APPLE */

  if(!img.upsidedown)
    stbi_flip_vertically_on_write(1);
  if("image/png" == mimetype) {
    err = stbi_write_png(filename.c_str(), img.xsize, img.ysize, img.csize, img.data, img.xsize * img.csize);
  } else if ("image/bmp" == mimetype) {
    err = stbi_write_bmp(filename.c_str(), img.xsize, img.ysize, img.csize, img.data);
  } else if ("image/targa" == mimetype) {
    err = stbi_write_tga(filename.c_str(), img.xsize, img.ysize, img.csize, img.data);
  } else if ("image/jpeg" == mimetype) {
    err = stbi_write_jpg(filename.c_str(), img.xsize, img.ysize, img.csize, img.data, quality);
  }

  return (0!=err);
}
float imageSTB::estimateSave(const imageStruct&img,
                             const std::string&filename, const std::string&mimetype,
                             const gem::Properties&props)
{
  float result=0;
  if("image/png" == mimetype) {
    result+=80;
  } else if ("image/bmp" == mimetype) {
    result+=80;
  } else if ("image/targa" == mimetype) {
    result+=80;
  } else if ("image/jpeg" == mimetype) {
    result+=80;
    if(gem::Properties::UNSET != props.type("quality")) {
      result += 1.;
    }

  }
  return result;
}
void imageSTB::getWriteCapabilities(std::vector<std::string>&mimetypes,
                                    gem::Properties&props)
{
  mimetypes.clear();
  props.clear();
  mimetypes.push_back("image/png");
  mimetypes.push_back("image/bmp");
  mimetypes.push_back("image/targa");
  mimetypes.push_back("image/jpeg");
  //mimetypes.push_back("image/hdr"); // ??; expects float
  gem::any value;

  value=100.f;
  props.set("quality", value);
}