File: pix_image.cpp

package info (click to toggle)
gem 1%3A0.93.3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,548 kB
  • ctags: 29,332
  • sloc: cpp: 134,188; sh: 11,215; makefile: 2,853; ansic: 84
file content (230 lines) | stat: -rw-r--r-- 5,831 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.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.
//
/////////////////////////////////////////////////////////
#include "Gem/GemConfig.h"
#include "pix_image.h"

#include "Gem/State.h"

#ifdef _WIN32
# include <io.h>
# define close _close
# define snprintf _snprintf
#endif

#if defined(__unix__) || defined(__APPLE__) 
# include <unistd.h>
# include <strings.h>
#endif

#include <stdio.h>
#include "Gem/Cache.h"

CPPEXTERN_NEW_WITH_ONE_ARG(pix_image, t_symbol *, A_DEFSYM);



/////////////////////////////////////////////////////////
//
// pix_image
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_image :: pix_image(t_symbol *filename) :
  m_wantThread(true),
  m_loadedImage(NULL),
  m_id(gem::image::load::INVALID),
  m_infoOut(gem::RTE::Outlet(this))
{
  if(filename!=&s_)openMess(filename->s_name);
  gem::image::load::poll();
}

////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_image :: ~pix_image()
{
  gem::image::load::cancel(m_id);
  cleanImage();
  m_id=gem::image::load::INVALID;
}

void pix_image :: threadMess(bool onoff)
{
  m_wantThread=onoff;
}

/////////////////////////////////////////////////////////
// openMess
//
/////////////////////////////////////////////////////////
void pix_image :: openMess(std::string filename)
{
  if(filename.empty())return;

  gem::image::load::cancel(m_id);
 
  m_filename = findFile(filename);

  gem::image::load::callback cb = loadCallback;
  void*userdata=reinterpret_cast<void*>(this);

  m_id = gem::image::load::INVALID;

  bool success=false;
  if(m_wantThread) {
    success=gem::image::load::async(cb, userdata, m_filename, m_id);
  } else {
    success=gem::image::load:: sync(cb, userdata, m_filename, m_id);
  }
  if(gem::image::load::INVALID == m_id)
    success=false;

  std::vector<gem::any>atoms;
  gem::any value;

  if(success) {
    if(gem::image::load::IMMEDIATE!=m_id) {
      verbose(1, "loading image '%s' with ID:%d", m_filename.c_str(), m_id);
      atoms.push_back(value=std::string("defer"));
      atoms.push_back(value=(int)m_id);
    } else {
      atoms.push_back(value=std::string("success"));
    }
  } else {
    error("loading of '%s' failed", m_filename.c_str());
    atoms.push_back(value=std::string("fail"));
  }
  atoms.push_back(value=m_filename);
  m_infoOut.send("load", atoms);
}


void    pix_image:: loaded(const gem::image::load::id_t ID, 
			   imageStruct*img,
			   const gem::Properties&props) {
  std::vector<gem::any>atoms;
  gem::any value;

  if(ID!=m_id || ID == gem::image::load::INVALID) {
    atoms.push_back(value=std::string("discard"));
    if(ID!=gem::image::load::INVALID)
      atoms.push_back(value=(int)ID);
    verbose(1, "discarding image with ID %d", ID);
    m_infoOut.send("load", atoms);
    return;
  }

  cleanImage();
  if(img) {
    m_loadedImage=img;
    m_loadedImage->copy2Image(&m_pixBlock.image);
    m_pixBlock.newimage = 1;
    verbose(0, "loaded image '%s'", m_filename.c_str());
    atoms.push_back(value=std::string("success"));
  } else {
    error("failed to load image '%s'", m_filename.c_str());
    atoms.push_back(value=std::string("fail"));
  }
  atoms.push_back(value=(int)ID);
  if(gem::image::load::IMMEDIATE!=m_id) {
    m_infoOut.send("load", atoms);
  }
}
void    pix_image:: loadCallback(void*data,
				 gem::image::load::id_t ID, 
				 imageStruct*img,
				 const gem::Properties&props) {
  pix_image*me=reinterpret_cast<pix_image*>(data);
  me->loaded(ID, img, props);
}



    	    	

/////////////////////////////////////////////////////////
// render
//
/////////////////////////////////////////////////////////
void pix_image :: render(GemState *state)
{
  gem::image::load::poll();

  // if we don't have an image, just return
  if (!m_loadedImage){
    return;
  }

  // do we need to reload the image?    
  if (m_cache&&m_cache->resendImage)
    {
      m_loadedImage->refreshImage(&m_pixBlock.image);
      m_pixBlock.newimage = 1;
      m_cache->resendImage = 0;
    }
  state->set(GemState::_PIX, &m_pixBlock);
}

/////////////////////////////////////////////////////////
// postrender
//
/////////////////////////////////////////////////////////
void pix_image :: postrender(GemState *state)
{
  m_pixBlock.newimage = 0;
  state->set(GemState::_PIX, static_cast<pixBlock*>(NULL));
}

/////////////////////////////////////////////////////////
// startRendering
//
/////////////////////////////////////////////////////////
void pix_image :: startRendering()
{
  if (!m_loadedImage) return;
  m_loadedImage->refreshImage(&m_pixBlock.image);
  m_pixBlock.newimage = 1;
}

/////////////////////////////////////////////////////////
// cleanImage
//
/////////////////////////////////////////////////////////
void pix_image :: cleanImage()
{
  // release previous data
  if (m_loadedImage)
    {
      delete m_loadedImage;
      m_loadedImage = NULL;
      m_pixBlock.image.clear();
      m_pixBlock.image.data = NULL;
    }
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_image :: obj_setupCallback(t_class *classPtr)
{
  CPPEXTERN_MSG1(classPtr, "open", openMess, std::string);
  CPPEXTERN_MSG1(classPtr, "thread", threadMess, bool);
}