1 /*
2  *  This file is part of XForms.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with XForms; see the file COPYING.  If not, write to
16  *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17  *  MA 02111-1307, USA.
18  */
19 
20 
21 /*
22  * image support test. See also iconvert program
23  *
24  *  T.C. Zhao  (1999.03.01)
25  *
26  *   The GUI (fdui) is placed in image->app_data;
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <stdlib.h>
34 
35 #include "include/forms.h"
36 #include "image/flimage.h"
37 #include "fd/ibrowser_gui.h"   /* from fd/ directory */
38 
39 static FL_IMAGE *curr_image;
40 
41 
42 /***************************************
43  ***************************************/
44 
45 static void
show_image_info(FL_IMAGE * im)46 show_image_info( FL_IMAGE * im )
47 {
48     FD_ibcanvas *fdui;
49     char buf[ 128 ],
50          wl[ 128 ];
51 
52     if ( ! im )
53         return;
54 
55      if ( im->type == FL_IMAGE_GRAY16 )
56          sprintf( wl, "(l=%d w=%d)", im->level, im->wwidth );
57      else
58          wl[ 0 ] = '\0';
59 
60      sprintf( buf, "%s(%s): (%dX%d) t=%s ot=%s dt=%s cols=%d used=%d %s",
61               im->infile,im->fmt_name, im->w,im->h,
62               flimage_type_name( im->type ) + 9,
63               flimage_type_name( im->original_type ) + 9,
64               flimage_type_name( im->display_type ) + 9,
65               im->map_len, im->colors, wl );
66 
67      fdui = im->app_data;
68      fl_set_object_label( fdui->text, buf );
69 
70      if ( im->comments )
71      {
72          fl_activate_object( fdui->comments );
73          fl_set_object_lcolor( fdui->comments, FL_BLACK );
74      }
75      else
76      {
77          fl_deactivate_object( fdui->comments );
78          fl_set_object_lcolor( fdui->comments, FL_INACTIVE_COL );
79      }
80 }
81 
82 
83 /***************************************
84  ***************************************/
85 
86 void
show_comments(FL_OBJECT * ob FL_UNUSED_ARG,long data FL_UNUSED_ARG)87 show_comments( FL_OBJECT * ob    FL_UNUSED_ARG,
88                long        data  FL_UNUSED_ARG )
89 {
90     if ( curr_image && curr_image->comments )
91         fprintf( stderr,"%s\n", curr_image->comments );
92 }
93 
94 
95 /***************************************
96  ***************************************/
97 
98 static int
load_and_show(const char * filename,void * data)99 load_and_show( const char * filename,
100                void       * data )
101 {
102     FD_ibcanvas *fdui = data;
103     FL_IMAGE *image;
104     int r,
105         g,
106         b;
107 
108     image = flimage_load( filename );
109 
110     if ( image )
111     {
112         show_image_info( image );
113         fl_get_icm_color( fdui->canvas->col1, &r, &g, &b );
114         image->app_background = FL_PACK3( r, g, b );
115 #if 0
116         image->sh = image->h / 2;
117         image->sw = image->w / 2;
118 #endif
119         flimage_display( image, FL_ObjWin( fdui->canvas ) );
120         show_image_info( image );
121         flimage_free( curr_image );
122         curr_image = image;
123 #if 0
124         fl_update_display( 1 );
125         sleep( 5 );
126         image->wx = image->wy = 90;
127         image->sx = image->sy = 80;
128         image->sh = image->h - 90;
129         image->sw = image->w - 90;
130         flimage_display( image, FL_ObjWin( fdui->canvas ) );
131 #endif
132 
133         /* Re-adjust the window level stuff */
134 
135         if ( image->type == FL_IMAGE_GRAY16)
136         {
137             fl_set_slider_bounds( fdui->level, 0, image->gray_maxval );
138             fl_set_slider_bounds( fdui->width, 0, image->gray_maxval );
139         }
140     }
141     else
142         fprintf( stderr, "can't load %s\n", filename );
143 
144     return 0;
145 }
146 
147 
148 /***************************************
149  ***************************************/
150 
151 void
scale_image(FL_OBJECT * ob,long data FL_UNUSED_ARG)152 scale_image( FL_OBJECT * ob,
153              long        data  FL_UNUSED_ARG )
154 {
155     FD_ibcanvas *fdui = ob->form->fdui;
156     float xs = fl_get_slider_value( fdui->xfloat );
157     float ys = fl_get_slider_value( fdui->yfloat );
158     int aa = fl_get_button( fdui->anti_aliasing );
159 
160     if ( ! curr_image )
161         return;
162 
163     flimage_scale( curr_image,
164                    xs * curr_image->w, ys * curr_image->h,
165                    aa ? FLIMAGE_SUBPIXEL : FLIMAGE_NOSUBPIXEL );
166     flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
167     show_image_info( curr_image );
168 }
169 
170 
171 /***************************************
172  ***************************************/
173 
174 void
annotate_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)175 annotate_cb( FL_OBJECT * ob,
176              long        data  FL_UNUSED_ARG )
177 {
178     FD_ibcanvas *fdui = ob->form->fdui;
179     char buf[ 128 ];
180     static int cnt;
181     static int tx = -1, ty;
182     static int r, g, b;
183     int mw = 35, mh = 35,  delta = 450, i, j;
184     FLIMAGE_TEXT text;
185 
186     if ( ! curr_image )
187         return;
188 
189     if ( tx < 0 )
190     {
191         tx = curr_image->w / 2;
192         ty = curr_image->h / 2;
193     }
194 
195     sprintf( buf, "Text at[%d %d]\nWith Centered display", tx, ty );
196 
197     if ( ++cnt % 3 == 0 )
198     {
199         r = 255;
200         g = 0;
201         b = 0;
202     }
203     else if ( cnt % 3 == 1 )
204     {
205         r = 0;
206         g = 255;
207         b = 0;
208     }
209     else if ( cnt % 3 == 2 )
210     {
211         r = 0;
212         g = 0;
213         b = 255;
214     }
215 
216     /* positioning check */
217 
218     flimage_add_text( curr_image, "@arrow", 6, 0, 20,
219                       FL_PACK(r,g,b), 0, 1, 30, 30, 0 );
220 
221     flimage_add_text( curr_image, "X", 1, 0, 20,
222                       FL_PACK(r,g,b), 0, 1, 30, 30, 0 );
223 
224     flimage_add_marker( curr_image, "oval",
225                         30,30,
226                         mw - 15, mh - 15, FL_SOLID,
227                         0, 0, FL_PACK( r, g, b ), 0 );
228 
229     flimage_add_text( curr_image, buf, strlen( buf ),
230                       FL_TIMESBOLD_STYLE, 20,
231                       FL_PACK( r, g, b ), FL_PACK( 255, 0, 255 ),
232                       0, tx, ty, 0 );
233 
234     memset( &text, 0, sizeof text );
235     text.align = FL_ALIGN_LEFT;
236     text.str = ( char * ) "qXXd\nqXXd";
237     text.len = strlen( text.str );
238     text.style = FL_TIMESBOLD_STYLE;
239     text.size = 12;
240     text.x = curr_image->w;
241     text.y = curr_image->h;
242     text.color = FL_PACK( r, g, b );
243     text.bcolor = FL_PACK( r, 0, b );
244     text.nobk   = 1;
245 
246     flimage_add_text_struct( curr_image, &text );
247 
248     tx += 5;
249     ty += 5;
250 
251     for ( j = 0; j < 8; j++ )
252     {
253         const char *ss[ ] = { "oval", "delta", "rect", "cross",
254                               "arrow", "thinarrow", "line", "star" };
255         for ( i = 0; i < 9; i++ )
256         {
257             flimage_add_marker( curr_image, ss[ j ],
258                                 curr_image->w - i * mw, curr_image->h- j * mh,
259                                 mw - 15, mh - 15, FL_SOLID,
260                                 i % 2, i * delta, FL_PACK( r, g, b ), 0 );
261         }
262     }
263 
264     flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
265 }
266 
267 
268 /***************************************
269  ***************************************/
270 
271 void
render_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)272 render_cb( FL_OBJECT * ob,
273            long        data  FL_UNUSED_ARG )
274 {
275     FD_ibcanvas *fdui = ob->form->fdui;
276 
277     if ( curr_image )
278     {
279         flimage_render_annotation( curr_image, FL_ObjWin( fdui->canvas ) );
280         show_image_info( curr_image );
281     }
282 }
283 
284 
285 /***************************************
286  ***************************************/
287 
288 void
flip_image(FL_OBJECT * ob,long data)289 flip_image( FL_OBJECT * ob,
290             long        data )
291 {
292      FD_ibcanvas *fdui = ob->form->fdui;
293 
294      if ( ! curr_image )
295          return;
296 
297      flimage_flip( curr_image, data );
298      flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
299 }
300 
301 
302 /***************************************
303  ***************************************/
304 
305 void
rotate_image(FL_OBJECT * ob,long data FL_UNUSED_ARG)306 rotate_image( FL_OBJECT * ob,
307               long        data  FL_UNUSED_ARG )
308 {
309     FD_ibcanvas *fdui = ob->form->fdui;
310     int angle = fl_get_counter_value( fdui->angle );
311     int subpix = fl_get_button( fdui->anti_aliasing );
312     unsigned int fill =  0;
313 
314     if ( ! curr_image )
315         return;
316 
317     curr_image->fill_color = fill;
318     flimage_rotate( curr_image, angle * 10, subpix ? FLIMAGE_SUBPIXEL : 0 );
319     flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
320     show_image_info( curr_image );
321 }
322 
323 
324 /***************************************
325  ***************************************/
326 
327 void
write_image(FL_OBJECT * ob,long data FL_UNUSED_ARG)328 write_image( FL_OBJECT * ob,
329              long        data  FL_UNUSED_ARG )
330 {
331     FD_ibcanvas *fdui = ob->form->fdui;
332     const char *f;
333 
334     if ( ! curr_image )
335         return;
336 
337     fl_use_fselector( 1 );
338     fl_set_fselector_placement( FL_PLACE_MOUSE );
339     if ( ! ( f = fl_show_fselector( "Save Image", NULL, "*", NULL ) ) )
340         return;
341     flimage_dump( curr_image, f, fl_get_choice_text( fdui->format ) );
342 }
343 
344 
345 /***************************************
346  ***************************************/
347 
348 static int
expose_handle(FL_OBJECT * ob FL_UNUSED_ARG,Window win,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * xev FL_UNUSED_ARG,void * data FL_UNUSED_ARG)349 expose_handle( FL_OBJECT * ob   FL_UNUSED_ARG,
350                Window      win,
351                int         w    FL_UNUSED_ARG,
352                int         h    FL_UNUSED_ARG,
353                XEvent    * xev  FL_UNUSED_ARG,
354                void      * data  FL_UNUSED_ARG )
355 {
356     if ( curr_image )
357         flimage_display( curr_image, win );
358     return 0;
359 }
360 
361 
362 /***************************************
363  ***************************************/
364 
365 static int
motion_handle(FL_OBJECT * ob FL_UNUSED_ARG,Window win FL_UNUSED_ARG,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * xev FL_UNUSED_ARG,void * data FL_UNUSED_ARG)366 motion_handle( FL_OBJECT * ob    FL_UNUSED_ARG,
367                Window      win   FL_UNUSED_ARG,
368                int         w     FL_UNUSED_ARG,
369                int         h     FL_UNUSED_ARG,
370                XEvent    * xev   FL_UNUSED_ARG,
371                void      * data  FL_UNUSED_ARG)
372 {
373     if ( ! curr_image )
374         return 0;
375 
376     if ( 1 )
377     {
378 /*      fprintf( stderr, "X=%d Y=%d\n", xev->xmotion.x, xev->xmotion.y ); */
379     }
380 
381     return 0;
382 }
383 
384 
385 /***************************************
386  ***************************************/
387 
388 void
convolve_it(FL_OBJECT * ob,long data)389 convolve_it( FL_OBJECT * ob,
390              long        data )
391 {
392      FD_ibcanvas *fdui = ob->form->fdui;
393      int subimage = fl_get_button( fdui->subimage );
394 
395      if ( ! curr_image )
396          return;
397 
398      if ( subimage )
399      {
400          curr_image->subw = curr_image->w / 2;
401          curr_image->subh = curr_image->h / 2;
402          curr_image->subx = curr_image->w / 4;
403          curr_image->suby = curr_image->h / 4;
404      }
405      else
406          curr_image->subw = 0;
407 
408      flimage_convolve( curr_image, ( int ** )data, 0, 0 );
409      flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
410      show_image_info( curr_image );
411 }
412 
413 
414 /***************************************
415  ***************************************/
416 
417 void
tint_it(FL_OBJECT * ob,long data FL_UNUSED_ARG)418 tint_it( FL_OBJECT * ob,
419          long        data  FL_UNUSED_ARG )
420 {
421      FD_ibcanvas *fdui = ob->form->fdui;
422      int subimage = fl_get_button( fdui->subimage );
423 
424      if ( ! curr_image )
425          return;
426 
427      if ( subimage )
428      {
429          curr_image->subw = curr_image->w / 2;
430          curr_image->subh = curr_image->h / 2;
431          curr_image->subx = curr_image->w / 4;
432          curr_image->suby = curr_image->h / 4;
433      }
434      else
435          curr_image->subw = 0;
436 
437      flimage_tint( curr_image, FL_PACK( 0, 0, 200 ), 0.3 );
438      flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
439      show_image_info( curr_image );
440 }
441 
442 
443 /***************************************
444  ***************************************/
445 
446 void
window_level(FL_OBJECT * ob,long data FL_UNUSED_ARG)447 window_level( FL_OBJECT * ob,
448               long        data  FL_UNUSED_ARG )
449 {
450      FD_ibcanvas *fdui = ob->form->fdui;
451 
452      if ( ! curr_image || curr_image->type != FL_IMAGE_GRAY16 )
453          return;
454 
455      flimage_windowlevel( curr_image, fl_get_slider_value( fdui->level ),
456                           fl_get_slider_value( fdui->width ) );
457      flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
458      show_image_info( curr_image );
459 }
460 
461 
462 /***************************************
463  ***************************************/
464 
465 void
warp_it(FL_OBJECT * ob,long data FL_UNUSED_ARG)466 warp_it( FL_OBJECT * ob,
467          long        data  FL_UNUSED_ARG )
468 {
469     static float m[ 2 ][ 2 ] = { { 0.5, 0.5 }, { -1, 0.7 } };
470     FD_ibcanvas *fdui = ob->form->fdui;
471     int aa = fl_get_button( fdui->anti_aliasing );
472 
473     if ( ! curr_image )
474         return ;
475 
476     curr_image->fill_color = FL_PACK(255,255,0);
477 #if 0
478     flimage_warp( curr_image, border, curr_image->w + 2, curr_image->h + 2,
479                   ( aa ? FLIMAGE_SUBPIXEL : FLIMAGE_NOSUBPIXEL )
480                   | FLIMAGE_CENTER );
481 #else
482     flimage_warp( curr_image, m, 0,0,
483                   aa ? FLIMAGE_SUBPIXEL : FLIMAGE_NOSUBPIXEL );
484 #endif
485     flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
486     show_image_info( curr_image );
487 }
488 
489 
490 /***************************************
491  ***************************************/
492 
493 void
enhance_it(FL_OBJECT * ob,long data FL_UNUSED_ARG)494 enhance_it( FL_OBJECT * ob,
495             long        data  FL_UNUSED_ARG )
496 {
497     FD_ibcanvas *fdui = ob->form->fdui;
498 
499     if ( curr_image )
500     {
501         flimage_enhance( curr_image, 0 );
502         flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
503         show_image_info( curr_image );
504     }
505 }
506 
507 
508 /***************************************
509  ***************************************/
510 
511 void
crop_it(FL_OBJECT * ob,long data)512 crop_it( FL_OBJECT * ob,
513          long        data )
514 {
515     FD_ibcanvas *fdui = ob->form->fdui;
516 
517     if ( ! curr_image )
518         return;
519 
520     if ( data == 0 )
521         flimage_autocrop( curr_image, FLIMAGE_AUTOCOLOR );
522     else
523     {
524         curr_image->fill_color = FL_PACK( 255, 255, 0 );
525         flimage_crop( curr_image, -1, -1, -1, -1 );
526     }
527 
528     flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
529     show_image_info( curr_image );
530 }
531 
532 
533 /***************************************
534  ***************************************/
535 
536 void
convert_type(FL_OBJECT * ob,long type)537 convert_type( FL_OBJECT * ob,
538               long        type )
539 {
540      FD_ibcanvas *fdui = ob->form->fdui;
541 
542      if ( curr_image )
543      {
544          flimage_convert( curr_image, type, 0 );
545          flimage_display( curr_image, FL_ObjWin( fdui->canvas ) );
546          show_image_info( curr_image );
547      }
548 }
549 
550 
551 /***************************************
552  ***************************************/
553 
554 void
browse_file(FL_OBJECT * ob,long data FL_UNUSED_ARG)555 browse_file( FL_OBJECT * ob,
556              long        data  FL_UNUSED_ARG )
557 {
558     fl_use_fselector( 0 );
559     fl_set_fselector_placement( FL_PLACE_MOUSE );
560     fl_set_fselector_callback( load_and_show, ob->form->fdui );
561     fl_show_fselector( "Load an Image",  0, "*", 0 );
562 }
563 
564 
565 /***************************************
566  ***************************************/
567 
568 void
quit_callback(FL_OBJECT * ob FL_UNUSED_ARG,long data FL_UNUSED_ARG)569 quit_callback( FL_OBJECT * ob    FL_UNUSED_ARG,
570                long        data  FL_UNUSED_ARG )
571 {
572     flimage_free( curr_image );
573 	fl_finish( );
574     exit( 0 );
575 }
576 
577 
578 /***************************************
579  ***************************************/
580 
581 static int
supported_image_filter(const char * name,int type)582 supported_image_filter( const char * name,
583                         int          type )
584 {
585     return    type == FT_DIR
586            || ( type == FT_FILE && flimage_is_supported( name ) > 0 );
587 }
588 
589 
590 /***************************************
591  ***************************************/
592 
593 static void
error_report(FL_IMAGE * im,const char * s)594 error_report( FL_IMAGE   * im,
595               const char * s )
596 {
597     FD_ibcanvas *fdui = im->app_data;
598 
599      if ( ! s )
600          s = " ";
601      fl_set_object_label( fdui->error, s );
602 }
603 
604 
605 /***************************************
606  ***************************************/
607 
608 static int
status_report(FL_IMAGE * im,const char * s)609 status_report( FL_IMAGE   * im,
610                const char * s )
611 {
612     FD_ibcanvas *fdui = im->app_data;
613     char buf[ 512 ];
614 
615     if ( im->completed < 0 )
616         strcpy( buf, s );
617     else if ( im->completed >= 0 && im->completed < im->total )
618     {
619         sprintf( buf,"%s %3.0f%% (%4d of %4d)", s,
620                  ( im->completed * 100.0 ) / im->total,
621                 im->completed, im->h );
622     }
623     else
624          strcpy( buf, s );
625 
626     fl_set_object_label( fdui->status, buf );
627     fl_update_display( 0 );
628 
629     return 0;
630 }
631 
632 
633 /***************************************
634  ***************************************/
635 
636 int
main(int argc,char * argv[])637 main( int    argc,
638       char * argv[ ] )
639 {
640     FD_ibcanvas *fd_ibcanvas;
641     int  n;
642     static FLIMAGE_SETUP setup;
643 
644     fl_set_border_width( -2 );
645     fl_initialize( &argc, argv, 0, 0, 0 );
646     fd_ibcanvas = create_form_ibcanvas( );
647     fl_set_object_color( fd_ibcanvas->canvas, FL_COL1, FL_COL1 );
648 
649     setup.visual_cue = status_report;
650     setup.error_message = error_report;
651     setup.app_data = fd_ibcanvas;
652     setup.delay = 70;
653     flimage_setup( &setup );
654 
655     flimage_enable_png( );
656     flimage_enable_xwd( );
657     flimage_enable_bmp( );
658     flimage_enable_xbm( );
659     flimage_enable_gif( );
660     flimage_enable_sgi( );
661     flimage_enable_fits( );
662     flimage_enable_xpm( );
663     flimage_enable_tiff( );
664     flimage_enable_ps( );
665 #ifndef NO_JPEG
666     flimage_enable_jpeg( );
667 #endif
668 
669     for ( n = flimage_get_number_of_formats(); n; n-- )
670         fl_addto_choice( fd_ibcanvas->format,
671                          ( flimage_get_format_info( n ) )->short_name );
672 
673     fl_set_dirlist_filter( supported_image_filter );
674 
675     fl_add_canvas_handler( fd_ibcanvas->canvas,
676                            Expose, expose_handle, 0 );
677 
678     fl_add_canvas_handler( fd_ibcanvas->canvas,
679                            MotionNotify, motion_handle, 0 );
680 
681    /* Show the first form */
682 
683     fl_show_form( fd_ibcanvas->ibcanvas, FL_PLACE_CENTERFREE, FL_FULLBORDER,
684                   "ibcanvas" );
685 
686     while ( fl_do_forms( ) )
687         /* empty */ ;
688 
689     return 0;
690 }
691 
692 
693 #include "fd/ibrowser_gui.c"
694 
695 
696 /*
697  * Local variables:
698  * tab-width: 4
699  * indent-tabs-mode: nil
700  * End:
701  */
702