temp-buf.c 14.1 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19
20
#include "config.h"

21
#include <gtk/gtk.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
22

Elliot Lee's avatar
Elliot Lee committed
23
24
25
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
26
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
27
#include <unistd.h>
28
#endif
Elliot Lee's avatar
Elliot Lee committed
29
#include <string.h>
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
30
#include <sys/types.h>
Elliot Lee's avatar
Elliot Lee committed
31
#include <sys/stat.h>
32

33
#ifdef G_OS_WIN32
34
35
36
#include <process.h>		/* For _getpid() */
#endif
 
Sven Neumann's avatar
Sven Neumann committed
37
38
#include "apptypes.h"

Elliot Lee's avatar
Elliot Lee committed
39
#include "appenv.h"
40
#include "gimpimage.h"
Elliot Lee's avatar
Elliot Lee committed
41
42
#include "gimprc.h"
#include "paint_funcs.h"
43
#include "pixel_region.h"
Elliot Lee's avatar
Elliot Lee committed
44
45
#include "temp_buf.h"

46
#include "libgimp/gimpcolorspace.h"
Elliot Lee's avatar
Elliot Lee committed
47

48
49

static guchar * temp_buf_allocate (guint);
50
51
52
53
static void     temp_buf_to_color (TempBuf *src_buf,
				   TempBuf *dest_buf);
static void     temp_buf_to_gray  (TempBuf *src_buf,
				   TempBuf *dest_buf);
Elliot Lee's avatar
Elliot Lee committed
54
55
56
57


/*  Memory management  */

58
59
static guchar *
temp_buf_allocate (guint size)
Elliot Lee's avatar
Elliot Lee committed
60
{
61
  guchar *data;
Elliot Lee's avatar
Elliot Lee committed
62

63
  data = g_new (guchar, size);
Elliot Lee's avatar
Elliot Lee committed
64
65
66
67
68
69
70
71

  return data;
}


/*  The conversion routines  */

static void
Sven Neumann's avatar
Sven Neumann committed
72
73
temp_buf_to_color (TempBuf *src_buf, 
		   TempBuf *dest_buf)
Elliot Lee's avatar
Elliot Lee committed
74
{
75
76
  guchar *src;
  guchar *dest;
77
  glong   num_bytes;
Elliot Lee's avatar
Elliot Lee committed
78
79
80
81
82
83
84
85

  src = temp_buf_data (src_buf);
  dest = temp_buf_data (dest_buf);

  num_bytes = src_buf->width * src_buf->height;

  while (num_bytes--)
    {
86
      guchar tmpch;
Elliot Lee's avatar
Elliot Lee committed
87
      *dest++ = *src++;  /* alpha channel */
88
89
90
      *dest++ = tmpch = *src++;
      *dest++ = tmpch;
      *dest++ = tmpch;
Elliot Lee's avatar
Elliot Lee committed
91
92
93
94
95
    }
}


static void
Sven Neumann's avatar
Sven Neumann committed
96
97
temp_buf_to_gray (TempBuf *src_buf, 
		  TempBuf *dest_buf)
Elliot Lee's avatar
Elliot Lee committed
98
{
99
100
  guchar *src;
  guchar *dest;
101
102
  glong   num_bytes;
  gfloat  pix;
Elliot Lee's avatar
Elliot Lee committed
103
104
105
106
107
108
109
110
111
112

  src = temp_buf_data (src_buf);
  dest = temp_buf_data (dest_buf);

  num_bytes = src_buf->width * src_buf->height;

  while (num_bytes--)
    {
      *dest++ = *src++;  /* alpha channel */

113
      pix = INTENSITY (*src++, *src++, *src++);
Elliot Lee's avatar
Elliot Lee committed
114

115
      *dest++ = (guchar) pix;
Elliot Lee's avatar
Elliot Lee committed
116
117
118
119
    }
}

TempBuf *
120
121
122
123
124
125
temp_buf_new (gint    width, 
	      gint    height, 
	      gint    bytes, 
	      gint    x, 
	      gint    y, 
	      guchar *col)
Elliot Lee's avatar
Elliot Lee committed
126
{
127
128
  glong    i;
  gint     j;
129
130
  guchar  *data;
  TempBuf *temp;
Elliot Lee's avatar
Elliot Lee committed
131

132
  temp = g_new (TempBuf, 1);
Elliot Lee's avatar
Elliot Lee committed
133

134
135
136
137
138
139
  temp->width    = width;
  temp->height   = height;
  temp->bytes    = bytes;
  temp->x        = x;
  temp->y        = y;
  temp->swapped  = FALSE;
Elliot Lee's avatar
Elliot Lee committed
140
141
  temp->filename = NULL;

142
  temp->data = data = temp_buf_allocate (width * height * bytes);
Elliot Lee's avatar
Elliot Lee committed
143
144
145
146

  /*  initialize the data  */
  if (col)
    {
147
148
149
150
151
152
153
154
155
      /* First check if we can save a lot of work */
      if (bytes == 1)
        {
          memset (data, *col, width * height);
        }
      else if ((bytes == 3) && (col[1] == *col) && (*col == col[2]))
        {
          memset (data, *col, width * height * 3);
        }
156
157
      else if ((bytes == 4) &&
	       (col[1] == *col) && (*col == col[2]) && (col[2] == col[3]))
158
159
160
161
162
163
        {
          memset (data, *col, (width * height) << 2);
        }
      else
        {
          /* No, we cannot */
164
165
          guchar *dptr;

166
167
          /* Fill the first row */
          dptr = data;
168

169
170
          for (i = width - 1; i >= 0; --i)
            {
171
172
173
              guchar *init;

              j    = bytes;
174
              init = col;
175

176
177
178
              while (j--)
                *dptr++ = *init++;
            }
179

180
181
          /* Now copy from it (we set bytes to bytesperrow now) */
          bytes *= width;
182

183
184
185
186
187
188
          while (--height)
            {
              memcpy (dptr, data, bytes);
              dptr += bytes;
            }
        }
Elliot Lee's avatar
Elliot Lee committed
189
190
191
192
193
194
    }

  return temp;
}

TempBuf *
Sven Neumann's avatar
Sven Neumann committed
195
196
temp_buf_copy (TempBuf *src, 
	       TempBuf *dest)
Elliot Lee's avatar
Elliot Lee committed
197
{
198
199
  TempBuf *new;
  glong    length;
Elliot Lee's avatar
Elliot Lee committed
200
201
202

  if (!src)
    {
203
      g_message ("trying to copy a temp buf which is NULL.");
Elliot Lee's avatar
Elliot Lee committed
204
205
206
207
      return dest;
    }

  if (!dest)
208
209
210
    {
      new = temp_buf_new (src->width, src->height, src->bytes, 0, 0, NULL);
    }
Elliot Lee's avatar
Elliot Lee committed
211
212
213
214
  else
    {
      new = dest;
      if (dest->width != src->width || dest->height != src->height)
215
	g_message ("In temp_buf_copy, the widths or heights don't match.");
216
217
218
219
220
221
222
223
224
      /*  The temp buf is smart, and can translate between color and gray  */
      /*  (only necessary if not we allocated it */
      if (src->bytes != new->bytes)
        {
          if (src->bytes == 4)  /* RGB color */
	    temp_buf_to_gray (src, new);
          else if (src->bytes == 2) /* grayscale */
	    temp_buf_to_color (src, new);
          else
225
	    g_message ("Cannot convert from indexed color.");
226

227
228
	  return new;
        }
Elliot Lee's avatar
Elliot Lee committed
229
230
    }

231
232
233
  /* make the copy */
  length = src->width * src->height * src->bytes;
  memcpy (temp_buf_data (new), temp_buf_data (src), length);
Elliot Lee's avatar
Elliot Lee committed
234
235
236
237
238

  return new;
}

TempBuf *
239
240
241
242
243
244
temp_buf_resize (TempBuf *buf,
		 gint     bytes,
		 gint     x,
		 gint     y,
		 gint     width,
		 gint     height)
Elliot Lee's avatar
Elliot Lee committed
245
{
246
  gint size;
Elliot Lee's avatar
Elliot Lee committed
247
248

  /*  calculate the requested size  */
249
  size = width * height * bytes;
Elliot Lee's avatar
Elliot Lee committed
250
251
252

  /*  First, configure the canvas buffer  */
  if (!buf)
253
254
255
    {
      buf = temp_buf_new (width, height, bytes, x, y, NULL);
    }
Elliot Lee's avatar
Elliot Lee committed
256
257
258
259
260
261
262
263
264
265
266
267
  else
    {
      if (size != (buf->width * buf->height * buf->bytes))
      {
	/*  Make sure the temp buf is unswapped  */
	temp_buf_unswap (buf);

	/*  Reallocate the data for it  */
	buf->data = g_realloc (buf->data, size);
      }

      /*  Make sure the temp buf fields are valid  */
268
269
270
271
272
      buf->x      = x;
      buf->y      = y;
      buf->width  = width;
      buf->height = height;
      buf->bytes  = bytes;
Elliot Lee's avatar
Elliot Lee committed
273
274
275
276
277
278
    }

  return buf;
}

TempBuf *
279
280
281
282
283
284
temp_buf_copy_area (TempBuf *src,
		    TempBuf *dest,
		    gint     x,
		    gint     y,
		    gint     width,
		    gint     height,
285
		    gint     border)
Elliot Lee's avatar
Elliot Lee committed
286
{
287
288
289
290
  TempBuf     *new;
  PixelRegion  srcR, destR;
  guchar       empty[MAX_CHANNELS] = { 0, 0, 0, 0 };
  gint         x1, y1, x2, y2;
Elliot Lee's avatar
Elliot Lee committed
291
292
293

  if (!src)
    {
294
      g_message ("trying to copy a temp buf which is NULL.");
Elliot Lee's avatar
Elliot Lee committed
295
296
297
298
      return dest;
    }

  /*  some bounds checking  */
299
300
  x1 = CLAMP (x, 0, src->width);
  y1 = CLAMP (y, 0, src->height);
301
302
  x2 = CLAMP (x + width, 0, src->width);
  y2 = CLAMP (y + height, 0, src->height);
Elliot Lee's avatar
Elliot Lee committed
303
304
305
306

  if (!(x2 - x1) || !(y2 - y1))
    return dest;

307
308
309
310
  x      = x1 - border;
  y      = y1 - border;
  width  = (x2 - x1) + border * 2;
  height = (y2 - y1) + border * 2;
Elliot Lee's avatar
Elliot Lee committed
311
312

  if (!dest)
313
314
315
    {
      new = temp_buf_new (width, height, src->bytes, x, y, empty);
    }
Elliot Lee's avatar
Elliot Lee committed
316
317
318
319
  else
    {
      new = dest;
      if (dest->bytes != src->bytes)
320
	g_message ("In temp_buf_copy_area, the widths or heights or bytes don't match.");
Elliot Lee's avatar
Elliot Lee committed
321
322
323
324
325
326
327
    }

  /*  Set the offsets for the dest  */
  new->x = src->x + x;
  new->y = src->y + y;

  /*  Copy the region  */
328
329
330
  srcR.bytes     = src->bytes;
  srcR.w         = (x2 - x1);
  srcR.h         = (y2 - y1);
Elliot Lee's avatar
Elliot Lee committed
331
  srcR.rowstride = src->bytes * src->width;
332
  srcR.data      = temp_buf_data (src) + y1 * srcR.rowstride + x1 * srcR.bytes;
Elliot Lee's avatar
Elliot Lee committed
333
334

  destR.rowstride = new->bytes * new->width;
335
  destR.data      = temp_buf_data (new) + (y1 - y) * destR.rowstride + (x1 - x) * srcR.bytes;
Elliot Lee's avatar
Elliot Lee committed
336
337
338
339
340
341
342

  copy_region (&srcR, &destR);

  return new;
}

void
Sven Neumann's avatar
Sven Neumann committed
343
temp_buf_free (TempBuf *temp_buf)
Elliot Lee's avatar
Elliot Lee committed
344
345
346
347
348
349
350
351
352
353
{
  if (temp_buf->data)
    g_free (temp_buf->data);

  if (temp_buf->swapped)
    temp_buf_swap_free (temp_buf);

  g_free (temp_buf);
}

354
guchar *
Sven Neumann's avatar
Sven Neumann committed
355
temp_buf_data (TempBuf *temp_buf)
Elliot Lee's avatar
Elliot Lee committed
356
357
358
359
360
361
362
{
  if (temp_buf->swapped)
    temp_buf_unswap (temp_buf);

  return temp_buf->data;
}

363
364
365
366
367
368
guchar *
temp_buf_data_clear (TempBuf *temp_buf)
{
  if (temp_buf->swapped)
    temp_buf_unswap (temp_buf);
  
369
370
371
  memset (temp_buf->data, 0,
	  temp_buf->height * temp_buf->width * temp_buf->bytes);

372
373
  return temp_buf->data;
}
Elliot Lee's avatar
Elliot Lee committed
374
375
376
377
378
379
380

/******************************************************************
 *  Mask buffer functions                                         *
 ******************************************************************/


MaskBuf *
381
382
mask_buf_new (gint width, 
	      gint height)
Elliot Lee's avatar
Elliot Lee committed
383
{
384
  static guchar empty = 0;
Elliot Lee's avatar
Elliot Lee committed
385

386
  return temp_buf_new (width, height, 1, 0, 0, &empty);
Elliot Lee's avatar
Elliot Lee committed
387
388
389
}

void
Sven Neumann's avatar
Sven Neumann committed
390
mask_buf_free (MaskBuf *mask)
Elliot Lee's avatar
Elliot Lee committed
391
392
393
394
{
  temp_buf_free ((TempBuf *) mask);
}

395
guchar *
Sven Neumann's avatar
Sven Neumann committed
396
mask_buf_data (MaskBuf *mask_buf)
Elliot Lee's avatar
Elliot Lee committed
397
398
{
  if (mask_buf->swapped)
399
    temp_buf_unswap ((TempBuf *) mask_buf);
Elliot Lee's avatar
Elliot Lee committed
400
401
402
403

  return mask_buf->data;
}

404
405
406
407
408
409
410
411
412
guchar *
mask_buf_data_clear (MaskBuf *mask_buf)
{
  if (mask_buf->swapped)
    temp_buf_unswap ((TempBuf *) mask_buf);

  return temp_buf_data_clear ((TempBuf *) mask_buf);
}

Elliot Lee's avatar
Elliot Lee committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432

/******************************************************************
 *  temp buffer disk caching functions                            *
 ******************************************************************/

/*  NOTES:
 *  Disk caching is setup as follows:
 *    On a call to temp_buf_swap, the TempBuf parameter is stored
 *    in a temporary variable called cached_in_memory.
 *    On the next call to temp_buf_swap, if cached_in_memory is non-null,
 *    cached_in_memory is moved to disk, and the latest TempBuf parameter
 *    is stored in cached_in_memory.  This method keeps the latest TempBuf
 *    structure in memory instead of moving it directly to disk as requested.
 *    On a call to temp_buf_unswap, if cached_in_memory is non-null, it is
 *    compared against the requested TempBuf.  If they are the same, nothing
 *    must be moved in from disk since it still resides in memory.  However,
 *    if the two pointers are different, the requested TempBuf is retrieved
 *    from disk.  In the former case, cached_in_memory is set to NULL;
 *    in the latter case, cached_in_memory is left unchanged.
 *    If temp_buf_swap_free is called, cached_in_memory must be checked
Sven Neumann's avatar
Sven Neumann committed
433
434
 *    against the temp buf being freed.  If they are the same, then 
 *    cached_in_memory must be set to NULL;
Elliot Lee's avatar
Elliot Lee committed
435
436
437
438
439
 *
 *  In the case where memory usage is set to "stingy":
 *    temp bufs are not cached in memory at all, they go right to disk.
 */

Sven Neumann's avatar
Sven Neumann committed
440

Elliot Lee's avatar
Elliot Lee committed
441
/*  a static counter for generating unique filenames  */
442
static gint swap_index = 0;
Elliot Lee's avatar
Elliot Lee committed
443
444
445
446
447

/*  a static pointer which keeps track of the last request for a swapped buffer  */
static TempBuf * cached_in_memory = NULL;


448
static gchar *
Elliot Lee's avatar
Elliot Lee committed
449
450
451
generate_unique_filename (void)
{
  pid_t pid;
452

Elliot Lee's avatar
Elliot Lee committed
453
  pid = getpid ();
454
455
  return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "gimp%d.%d",
			  temp_path, (int) pid, swap_index++);
Elliot Lee's avatar
Elliot Lee committed
456
457
458
}

void
Sven Neumann's avatar
Sven Neumann committed
459
temp_buf_swap (TempBuf *buf)
Elliot Lee's avatar
Elliot Lee committed
460
{
461
462
463
464
465
  TempBuf     *swap;
  gchar       *filename;
  struct stat  stat_buf;
  gint         err;
  FILE        *fp;
Elliot Lee's avatar
Elliot Lee committed
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

  if (!buf || buf->swapped)
    return;

  /*  Set the swapped flag  */
  buf->swapped = TRUE;

  if (stingy_memory_use)
    swap = buf;
  else
    {
      swap = cached_in_memory;
      cached_in_memory = buf;
    }

  /*  For the case where there is no temp buf ready to be moved to disk, return  */
  if (!swap)
    return;

  /*  Get a unique filename for caching the data to a UNIX file  */
  filename = generate_unique_filename ();

  /*  Check if generated filename is valid  */
  err = stat (filename, &stat_buf);
  if (!err)
    {
      if (stat_buf.st_mode & S_IFDIR)
	{
494
	  g_message ("Error in temp buf caching: \"%s\" is a directory (cannot overwrite)", filename);
Elliot Lee's avatar
Elliot Lee committed
495
496
497
498
499
500
	  g_free (filename);
	  return;
	}
    }

  /*  Open file for overwrite  */
501
  if ((fp = fopen (filename, "wb")))
Elliot Lee's avatar
Elliot Lee committed
502
    {
503
504
505
506
507
508
509
      size_t blocks_written;
      blocks_written = fwrite (swap->data, swap->width * swap->height * swap->bytes, 1, fp);
      /* Check whether all bytes were written and fclose() was able to flush its buffers */
      if ((0 != fclose (fp)) || (1 != blocks_written))
        {
          (void) unlink (filename);
          perror ("Write error on temp buf");
510
          g_message ("Cannot write \"%s\"", filename);
511
512
513
          g_free (filename);
          return;
        }
Elliot Lee's avatar
Elliot Lee committed
514
515
516
    }
  else
    {
517
      (void) unlink (filename);
Elliot Lee's avatar
Elliot Lee committed
518
      perror ("Error in temp buf caching");
519
      g_message ("Cannot write \"%s\"", filename);
Elliot Lee's avatar
Elliot Lee committed
520
521
522
523
524
525
526
527
528
529
530
      g_free (filename);
      return;
    }
  /*  Finally, free the buffer's data  */
  g_free (swap->data);
  swap->data = NULL;

  swap->filename = filename;
}

void
Sven Neumann's avatar
Sven Neumann committed
531
temp_buf_unswap (TempBuf *buf)
Elliot Lee's avatar
Elliot Lee committed
532
{
533
534
535
  struct stat  stat_buf;
  FILE        *fp;
  gboolean     succ = FALSE;
Elliot Lee's avatar
Elliot Lee committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553

  if (!buf || !buf->swapped)
    return;

  /*  Set the swapped flag  */
  buf->swapped = FALSE;

  /*  If the requested temp buf is still in memory, simply return  */
  if (cached_in_memory == buf)
    {
      cached_in_memory = NULL;
      return;
    }

  /*  Allocate memory for the buffer's data  */
  buf->data   = temp_buf_allocate (buf->width * buf->height * buf->bytes);

  /*  Find out if the filename of the swapped data is an existing file... */
554
  /*  (buf->filname HAS to be != 0 */
Elliot Lee's avatar
Elliot Lee committed
555
556
  if (!stat (buf->filename, &stat_buf))
    {
557
      if ((fp = fopen (buf->filename, "rb")))
Elliot Lee's avatar
Elliot Lee committed
558
	{
559
560
561
562
563
	  size_t blocks_read;
	  blocks_read = fread (buf->data, buf->width * buf->height * buf->bytes, 1, fp);
	  (void) fclose (fp);
	  if (blocks_read != 1)
            perror ("Read error on temp buf");
564
565
	  else
	    succ = TRUE;
Elliot Lee's avatar
Elliot Lee committed
566
567
568
569
570
571
572
	}
      else
	perror ("Error in temp buf caching");

      /*  Delete the swap file  */
      unlink (buf->filename);
    }
573
  if (!succ)
574
    g_message ("Error in temp buf caching: information swapped to disk was lost!");
Elliot Lee's avatar
Elliot Lee committed
575

576
  g_free (buf->filename);   /*  free filename  */
Elliot Lee's avatar
Elliot Lee committed
577
578
579
580
  buf->filename = NULL;
}

void
Sven Neumann's avatar
Sven Neumann committed
581
temp_buf_swap_free (TempBuf *buf)
Elliot Lee's avatar
Elliot Lee committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
{
  struct stat stat_buf;

  if (!buf->swapped)
    return;

  /*  Set the swapped flag  */
  buf->swapped = FALSE;

  /*  If the requested temp buf is cached in memory...  */
  if (cached_in_memory == buf)
    {
      cached_in_memory = NULL;
      return;
    }

  /*  Find out if the filename of the swapped data is an existing file... */
  if (!stat (buf->filename, &stat_buf))
    {
      /*  Delete the swap file  */
      unlink (buf->filename);
    }
  else
605
    g_message ("Error in temp buf disk swapping: information swapped to disk was lost!");
Elliot Lee's avatar
Elliot Lee committed
606
607
608
609
610
611
612
613
614
615
616
617

  if (buf->filename)
    g_free (buf->filename);   /*  free filename  */
  buf->filename = NULL;
}

void
swapping_free (void)
{
  if (cached_in_memory)
    temp_buf_free (cached_in_memory);
}