summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/showimg/ImageViewer.java
blob: bb7ae395ffbf50f8f11564cf833f868e024764cd (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
* This file is part of an example program for Qt.  This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/

import org.kde.qt.*;
import java.util.*;

class ImageViewer  extends TQWidget
{
private int		conversion_flags;
private int		alloc_context;
private String filename;
private TQImage	image = new TQImage();			// the loaded image
private TQPixmap	pm = new TQPixmap();			// the converted pixmap
private TQPixmap	pmScaled;		// the scaled pixmap

private TQMenuBar   menubar;
private TQPopupMenu  file;
private TQPopupMenu   saveimage;
private TQPopupMenu   savepixmap;
private TQPopupMenu  edit;
private TQPopupMenu  options;

private TQWidget    helpmsg;
private TQLabel     status;
private int         si, sp, ac, co, mo, fd, bd, // Menu item ids
		td, ta, ba, fa, au, ad, dd,
		ss, cc, t1, t8, t32;
private int[]		pickx = { 0 };
private int[]		picky = { 0 };
private int[]		clickx = { 0 };
private int[]		clicky = { 0 };
private boolean	may_be_other;
private static ImageViewer other = null;




/*
  In the constructor, we just pass the standard parameters on to
  TQWidget.

  The menu uses a single slot to simplify the process of adding
  more items to the options menu.
*/
ImageViewer( TQWidget parent, String name, int wFlags )
{
    super( parent, name, wFlags );
    conversion_flags = PreferDither;
	filename = "";
    helpmsg = null;
    pickx[0] = -1;
    picky[0] = -1;
    clickx[0] = -1;
    clicky[0] = -1;
    alloc_context = 0;

    menubar = new TQMenuBar(this);
    menubar.setSeparator( TQMenuBar.InWindowsStyle );

    ArrayList fmt = TQImage.outputFormats();
    saveimage = new TQPopupMenu( menubar );
    savepixmap = new TQPopupMenu( menubar );
	Iterator it = fmt.iterator();
	while (it.hasNext()) {
	String f = (String) it.next();
	saveimage.insertItem( f );
	savepixmap.insertItem( f );
    }
    connect( saveimage, SIGNAL("activated(int)"), this, SLOT("saveImage(int)") );
    connect( savepixmap, SIGNAL("activated(int)"), this, SLOT("savePixmap(int)") );

    file = new TQPopupMenu( menubar );
    menubar.insertItem( "&File", file );
    file.insertItem( "&New window", this,  SLOT("newWindow()"), new TQKeySequence(CTRL+Key_N) );
    file.insertItem( "&Open...", this,  SLOT("openFile()"), new TQKeySequence(CTRL+Key_O) );
    si = file.insertItem( "Save image", saveimage );
    sp = file.insertItem( "Save pixmap", savepixmap );
    file.insertSeparator();
    file.insertItem( "E&xit", tqApp(),  SLOT("quit()"), new TQKeySequence(CTRL+Key_Q) );

    edit =  new TQPopupMenu( menubar );
    menubar.insertItem( "&Edit", edit );
    edit.insertItem("&Copy", this, SLOT("copy()"), new TQKeySequence(CTRL+Key_C));
    edit.insertItem("&Paste", this, SLOT("paste()"), new TQKeySequence(CTRL+Key_V));
    edit.insertSeparator();
    edit.insertItem("&Horizontal flip", this, SLOT("hFlip()"), new TQKeySequence(ALT+Key_H));
    edit.insertItem("&Vertical flip", this, SLOT("vFlip()"), new TQKeySequence(ALT+Key_V));
    edit.insertItem("&Rotate 180", this, SLOT("rot180()"), new TQKeySequence(ALT+Key_R));
    edit.insertSeparator();
    edit.insertItem("&Text...", this, SLOT("editText()"));
    edit.insertSeparator();
    t1 = edit.insertItem( "Convert to &1 bit", this, SLOT("to1Bit()") );
    t8 = edit.insertItem( "Convert to &8 bit", this, SLOT("to8Bit()") );
    t32 = edit.insertItem( "Convert to &32 bit", this, SLOT("to32Bit()") );

    options =  new TQPopupMenu( menubar );
    menubar.insertItem( "&Options", options );
    ac = options.insertItem( "AutoColor" );
    co = options.insertItem( "ColorOnly" );
    mo = options.insertItem( "MonoOnly" );
    options.insertSeparator();
    fd = options.insertItem( "DiffuseDither" );
    bd = options.insertItem( "OrderedDither" );
    td = options.insertItem( "ThresholdDither" );
    options.insertSeparator();
    ta = options.insertItem( "ThresholdAlphaDither" );
    ba = options.insertItem( "OrderedAlphaDither" );
    fa = options.insertItem( "DiffuseAlphaDither" );
    options.insertSeparator();
    ad = options.insertItem( "PreferDither" );
    dd = options.insertItem( "AvoidDither" );
    options.insertSeparator();
    ss = options.insertItem( "Smooth scaling" );
    cc = options.insertItem( "Use color context" );
    if ( TQApplication.colorSpec() == TQApplication.ManyColor )
	options.setItemEnabled( cc, false );
    options.setCheckable( true );
    setMenuItemFlags();

    menubar.insertSeparator();

    TQPopupMenu help = new TQPopupMenu( menubar );
    menubar.insertItem( "&Help", help );
    help.insertItem( "Help!", this, SLOT("giveHelp()"), new TQKeySequence(CTRL+Key_H) );

    connect( options, SIGNAL("activated(int)"), this, SLOT("doOption(int)") );

    status = new TQLabel(this);
    status.setFrameStyle( TQFrame.WinPanel | TQFrame.Sunken );
    status.setFixedHeight( fontMetrics().height() + 4 );

    setMouseTracking( true );
}

void cleanUp()
{
    if ( alloc_context != 0 )
	TQColor.destroyAllocContext( alloc_context );
    if ( other == this )
	other = null;
}

/*
  This function modifies the conversion_flags when an options menu item
  is selected, then ensures all menu items are up to date, and reconverts
  the image if possibly necessary.
*/
void doOption(int item)
{
    if ( item == ss || item == cc ) {
	// Toggle
	boolean newboolean = !options.isItemChecked(item);
	options.setItemChecked(item, newboolean);
	// And reconvert...
	reconvertImage();
	repaint(image.hasAlphaBuffer());	// show image in widget
	return;
    }

    if ( options.isItemChecked( item ) ) return; // They are all radio buttons

    int ocf = conversion_flags;

    if ( item == ac ) {
	conversion_flags = conversion_flags & ~ColorMode_Mask | AutoColor;
    } else if ( item == co ) {
	conversion_flags = conversion_flags & ~ColorMode_Mask | ColorOnly;
    } else if ( item == mo ) {
	conversion_flags = conversion_flags & ~ColorMode_Mask | MonoOnly;
    } else if ( item == fd ) {
	conversion_flags = conversion_flags & ~Dither_Mask | DiffuseDither;
    } else if ( item == bd ) {
	conversion_flags = conversion_flags & ~Dither_Mask | OrderedDither;
    } else if ( item == td ) {
	conversion_flags = conversion_flags & ~Dither_Mask | ThresholdDither;
    } else if ( item == ta ) {
	conversion_flags = conversion_flags & ~AlphaDither_Mask | ThresholdAlphaDither;
    } else if ( item == fa ) {
	conversion_flags = conversion_flags & ~AlphaDither_Mask | DiffuseAlphaDither;
    } else if ( item == ba ) {
	conversion_flags = conversion_flags & ~AlphaDither_Mask | OrderedAlphaDither;
    } else if ( item == ad ) {
	conversion_flags = conversion_flags & ~DitherMode_Mask | PreferDither;
    } else if ( item == dd ) {
	conversion_flags = conversion_flags & ~DitherMode_Mask | AvoidDither;
    }

    if ( ocf != conversion_flags ) {
	setMenuItemFlags();
	// And reconvert...
	reconvertImage();
	repaint(image.hasAlphaBuffer());	// show image in widget
    }
}

/*
  Set the options menu to reflect the conversion_flags value.
*/
void setMenuItemFlags()
{
    // File
    boolean valid_image = pm.size().width() != 0 || pm.size().height() != 0;
    file.setItemEnabled( si, valid_image );
    file.setItemEnabled( sp, valid_image );

    // Edit
    edit.setItemEnabled( t1, image.depth() != 1 );
    edit.setItemEnabled( t8, image.depth() != 8 );
    edit.setItemEnabled( t32, image.depth() != 32 );

    // Options
    boolean may_need_color_dithering =
	    !valid_image
	|| image.depth() == 32 && TQPixmap.defaultDepth() < 24;
    boolean may_need_dithering = may_need_color_dithering
	 || image.depth() > 1 && options.isItemChecked(mo)
	 || image.depth() > 1 && TQPixmap.defaultDepth() == 1;
    boolean has_alpha_mask = !valid_image || image.hasAlphaBuffer();

    options.setItemEnabled( fd, may_need_dithering );
    options.setItemEnabled( bd, may_need_dithering );
    options.setItemEnabled( td, may_need_dithering );

    options.setItemEnabled( ta, has_alpha_mask );
    options.setItemEnabled( fa, has_alpha_mask );
    options.setItemEnabled( ba, has_alpha_mask );

    options.setItemEnabled( ad, may_need_color_dithering );
    options.setItemEnabled( dd, may_need_color_dithering );

    options.setItemChecked( ac, (conversion_flags & ColorMode_Mask) == AutoColor );
    options.setItemChecked( co, (conversion_flags & ColorMode_Mask) == ColorOnly );
    options.setItemChecked( mo, (conversion_flags & ColorMode_Mask) == MonoOnly );
    options.setItemChecked( fd, (conversion_flags & Dither_Mask) == DiffuseDither );
    options.setItemChecked( bd, (conversion_flags & Dither_Mask) == OrderedDither );
    options.setItemChecked( td, (conversion_flags & Dither_Mask) == ThresholdDither );
    options.setItemChecked( ta, (conversion_flags & AlphaDither_Mask) == ThresholdAlphaDither );
    options.setItemChecked( fa, (conversion_flags & AlphaDither_Mask) == DiffuseAlphaDither );
    options.setItemChecked( ba, (conversion_flags & AlphaDither_Mask) == OrderedAlphaDither );
    options.setItemChecked( ad, (conversion_flags & DitherMode_Mask) == PreferDither );
    options.setItemChecked( dd, (conversion_flags & DitherMode_Mask) == AvoidDither );
}

void updateStatus()
{
    if ( pm.size().width() == 0 && pm.size().height() == 0 ) {
	if ( !filename.equals("") )
	    status.setText("Could not load image");
	else
	    status.setText("No image - select Open from File menu.");
    } else {
	String message, moremsg;
	message = "" + image.width() + "x" + image.height();
	if ( pm.size().width() != pmScaled.size().width()
	     || pm.size().height() != pmScaled.size().height() )
	{
	    moremsg = " [" + pmScaled.width() + "x" + pmScaled.height() + "]";
	    message += moremsg;
	}
	moremsg = ", " +  image.depth() + " bits ";
	message += moremsg;
	if (image.valid(pickx[0],picky[0])) {
	    moremsg = "("
			  + pickx[0] + "," + picky[0] + ")=#"
			  + Integer.toHexString(image.pixel(pickx[0],picky[0]))
			  + " ";
	    message += moremsg;
	}
	if ( image.numColors() > 0 ) {
	    if (image.valid(pickx[0],picky[0])) {
		moremsg = ", " + image.pixelIndex(pickx[0],picky[0]) + "/"
		    + image.numColors() + " colors";
	    } else {
		moremsg = ", " + image.numColors() + " colors";
	    }
	    message += moremsg;
	}
	if ( image.hasAlphaBuffer() ) {
	    if ( image.depth() == 8 ) {
		int i;
		boolean alpha[] = new boolean[256];
		int nalpha=0;

		for (i=0; i<256; i++)
		    alpha[i] = false;

		for (i=0; i<image.numColors(); i++) {
		    int alevel = image.color(i) >> 24;
			// Hack - image.color() returns a C++ unsigned int, but alevel is a signed int.
			// If the top 8 bits are 0xffffffff, alevel is -1, not 255, and needs fixing up
			if (alevel == -1) {
				alevel = 255;
			}
		    if (!alpha[alevel]) {
			alpha[alevel] = true;
			nalpha++;
		    }
		}
		moremsg = ", " + nalpha + " alpha levels";
	    } else {
		// Too many pixels to bother counting.
		moremsg = ", 8-bit alpha channel";
	    }
	    message += moremsg;
	}
	status.setText(message);
    }
}

/*
  This function saves the image.
*/
void saveImage( int item )
{
    String fmt = saveimage.text(item);
    String savefilename = TQFileDialog.getSaveFileName("", "",
					this, filename);
    if ( !savefilename.equals("") )
	if ( !image.save( savefilename, fmt ) )
	    TQMessageBox.warning( this, "Save failed", "Error saving file" );
}

/*
  This function saves the converted image.
*/
void savePixmap( int item )
{
    String fmt = savepixmap.text(item);
    String savefilename = TQFileDialog.getSaveFileName("",
					"", this, filename);
    if ( !savefilename.equals("") )
	if ( !pmScaled.save( savefilename, fmt ) )
	    TQMessageBox.warning( this, "Save failed", "Error saving file" );
}


void newWindow()
{
    ImageViewer that = new ImageViewer(null, "new window", WDestructiveClose);
    that.options.setItemChecked( that.cc, useColorContext() );
    that.show();
}

/*
  This function is the slot for processing the Open menu item.
*/
void openFile()
{
    String newfilename = TQFileDialog.getOpenFileName();
    if ( !newfilename.equals("") ) {
	loadImage( newfilename ) ;
	repaint();			// show image in widget
    }
}

/*
  This function loads an image from a file and resizes the widget to
  exactly fit the image size. If the file was not found or the image
  format was unknown it will resize the widget to fit the errorText
  message (see above) displayed in the current font.

  Returns true if the image was successfully loaded.
*/

boolean loadImage( String fileName )
{
    filename = fileName;
    boolean ok = false;
    if ( ! filename.equals("") ) {
	TQApplication.setOverrideCursor( waitCursor() ); // this might take time
	ok = image.load(filename, null);
	pickx[0] = -1;
	clickx[0] = -1;
	if ( ok )
	    ok = reconvertImage();
	if ( ok ) {
	    setCaption( filename );			// set window caption
	    int w = pm.width();
	    int h = pm.height();

	    int reasonable_width = 128;
	    if ( w < reasonable_width ) {
		// Integer scale up to something reasonable
		int multiply = ( reasonable_width + w - 1 ) / w;
		w *= multiply;
		h *= multiply;
	    }

	    h += menubar.heightForWidth(w) + status.height();
	    resize( w, h );				// we resize to fit image
	} else {
	    pm.resize(0,0);				// couldn't load image
	    update();
	}
	TQApplication.restoreOverrideCursor();	// restore original cursor
    }
    updateStatus();
    setMenuItemFlags();
    return ok;
}

boolean reconvertImage()
{
    boolean success = false;

    if ( image.isNull() ) return false;

    if ( alloc_context != 0 ) {
	TQColor.destroyAllocContext( alloc_context );
	alloc_context = 0;
    }
    if ( useColorContext() ) {
	alloc_context = TQColor.enterAllocContext();
	// Clear the image to hide flickering palette
	TQPainter painter = new TQPainter(this);
	painter.eraseRect(0, menubar.heightForWidth( width() ), width(), height());
    }

    TQApplication.setOverrideCursor( waitCursor() ); // this might take time
    if ( pm.convertFromImage(image, conversion_flags) )
    {
	pmScaled = new TQPixmap();
	scale();
	resize( width(), height() );
	success = true;				// load successful
    } else {
	pm.resize(0,0);				// couldn't load image
    }
    updateStatus();
    setMenuItemFlags();
    TQApplication.restoreOverrideCursor();	// restore original cursor

    if ( useColorContext() )
	TQColor.leaveAllocContext();

    return success;				// true if loaded OK
}

boolean smooth()
{
    return options.isItemChecked(ss);
}

boolean useColorContext()
{
    return options.isItemChecked(cc);
}

/*
  This functions scales the pixmap in the member variable "pm" to fit the
  widget size and  puts the resulting pixmap in the member variable "pmScaled".
*/

void scale()
{
    int h = height() - menubar.heightForWidth( width() ) - status.height();

    if ( image.isNull() ) return;

    TQApplication.setOverrideCursor( waitCursor() ); // this might take time
    if ( width() == pm.width() && h == pm.height() )
    {						// no need to scale if widget
	pmScaled = pm;				// size equals pixmap size
    } else {
	if (smooth()) {
	    pmScaled.convertFromImage(image.smoothScale(width(), h),
		conversion_flags);
	} else {
	    TQWMatrix m = new TQWMatrix();				// transformation matrix
	    m.scale(((double)width())/pm.width(),// define scale factors
		    ((double)h)/pm.height());
	    pmScaled = (TQPixmap) pm.xForm( m );		// create scaled pixmap
	}
    }
    TQApplication.restoreOverrideCursor();	// restore original cursor
}

/*
  The resize event handler, if a valid pixmap was loaded it will call
  scale() to fit the pixmap to the new widget size.
*/

protected void resizeEvent( TQResizeEvent e )
{
    status.setGeometry(0, height() - status.height(),
			width(), status.height());

    if ( pm.size().width() == 0 && pm.size().height() == 0 )		// we couldn't load the image
	return;

    int h = height() - menubar.heightForWidth( width() ) - status.height();
    if ( width() != pmScaled.width() || h != pmScaled.height())
    {						// if new size,
	scale();				// scale pmScaled to window
	updateStatus();
    }
    if ( image.hasAlphaBuffer() )
	erase();
}

protected boolean convertEvent( TQMouseEvent e, int[] x, int[] y)
{
    if ( pm.size().width() != 0 || pm.size().height() != 0 ) {
	int h = height() - menubar.heightForWidth( width() ) - status.height();
	int nx = e.x() * image.width() / width();
	int ny = (e.y()-menubar.heightForWidth( width() )) * image.height() / h;
	if (nx != x[0] || ny != y[0] ) {
	    x[0] = nx;
	    y[0] = ny;
	    updateStatus();
	    return true;
	}
    }
    return false;
}

protected void mousePressEvent( TQMouseEvent e )
{
    may_be_other = convertEvent(e, clickx, clicky);
}

protected void mouseReleaseEvent( TQMouseEvent e )
{
    if ( may_be_other )
	other = this;
}

/*
  Record the pixel position of interest.
*/
protected void mouseMoveEvent( TQMouseEvent e )
{
    if (convertEvent(e,pickx,picky)) {
	updateStatus();
	if ((e.state()&LeftButton) != 0) {
	    may_be_other = false;
	    if ( clickx[0] >= 0 && other != null) {
		copyFrom(other);
	    }
	}
    }
}

/*
  Draws the portion of the scaled pixmap that needs to be updated or prints
  an error message if no legal pixmap has been loaded.
*/

protected void paintEvent( TQPaintEvent e )
{
    if ( pm.size().width() != 0 || pm.size().height() != 0 ) {		// is an image loaded?
	TQPainter painter = new TQPainter(this);
	painter.setClipRect(e.rect());
	painter.drawPixmap(0, menubar.heightForWidth( width() ), pmScaled);
    }
}


/*
  Explain anything that might be confusing.
*/
void giveHelp()
{
    if (helpmsg == null) {
	String helptext =
	    "<b>Usage:</b> <tt>showimg [-m] <i>filename ...</i></tt>"
	    + "<blockquote>"
		+ "<tt>-m</tt> - use <i>ManyColor</i> color spec"
	    + "</blockquote>"
	    + "<p>Supported input formats:"
	    + "<blockquote>";
	Iterator it = TQImage.inputFormatList().iterator();
	if (it.hasNext())
        helptext += (String) it.next();
	while (it.hasNext()) {
    	helptext += ", " + (String) it.next();
	}
	helptext += "</blockquote>";

	helpmsg = new TQMessageBox( "Help", helptext,
	    TQMessageBox.Information, TQMessageBox.Ok, 0, 0, null, null, false );
    }
    helpmsg.show();
    helpmsg.raise();
}

void copyFrom(ImageViewer s)
{
    if ( clickx[0] >= 0 ) {
	int dx = clickx[0];
	int dy = clicky[0];
	int sx = s.clickx[0];
	int sy = s.clicky[0];
	int sw = Math.abs(clickx[0] - pickx[0])+1;
	int sh = Math.abs(clicky[0] - picky[0])+1;
	if ( clickx[0] > pickx[0] ) {
	    dx = pickx[0];
	    sx -= sw-1;
	}
	if ( clicky[0] > picky[0] ) {
	    dy = picky[0];
	    sy -= sh-1;
	}
	bitBlt( new TQPixmap(image), dx, dy, new TQPixmap(s.image), sx, sy, sw, sh );
	reconvertImage();
	repaint( image.hasAlphaBuffer() );
    }
}

void hFlip()
{
    setImage(image.mirror(true,false));
}

void vFlip()
{
    setImage(image.mirror(false,true));
}

void rot180()
{
    setImage(image.mirror(true,true));
}

void copy()
{
    TQApplication.clipboard().setImage(image); // Less information loss
}

void paste()
{
    TQImage p = TQApplication.clipboard().image();
    if ( !p.isNull() ) {
	filename = "pasted";
	setImage(p);
    }
}

void setImage(TQImage newimage)
{
    image = newimage;

    pickx[0] = -1;
    clickx[0] = -1;
    setCaption( filename );			// set window caption
    int w = image.width();
    int h = image.height();
    if ( w == 0 )
	return;

    int reasonable_width = 128;
    if ( w < reasonable_width ) {
	// Integer scale up to something reasonable
	int multiply = ( reasonable_width + w - 1 ) / w;
	w = multiply;
	h = multiply;
    }

    h += menubar.heightForWidth(w) + status.height();
    resize( w, h );				// we resize to fit image

    reconvertImage();
    repaint( image.hasAlphaBuffer() );

    updateStatus();
    setMenuItemFlags();
}

void editText()
{
    ImageTextEditor editor = new ImageTextEditor(image,this);
    editor.exec();
}

void to1Bit()
{
    toBitDepth(1);
}

void to8Bit()
{
    toBitDepth(8);
}

void to32Bit()
{
    toBitDepth(32);
}

void toBitDepth(int d)
{
    image = image.convertDepth(d);
    reconvertImage();
    repaint( image.hasAlphaBuffer() );
}
}