~ubuntu-branches/ubuntu/hoary/gimp/hoary

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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
 * WinClipboard Win32 Windoze Copy&Paste Plug-in
 * Copyright (C) 1999 Hans Breuer
 * Hans Breuer, Hans@Breuer.org
 * 08/07/99
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Based on (at least) the following plug-ins:
 *  Header
 *  WinSnap
 *
 * Any suggestions, bug-reports or patches are welcome.
 */

#include "config.h"

#include <windows.h>
#include <stdlib.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"


/* History:
 *
 *   08/07/99 Implementation and release.
 *	 08/10/99 Big speed increase by using gimp_tile_cache_size()
 *			  Thanks to Kevin Turner's documentation at:
 *			  http://www.poboxes.com/kevint/gimp/doc/plugin-doc-2.1.html
 *
 * TODO (maybe):
 *
 *   - Support for 4,2,1 bit bitmaps
 *   - Unsupported formats could be delegated to GIMP Loader (e.g. wmf)
 *   - ...
 */

/* How many steps the progress control should do
 */
#define PROGRESS_STEPS 25
#define StepProgress(one,all) \
	(0 == (one % ((all / PROGRESS_STEPS)+1)))

/* FIXME: I'll use -1 as IMAGE_NONE. Is it correct ???
 */
#define IMAGE_NONE -1

/* Declare some local functions.
 */
static void   query      (void);
static void   run        (const gchar      *name,
                          gint              nparams,
                          const GimpParam  *param,
                          gint             *nreturn_vals,
                          GimpParam       **return_vals);

/* Plugin function prototypes
 */
static int CB_CopyImage (gboolean    interactive,
			 gint32      image_ID,
			 gint32      drawable_ID);

static int CB_PasteImage (gboolean   interactive,
			  gint32     image_ID,
			  gint32     drawable_ID);


GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
};


MAIN ()

static void
query ()
{
  static GimpParamDef copy_args[] =
  {
    { GIMP_PDB_INT32,    "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image"                  },
    { GIMP_PDB_DRAWABLE, "drawable", "Drawable to save"             }
  };

  gimp_install_procedure ("plug_in_clipboard_copy",
                          "copy image to clipboard",
                          "Copies the active drawable to the clipboard.",
                          "Hans Breuer",
                          "Hans Breuer",
                          "1999",
                          N_("Copy to Clipboard"),
                          "INDEXED*, RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (copy_args), 0,
                          copy_args, NULL);

  gimp_install_procedure ("plug_in_clipboard_paste",
                          "paste image from clipboard",
                          "Paste image from clipboard into active image.",
                          "Hans Breuer",
                          "Hans Breuer",
                          "1999",
                          N_("Paste from Clipboard"),
                          "INDEXED*, RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (copy_args), 0,
                          copy_args, NULL);

  gimp_install_procedure ("plug_in_clipboard_paste_as_new",
                          "Get image from clipboard",
                          "Get an image from the Windows clipboard, creating a new image",
                          "Hans Breuer",
                          "Hans Breuer",
                          "1999",
                          N_("From Clipboard"),
                          "",
                          GIMP_PLUGIN,
                          1, 0,
                          copy_args, NULL);

  gimp_plugin_menu_register ("plug_in_clipboard_copy",
                             "<Image>/Edit");
  gimp_plugin_menu_register ("plug_in_clipboard_paste",
                             "<Image>/Edit");
  gimp_plugin_menu_register ("plug_in_clipboard_paste_as_new",
                             "<Toolbox>/File/Acquire");
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam values[2];
  GimpRunMode run_mode;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals = values;
  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;

  INIT_I18N();

  if (strcmp (name, "plug_in_clipboard_copy") == 0)
    {
      *nreturn_vals = 1;
      if (CB_CopyImage (GIMP_RUN_INTERACTIVE==run_mode,
			param[1].data.d_int32,
			param[2].data.d_int32))
	values[0].data.d_status = GIMP_PDB_SUCCESS;
      else
	values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
    }
  else if (strcmp (name, "plug_in_clipboard_paste") == 0)
    {
      *nreturn_vals = 1;
      if (CB_PasteImage (GIMP_RUN_INTERACTIVE==run_mode,
			 param[1].data.d_int32,
			 param[2].data.d_int32))
	values[0].data.d_status = GIMP_PDB_SUCCESS;
      else
	values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
    }
  else if (strcmp (name, "plug_in_clipboard_paste_as_new") == 0)
    {
      *nreturn_vals = 1;
      if (CB_PasteImage (GIMP_RUN_INTERACTIVE==run_mode,
			 IMAGE_NONE,
			 IMAGE_NONE))
	values[0].data.d_status = GIMP_PDB_SUCCESS;
      else
	values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
    }
}

/* Plugin Function implementation
 */
static int
CB_CopyImage (gboolean interactive,
	      gint32   image_ID,
	      gint32   drawable_ID)
{
  GimpDrawable *drawable;
  GimpImageType drawable_type;
  GimpPixelRgn pixel_rgn;
  gchar *sStatus = NULL;

  int nSizeDIB=0;
  int nSizePal=0;
  int nSizeLine=0; /* DIB lines are 32 bit aligned */

  HANDLE hDIB;
  BOOL bRet;

  drawable = gimp_drawable_get (drawable_ID);
  drawable_type = gimp_drawable_type (drawable_ID);
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width, drawable->height, FALSE, FALSE);

  /* allocate room for DIB */
  if (GIMP_INDEXED_IMAGE == drawable_type || GIMP_GRAY_IMAGE == drawable_type)
    {
      nSizeLine = ((drawable->width-1)/4+1)*4;
      nSizeDIB = sizeof(RGBQUAD) * 256 /* always full color map size */
	+ nSizeLine * drawable->height
	+ sizeof (BITMAPINFOHEADER);
    }
  else
    {
      nSizeLine = ((drawable->width*3-1)/4+1)*4;
      nSizeDIB = nSizeLine * drawable->height
	+ sizeof (BITMAPINFOHEADER);
    }

  hDIB = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, nSizeDIB);
  if (NULL == hDIB)
    {
      g_message ("Failed to allocate DIB");
      bRet = FALSE;
    }

  /* fill header info */
  if (bRet)
    {
      BITMAPINFOHEADER *pInfo;

      bRet = FALSE;
      pInfo = GlobalLock (hDIB);
      if (pInfo)
	{
	  pInfo->biSize   = sizeof(BITMAPINFOHEADER);
	  pInfo->biWidth  = drawable->width;
	  pInfo->biHeight = drawable->height;
	  pInfo->biPlanes = 1;
	  pInfo->biBitCount = 
	    (GIMP_INDEXED_IMAGE == drawable_type || GIMP_GRAY_IMAGE == drawable_type ? 8 : 24);
	  pInfo->biCompression = BI_RGB; /* none */
	  pInfo->biSizeImage = 0; /* not calculated/needed */
	  pInfo->biXPelsPerMeter =
	    pInfo->biYPelsPerMeter = 0;
	  /* color map size */
	  pInfo->biClrUsed = 
	    (GIMP_INDEXED_IMAGE == drawable_type || GIMP_GRAY_IMAGE == drawable_type ? 256 : 0);
	  pInfo->biClrImportant = 0; /* all */

	  GlobalUnlock (hDIB);
	  bRet = TRUE;
	} /* (pInfo) */
      else
	g_message("Failed to lock DIB Header");
    }

  /* fill color map */
  if (bRet && (GIMP_INDEXED_IMAGE == drawable_type  || GIMP_GRAY_IMAGE == drawable_type))
    {
      char *pBmp;

      bRet = FALSE;
      pBmp = GlobalLock (hDIB);
      if (pBmp)
	{
	  RGBQUAD *pPal;
	  int nColors;
	  unsigned char *cmap = NULL;
	  pPal = (RGBQUAD*)(pBmp + sizeof(BITMAPINFOHEADER));
	  nSizePal = sizeof(RGBQUAD) * 256;

	  /* get the gimp colormap */
	  if (GIMP_GRAY_IMAGE != drawable_type)
	    cmap = gimp_image_get_colormap (image_ID, &nColors);

	  if (cmap)
	    {
	      int i;
	      for (i = 0; (i < 256) && (i < nColors); i++)
		{
		  pPal[i].rgbReserved = 0; /* is this alpha? */
		  pPal[i].rgbRed      = cmap[3*i];
		  pPal[i].rgbGreen    = cmap[3*i+1];
		  pPal[i].rgbBlue     = cmap[3*i+2];
		}

	      g_free(cmap);
	      bRet = TRUE;
	    } /* (cmap) */
	  else if (GIMP_GRAY_IMAGE == drawable_type)
	    {
	      /* fill with identity palette */
	      int i;
	      for (i = 0; (i < 256) && (i < nColors); i++)
		{
		  pPal[i].rgbReserved = 0; /* is this alpha? */
		  pPal[i].rgbRed      = i;
		  pPal[i].rgbGreen    = i;
		  pPal[i].rgbBlue     = i;
		}

	      bRet = TRUE;
	    }
	  else
	    g_message ("Can't get color map");
	  GlobalUnlock (hDIB);
	} /* (pBmp) */
      else
	g_message ("Failed to lock DIB Palette");
    } /* indexed or grayscale */

  /* following the slow part ... */
  if (interactive)
    gimp_progress_init (_("Copying..."));

  /* speed it up with: */
  gimp_tile_cache_size (drawable->width * gimp_tile_height () * drawable->bpp);

  /* copy data to DIB */
  if (bRet)
    {
      unsigned char *pData;

      bRet = FALSE;
      pData = GlobalLock (hDIB);

      if (pData)
	{
	  unsigned char *pLine;

	  /* calculate real offset */
	  pData += (sizeof(BITMAPINFOHEADER) + nSizePal);

	  pLine = g_new (guchar, drawable->width * drawable->bpp);

	  if (GIMP_INDEXED_IMAGE == drawable_type || GIMP_GRAY_IMAGE == drawable_type)
	    {
	      int x, y;
	      for (y = 0; y < drawable->height; y++)
		{
		  if ((interactive) && (StepProgress(y,drawable->height)))
		    gimp_progress_update ((double)y / drawable->height);

		  gimp_pixel_rgn_get_row (&pixel_rgn, pLine, 0,
					  drawable->height-y-1, /* invert it */
					  drawable->width);
		  for (x = 0; x < drawable->width; x++)
		    pData[x+y*nSizeLine] = pLine[x*drawable->bpp];
		}
	    }
	  else
	    {
	      int x, y;
	      for (y = 0; y < drawable->height; y++)
		{
		  if ((interactive) && (StepProgress(y,drawable->height)))
		    gimp_progress_update ((double)y / drawable->height);

		  gimp_pixel_rgn_get_row (&pixel_rgn, pLine, 0,
					  drawable->height-y-1, /* invert it */
					  drawable->width);
		  for (x = 0; x < drawable->width; x++)
		    {
		      /* RGBQUAD: blue, green, red, reserved */
		      pData[x*3+y*nSizeLine]   = pLine[x*drawable->bpp+2]; /* blue */
		      pData[x*3+y*nSizeLine+1] = pLine[x*drawable->bpp+1]; /* green */
		      pData[x*3+y*nSizeLine+2] = pLine[x*drawable->bpp];   /* red */
		      /*pData[x+y*drawable->width*3+3] = 0;*/          /* reserved */
		    }
		}
	    }

	  g_free (pLine);
	  bRet = TRUE;

	  GlobalUnlock (hDIB);
	} /* (pData) */
      else
	g_message("Failed to lock DIB Data");
    } /* copy data to DIB */

  /* copy DIB to ClipBoard */
  if (bRet)
    {
      if (!OpenClipboard (NULL))
	{
	  g_message ("Cannot open the Clipboard!");
	  bRet = FALSE;
	}
      else
	{
	  if (bRet && !EmptyClipboard ())
	    {
	      g_message ("Cannot empty the Clipboard");
	      bRet = FALSE;
	    }
	  if (bRet)
	    {
	      if (NULL != SetClipboardData (CF_DIB, hDIB))
		hDIB = NULL; /* data now owned by clipboard */
	      else
		g_message ("Failed to set clipboard data ");
	    }

	  if (!CloseClipboard ())
	    g_message ("Failed to close Clipboard");
	}
    }
  /* done */
  if (hDIB)
    GlobalFree(hDIB);

  gimp_drawable_detach (drawable);

  return bRet;
} /* CB_CopyImage */

static void
decompose_mask (guint32  mask,
		gint    *shift,
		gint    *prec)
{
  *shift = 0;
  *prec = 0;

  while (!(mask & 0x1))
    {
      (*shift)++;
      mask >>= 1;
    }

  while (mask & 0x1)
    {
      (*prec)++;
      mask >>= 1;
    }
}

static int
CB_PasteImage (gboolean interactive,
			   gint32   image_ID,
			   gint32   drawable_ID)
{
  UINT fmt;
  BOOL bRet=TRUE;
  HANDLE hDIB;
  BITMAPINFO *pInfo;
  gint32 nWidth  = 0;
  gint32 nHeight = 0;
  gint32 nBitsPS = 0;
  gint32 nColors = 0;
  guint32 maskR;
  guint32 maskG;
  guint32 maskB;
  gint shiftR;
  gint shiftG;
  gint shiftB;
  gint precR;
  gint precG;
  gint precB;
  gint maxR;
  gint maxG;
  gint maxB;

  if (!OpenClipboard (NULL))
    {
      g_message("Failed to open clipboard");
      return FALSE;
    }

  fmt = EnumClipboardFormats (0);
  while ((CF_BITMAP != fmt) && (CF_DIB != fmt) && (0 != fmt))
    fmt = EnumClipboardFormats (fmt);

  if (0 == fmt)
    {
      g_message (_("Unsupported format or Clipboard empty!"));
      bRet = FALSE;
    }

  /* there is something supported */
  if (bRet)
    {
      hDIB = GetClipboardData (CF_DIB);

      if (NULL == hDIB)
	{
	  g_message (_("Can't get Clipboard data."));
	  bRet = FALSE;
	}
    }

  /* read header */
  if (bRet && hDIB)
    {
      pInfo = GlobalLock (hDIB);
      if (NULL == pInfo)
	{
	  g_message ("Can't lock Clipboard data!");
	  bRet = FALSE;
	}

      if (bRet && pInfo->bmiHeader.biCompression == BI_BITFIELDS)
	{
	  maskR = ((DWORD *)pInfo->bmiColors)[0];
	  maskG = ((DWORD *)pInfo->bmiColors)[1];
	  maskB = ((DWORD *)pInfo->bmiColors)[2];
	  decompose_mask (maskR, &shiftR, &precR);
	  maxR = (1 << precR) - 1;
	  decompose_mask (maskG, &shiftG, &precG);
	  maxG = (1 << precG) - 1;
	  decompose_mask (maskB, &shiftB, &precB);
	  maxB = (1 << precB) - 1;
	}

      if ((bRet) &&
	  ((pInfo->bmiHeader.biSize != sizeof(BITMAPINFOHEADER)
	    || (pInfo->bmiHeader.biCompression != BI_RGB
		&& pInfo->bmiHeader.biCompression != BI_BITFIELDS))))
	{
	  g_message ("Unsupported bitmap format (%d)!",
		     pInfo->bmiHeader.biCompression);
	  bRet = FALSE;
	}

      if (bRet && pInfo)
	{
	  nWidth  = pInfo->bmiHeader.biWidth;
	  nHeight = pInfo->bmiHeader.biHeight;
	  nBitsPS = pInfo->bmiHeader.biBitCount;
	  nColors = pInfo->bmiHeader.biClrUsed;
	}
    }

  if ((0 != nWidth) && (0 != nHeight))
    {
      GimpDrawable *drawable;
      GimpPixelRgn pixel_rgn;
      guchar *pData;
      GimpParam *params;
      gint retval;
      gboolean bIsNewImage = TRUE;
      gint oldBPP = 0;
      guchar *pLine;
      RGBQUAD *pPal;
      const int nSizeLine = ((nWidth*(nBitsPS/8)-1)/4+1)*4;

      /* Check if clipboard data and existing image are compatible */
      if (IMAGE_NONE != drawable_ID)
	{
	  drawable = gimp_drawable_get (drawable_ID);
	  oldBPP = drawable->bpp;
	  gimp_drawable_detach (drawable);
	}

      if ((IMAGE_NONE == image_ID)
	  || (3 != oldBPP) || (24 != nBitsPS))
	{
	  /* create new image */
	  image_ID = gimp_image_new (nWidth, nHeight, nBitsPS <= 8 ? GIMP_INDEXED : GIMP_RGB);
	  gimp_image_undo_disable(image_ID);
	  drawable_ID = gimp_layer_new (image_ID, _("Background"), nWidth, nHeight,
					nBitsPS <= 8 ? GIMP_INDEXED_IMAGE : GIMP_RGB_IMAGE,
					100, GIMP_NORMAL_MODE);
	}
      else
	{
	  /* ??? gimp_image_convert_rgb (image_ID);
	   */
	  drawable_ID = gimp_layer_new (image_ID, _("Pasted"), nWidth, nHeight,
					nBitsPS <= 8 ? GIMP_INDEXEDA_IMAGE : GIMP_RGBA_IMAGE,
					100, GIMP_NORMAL_MODE);
	  bIsNewImage = FALSE;
	}

      gimp_image_add_layer (image_ID, drawable_ID, -1);
      drawable = gimp_drawable_get (drawable_ID);

      gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width, drawable->height, FALSE, FALSE);

      /* following the slow part ... */
      if (interactive)
	gimp_progress_init (_("Pasting..."));

      /* adjust pointer */
      pPal  = pInfo->bmiColors;
      if (pInfo->bmiHeader.biCompression == BI_BITFIELDS)
        pData = (guchar*)pPal + sizeof(DWORD) * 3;
      else
        pData = (guchar*)pPal + sizeof(RGBQUAD) * nColors;

      /* create palette */
      if (0 != nColors)
	{
	  int c;
	  guchar *cmap;

	  cmap = g_new (guchar, nColors*3);
	  for (c = 0; c < nColors; c++)
	    {
	      cmap[c*3]   = pPal[c].rgbRed;
	      cmap[c*3+1] = pPal[c].rgbGreen;
	      cmap[c*3+2] = pPal[c].rgbBlue;
	    }
	  gimp_image_set_colormap (image_ID, cmap, nColors);
	  g_free (cmap);
	}

      /* speed it up with: */
      gimp_tile_cache_size (drawable->width * gimp_tile_height () * drawable->bpp );

      /* change data format and copy data */
      if ((24 == nBitsPS && pInfo->bmiHeader.biCompression == BI_RGB)
	  || (pInfo->bmiHeader.biCompression == BI_BITFIELDS))
	{
	  int y;
	  const int bps = nBitsPS / 8;
	  pLine = g_new (guchar, drawable->width*drawable->bpp);

	  for (y = 0; y < drawable->height; y++)
	    {
	      int x;

	      if ((interactive) && (StepProgress (y, drawable->height)))
		gimp_progress_update ((double)y / drawable->height);

	      if (pInfo->bmiHeader.biCompression == BI_RGB)
		for (x = 0; x < drawable->width; x++)
		  {
		    pLine[x*drawable->bpp]   = pData[y*nSizeLine+x*bps+2];
		    pLine[x*drawable->bpp+1] = pData[y*nSizeLine+x*bps+1];
		    pLine[x*drawable->bpp+2] = pData[y*nSizeLine+x*bps];
		    if (drawable->bpp == 4)
		      pLine[x*drawable->bpp+3] = 255;
		  }
	      else
		for (x = 0; x < drawable->width; x++)
		  {
		    guint dw;

		    if (nBitsPS == 32)
		      dw = *((DWORD *)(pData + y*nSizeLine + x*4));
		    else
		      dw = *((WORD *)(pData + y*nSizeLine + x*2));
		    pLine[x*drawable->bpp]   = (255 * ((dw & maskR) >> shiftR)) / maxR;
		    pLine[x*drawable->bpp+1] = (255 * ((dw & maskG) >> shiftG)) / maxG;
		    pLine[x*drawable->bpp+2] = (255 * ((dw & maskB) >> shiftB)) / maxB;
		    if (drawable->bpp == 4)
		      pLine[x*drawable->bpp+3] = 255;
		  }

	      /* copy data to GIMP */
	      gimp_pixel_rgn_set_rect (&pixel_rgn, pLine, 0, drawable->height-1-y, drawable->width, 1);
	    }
	  g_free (pLine);
	}
      else if (8 == nBitsPS)
	{
	  int y;
	  /* copy line by line */
	  /* This will only be reached for new images, so no need for alpha */
	  for (y = 0; y < drawable->height; y++)
	    {
	      if ((interactive) && (StepProgress (y, drawable->height)))
		gimp_progress_update ((double)y / drawable->height);

	      pLine = pData + y*nSizeLine; /* adjust pointer */
	      gimp_pixel_rgn_set_row (&pixel_rgn, pLine, 0, drawable->height-1-y, drawable->width);
	    }
	}
      else
	{
	  /* copy and shift bits */
	  g_message ("%d bits per sample not yet supported!", nBitsPS);
	}

      gimp_drawable_flush (drawable);
      gimp_drawable_detach (drawable);

      /* Don't forget to display the new image! */
      if (bIsNewImage)
	gimp_display_new (image_ID);
      else
	{
	  gimp_drawable_set_visible (drawable_ID, TRUE);
	  gimp_displays_flush ();
	}
    }

  /* done */
  /* clear clipboard? */
  if (NULL != hDIB)
    {
      GlobalUnlock (pInfo);
      GlobalFree (hDIB);
    }

  CloseClipboard ();

  /* shouldn't this be done by caller?? */
  if (!gimp_image_undo_is_enabled (image_ID))
    gimp_image_undo_enable (image_ID);

  return bRet;
} /* CB_PasteImage */