1 | #include "sopnamsp.h"
|
---|
2 | #include "machdefs.h"
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <iostream>
|
---|
6 | #include <math.h>
|
---|
7 |
|
---|
8 | #include <typeinfo>
|
---|
9 |
|
---|
10 | #include <vector>
|
---|
11 | #include <string>
|
---|
12 |
|
---|
13 | #include "piacmd.h"
|
---|
14 | #include "nobjmgr.h"
|
---|
15 | #include "pistdimgapp.h"
|
---|
16 | #include "servnobjm.h"
|
---|
17 | #include "picmap.h"
|
---|
18 | #include "srandgen.h"
|
---|
19 |
|
---|
20 | #include "imgmag_pi.h"
|
---|
21 | //--- Pour WidgetToImgMag()
|
---|
22 | #include <X11/Xlib.h>
|
---|
23 | #include <X11/Xutil.h>
|
---|
24 | #include <X11/Xatom.h>
|
---|
25 |
|
---|
26 | //-----------------------------------------------------------------------
|
---|
27 | // Module chargeable de gestion d'images (jpeg,gif...) dans piapp
|
---|
28 | // Ce module utilise la librairie ImageMagick
|
---|
29 |
|
---|
30 | // Declaration de la fonction d'activation et de desactivation du module
|
---|
31 | extern "C" {
|
---|
32 | void imgmagmod_init();
|
---|
33 | void imgmagmod_end();
|
---|
34 | }
|
---|
35 |
|
---|
36 | // Fonction pour creer et remplir un ImgMagObj en copiant le contenu d'un widget
|
---|
37 | ImgMagObj* WidgetToImgMag(PIWdg* wdg);
|
---|
38 |
|
---|
39 | // Une classe commande-executor, permettant d'enregistrer de
|
---|
40 | // nouvelles commandes a piapp
|
---|
41 | class imgmagmodExecutor : public CmdExecutor {
|
---|
42 | public:
|
---|
43 | imgmagmodExecutor();
|
---|
44 | virtual ~imgmagmodExecutor();
|
---|
45 | // Execute s'occupe de l'execution effective des commandes
|
---|
46 | virtual int Execute(string& keyw, vector<string>& args, string& toks);
|
---|
47 | };
|
---|
48 |
|
---|
49 | /* --Methode-- */
|
---|
50 | imgmagmodExecutor::imgmagmodExecutor()
|
---|
51 | {
|
---|
52 |
|
---|
53 | PIACmd * mpiac;
|
---|
54 | NamedObjMgr omg;
|
---|
55 | mpiac = omg.GetImgApp()->CmdInterpreter();
|
---|
56 |
|
---|
57 | // On enregistre deux nouvelles commandes
|
---|
58 | string hgrp = "ImageMagick";
|
---|
59 | // commande readphoto
|
---|
60 | string kw = "readphoto";
|
---|
61 | string usage = "readphoto: Read an image file (jpeg/gif ...) \n" ;
|
---|
62 | usage += "An object of type ImgMagObj (wrapper for Magick::Image) is \n";
|
---|
63 | usage += "created and managed by the NamedObjMgr\n";
|
---|
64 | usage += " Usage: readphoto ImageFileName \n";
|
---|
65 | usage += " See also writephoto \n";
|
---|
66 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
67 | // commande pirgb_rdphoto
|
---|
68 | kw = "pirgb_rdphoto";
|
---|
69 | usage = "pirgb_rdphoto: Read an PI_RGB file (.rgb) into a ImgMagObj object\n" ;
|
---|
70 | usage += " Usage: pirgb_rdphoto PI_RGB_FileName \n";
|
---|
71 | usage += " See also readphoto \n";
|
---|
72 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
73 | // commande writephoto
|
---|
74 | kw = "writephoto";
|
---|
75 | usage = "writephoto: Save an ImgMagObj object to an image file (jpeg/gif ...) \n" ;
|
---|
76 | usage += " Usage: writephoto ObjName OutFileName \n";
|
---|
77 | usage += " - ObjName : ImgMagObj object name \n";
|
---|
78 | usage += " - OutFileName : .jpg .gif .tiff output filename \n";
|
---|
79 | usage += " See also readphoto pirgb_wrphoto";
|
---|
80 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
81 | // commande pirgb_wrphoto
|
---|
82 | kw = "pirgb_wrphoto";
|
---|
83 | usage = "pirgb_wrphoto: Save an ImgMagObj object to a PI_RGB file (.rgb) \n" ;
|
---|
84 | usage += " Usage: pirgb_wrphoto ObjName OutFileName \n";
|
---|
85 | usage += " See also writephoto ";
|
---|
86 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
87 | // commande wdg2imgfile win2imgfile
|
---|
88 | kw = "wdg2imgfile";
|
---|
89 | usage = "wdg2imgfile: Save current widget to image file \n" ;
|
---|
90 | usage += " Usage: wdg2imgfile OutFileName \n";
|
---|
91 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
92 | kw = "win2imgfile";
|
---|
93 | usage = "win2imgfile: Save current window to image file \n" ;
|
---|
94 | usage += " Usage: win2imgfile OutFileName \n";
|
---|
95 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* --Methode-- */
|
---|
100 | imgmagmodExecutor::~imgmagmodExecutor()
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* --Methode-- */
|
---|
105 | int imgmagmodExecutor::Execute(string& kw, vector<string>& tokens, string&)
|
---|
106 | {
|
---|
107 |
|
---|
108 | NamedObjMgr omg;
|
---|
109 | if ((kw=="readphoto") || (kw=="pirgb_rdphoto")) {
|
---|
110 | if (tokens.size() < 1) {
|
---|
111 | cout << "Usage: readphoto/pirgb_rdphoto FileName" << endl;
|
---|
112 | return(0);
|
---|
113 | }
|
---|
114 | cout << " readphoto: reading file: " << tokens[0] << endl;
|
---|
115 | Services2NObjMgr* serv = omg.GetServiceObj();
|
---|
116 | string objname = serv->FileName2Name(tokens[0]);
|
---|
117 | ImgMagObj* img = NULL;
|
---|
118 | if (kw=="readphoto") img = new ImgMagObj(tokens[0]);
|
---|
119 | else {
|
---|
120 | PIPixRGBArray rgbimg(tokens[0].c_str());
|
---|
121 | img = new ImgMagObj(rgbimg);
|
---|
122 | }
|
---|
123 | if (img != NULL) omg.AddObj(img, objname);
|
---|
124 | }
|
---|
125 | else if ((kw=="writephoto") || (kw=="pirgb_wrphoto")) {
|
---|
126 | if (tokens.size() < 2) {
|
---|
127 | cout << "Usage: writephoto/pirgb_wrphoto ObjName OutFileName" << endl;
|
---|
128 | return(0);
|
---|
129 | }
|
---|
130 | AnyDataObj* obj=omg.GetObj(tokens[0]);
|
---|
131 | if (obj == NULL) {
|
---|
132 | cout << " No object with name " << tokens[0] << endl;
|
---|
133 | return 1;
|
---|
134 | }
|
---|
135 | ImgMagObj* img = dynamic_cast<ImgMagObj*>(obj);
|
---|
136 | if (img == NULL) {
|
---|
137 | cout << " Object with name " << tokens[0] << " Not an ImgMagObj object" << endl;
|
---|
138 | return 2;
|
---|
139 | }
|
---|
140 | if (kw=="writephoto") {
|
---|
141 | cout << " writephoto: saving" << tokens[0] << " to file " << tokens[1] << endl;
|
---|
142 | img->write(tokens[1]);
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 | else if (kw=="pirgb_wrphoto") {
|
---|
146 | cout << " pirgb_wrphoto: saving" << tokens[0] << " to PI_RGB file " << tokens[1] << endl;
|
---|
147 | PIPixRGBArray rgba(0,0);
|
---|
148 | img->FillPIRGBArray(rgba);
|
---|
149 | rgba.SaveToFile(tokens[1].c_str());
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else if ((kw=="wdg2imgfile")||(kw=="win2imgfile")) {
|
---|
154 | if (tokens.size() < 1) {
|
---|
155 | cout << "Usage: wdg2imgfile/win2imgfile FileName" << endl;
|
---|
156 | return(0);
|
---|
157 | }
|
---|
158 | PIWdg* wdg = NULL;
|
---|
159 | if (kw=="wdg2imgfile") wdg = (PIWdg *)omg.GetImgApp()->CurrentBaseWdg();
|
---|
160 | else wdg = (PIWdg *)omg.GetImgApp()->CurrentWindow();
|
---|
161 | if (wdg == NULL) {
|
---|
162 | cout << " wdg2imgfile/win2imgfile: No current window/widget " << endl;
|
---|
163 | return 1;
|
---|
164 | }
|
---|
165 | ImgMagObj* img = WidgetToImgMag(wdg);
|
---|
166 | if (img) {
|
---|
167 | cout << " wdg2imgfile : saving current widget to file " << tokens[0] << endl;
|
---|
168 | img->write(tokens[0]);
|
---|
169 | }
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | return(0);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static imgmagmodExecutor * imgmagex = NULL;
|
---|
177 | static PIColorMap * cmap32768 = NULL;
|
---|
178 | /* Nouvelle-Fonction */
|
---|
179 | void imgmagmod_init()
|
---|
180 | {
|
---|
181 | // Fonction d'initialisation du module
|
---|
182 | cout << " imgmagmod_init() : Initializing ImageMagick interface piapp-module ..." << endl;
|
---|
183 | // Appele par le gestionnaire de modules de piapp (PIACmd::LoadModule())
|
---|
184 | if (imgmagex) delete imgmagex;
|
---|
185 | imgmagex = new imgmagmodExecutor;
|
---|
186 | NamedObjMgr omg;
|
---|
187 | // On alloue la grosse table de couleur avec 32768 couleurs RGB ...
|
---|
188 | if (cmap32768 == NULL) {
|
---|
189 | omg.GetImgApp()->LockMutex();
|
---|
190 | cmap32768 = new PIColorMap(CMAP_RGB32768);
|
---|
191 | // cmap32768 = new PIColorMap(CMAP_RGB4096);
|
---|
192 | omg.GetImgApp()->UnlockMutex(true);
|
---|
193 | }
|
---|
194 | Services2NObjMgr* serv = omg.GetServiceObj();
|
---|
195 | serv->RegisterClass(new ImgMagObj, new NOMAdapter_ImgMag );
|
---|
196 | cout << " imgmagmod_init() : Initialization OK " << endl;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Nouvelle-Fonction */
|
---|
200 | void imgmagmod_end()
|
---|
201 | {
|
---|
202 | // Desactivation du module
|
---|
203 | if (imgmagex) delete imgmagex;
|
---|
204 | imgmagex = NULL;
|
---|
205 | NamedObjMgr omg;
|
---|
206 | omg.GetImgApp()->CkEvt_LockMutex();
|
---|
207 | if (cmap32768) delete cmap32768;
|
---|
208 | omg.GetImgApp()->UnlockMutex(true);
|
---|
209 | cmap32768 = NULL;
|
---|
210 | }
|
---|
211 |
|
---|
212 | inline void _col2fcol_(unsigned long & col, unsigned long & fcol)
|
---|
213 | {
|
---|
214 | if (col > 0x00FFFFFF) fcol = 0x01000000;
|
---|
215 | else if (col > 0x0000FFFF) fcol = 0x000010000;
|
---|
216 | else if (col > 0x000000FF) fcol = 0x00000100;
|
---|
217 | else fcol = 1;
|
---|
218 | //DBG cout << " _col2fcol_ col= " << col << " fcol=" << fcol << " ->" << col/fcol << endl;
|
---|
219 | //DBG cout << " HEX _col2fcol_ col= " << hex << col << " fcol=" << fcol << " ->" << col/fcol << dec << endl;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* Nouvelle-Fonction */
|
---|
223 | ImgMagObj* WidgetToImgMag(PIWdg* wdg)
|
---|
224 | {
|
---|
225 | if (wdg == NULL) {
|
---|
226 | cout << " WidgetToImgMag() wdg=NULL " << endl;
|
---|
227 | return NULL;
|
---|
228 | }
|
---|
229 | //DBG cout << "------ WidgetToImgMag() ------" << endl;
|
---|
230 |
|
---|
231 | NamedObjMgr omg;
|
---|
232 | omg.GetImgApp()->CkEvt_LockMutex();
|
---|
233 | Display* mdsp = PIXDisplay();
|
---|
234 | int scr = XDefaultScreen(mdsp);
|
---|
235 | int depth = DefaultDepth(mdsp,scr);
|
---|
236 | Window xw = XtWindow(wdg->XtWdg());
|
---|
237 | unsigned long plane_mask = ~0; // tous les bits a 1
|
---|
238 | XImage * ximg = XGetImage(mdsp, xw, 0, 0, wdg->XSize(), wdg->YSize(),
|
---|
239 | plane_mask, ZPixmap);
|
---|
240 | //DBG cout << " Resultat XGetImage -> " << hex << ximg << dec << endl;
|
---|
241 | if (ximg == NULL) return NULL;
|
---|
242 |
|
---|
243 | unsigned long red,green,blue;
|
---|
244 | unsigned long fred,fgreen,fblue;
|
---|
245 |
|
---|
246 | XColor col;
|
---|
247 | Colormap cmap = XDefaultColormap (mdsp, scr);
|
---|
248 | red = 0x000000FF;
|
---|
249 | green = 0x0000FF00;
|
---|
250 | blue = 0x00FF0000;
|
---|
251 | unsigned short maxrgb = ~0;
|
---|
252 | col.red = maxrgb; col.green = col.blue = 0;
|
---|
253 | if (XAllocColor(mdsp, cmap, &col)) {
|
---|
254 | //DBG cout << " --DBG red-color , R,G,B=" << hex << col.red << "," << col.green << ","
|
---|
255 | // << col.blue << " -> pixel=" << col.pixel << dec << endl;
|
---|
256 | red = col.pixel;
|
---|
257 | }
|
---|
258 | col.green = maxrgb; col.red = col.blue = 0;
|
---|
259 | if (XAllocColor(mdsp, cmap, &col)) {
|
---|
260 | //DBG cout << " --DBG green-color , R,G,B=" << hex << col.red << "," << col.green << ","
|
---|
261 | // << col.blue << " -> pixel=" << col.pixel << dec << endl;
|
---|
262 | green = col.pixel;
|
---|
263 | }
|
---|
264 | col.blue = maxrgb; col.red = col.green = 0;
|
---|
265 | if (XAllocColor(mdsp, cmap, &col)) {
|
---|
266 | //DBG cout << " --DBG blue-color , R,G,B=" << hex << col.red << "," << col.green << ","
|
---|
267 | // << col.blue << " -> pixel=" << col.pixel << dec << endl;
|
---|
268 | blue = col.pixel;
|
---|
269 | }
|
---|
270 | _col2fcol_(red, fred);
|
---|
271 | _col2fcol_(green, fgreen);
|
---|
272 | _col2fcol_(blue, fblue);
|
---|
273 |
|
---|
274 | ImgMagObj* img = new ImgMagObj(wdg->XSize(), wdg->YSize());
|
---|
275 | Magick::ColorRGB rgb;
|
---|
276 |
|
---|
277 | int errcnt[3] = {0,0,0};
|
---|
278 | int errcnt2[3] = {0,0,0};
|
---|
279 | unsigned long pixel;
|
---|
280 | for(int j=0; j<wdg->YSize(); j++)
|
---|
281 | for(int i=0; i<wdg->XSize(); i++) {
|
---|
282 | pixel = XGetPixel(ximg, i, j);
|
---|
283 | double xcc[3];
|
---|
284 | xcc[0] = (double)((pixel&red)/fred)/255.;
|
---|
285 | xcc[1] = (double)((pixel&green)/fgreen)/255.;
|
---|
286 | xcc[2] = (double)((pixel&blue)/fblue)/255.;
|
---|
287 | // Il faut proteger contre <0 et >1 sinon exception ImageMagck !
|
---|
288 | for(int kk=0; kk<3;kk++) {
|
---|
289 | if (xcc[kk]<=0.) { xcc[kk] = 0.; errcnt[kk]++; }
|
---|
290 | if (xcc[kk]>=1.) { xcc[kk] = 1.; errcnt2[kk]++; }
|
---|
291 | }
|
---|
292 | Magick::ColorRGB mcol(xcc[0], xcc[1], xcc[2]);
|
---|
293 | // Magick::ColorRGB mcol(0.5+0.2*frandpm1(), 0.5+0.35*frandpm1(), 0.6+0.2*frandpm1());
|
---|
294 | img->pixelColor(i,j,mcol);
|
---|
295 | }
|
---|
296 | /*
|
---|
297 | for(int kk=0; kk<3;kk++)
|
---|
298 | cout << " ErrCnt k=" << kk << " : " << errcnt[kk] << "," << errcnt2[kk] << endl;
|
---|
299 | */
|
---|
300 | XDestroyImage(ximg);
|
---|
301 | omg.GetImgApp()->UnlockMutex(true);
|
---|
302 | return img;
|
---|
303 | }
|
---|
304 |
|
---|