The R Project SVN R

Rev

Rev 6430 | Rev 6459 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6430 Rev 6458
Line 332... Line 332...
332
  jpeg_finish_compress(&cinfo);
332
  jpeg_finish_compress(&cinfo);
333
 
333
 
334
  /* Step 7: release JPEG compression object */
334
  /* Step 7: release JPEG compression object */
335
 
335
 
336
  /* This is an important step since it will release a good deal of memory. */
336
  /* This is an important step since it will release a good deal of memory. */
-
 
337
  free(scanline);
337
  jpeg_destroy_compress(&cinfo);
338
  jpeg_destroy_compress(&cinfo);
338
 
339
 
-
 
340
 
339
  /* And we're done! */
341
  /* And we're done! */
340
  return 1;
342
  return 1;
341
}
343
}
342
 
344
 
343
#endif /* HAVE_JPEG */
345
#endif /* HAVE_JPEG */
344
 
346
 
-
 
347
/* 
-
 
348
 * Try to save the content of the device 'd' in 'filename' as Windows BMP.
-
 
349
 * If numbers of colors is less than 256 we use a 'palette' BMP.
-
 
350
 * Return 1 on success, 0 on failure 
-
 
351
*/
-
 
352
 
-
 
353
#define BMPERROR {R_ShowMessage("Problems writing to 'bmp' file");return 0;} 
-
 
354
#define BMPW(a) {wrd=a;if(fwrite(&wrd,sizeof(unsigned short),1,fp)!=1) BMPERROR}
-
 
355
#define BMPDW(a) {dwrd=a;if(fwrite(&dwrd,sizeof(unsigned long),1,fp)!=1) BMPERROR}
-
 
356
#define BMPLONG(a) {lng=a;if(fwrite(&lng,sizeof(long),1,fp)!=1) BMPERROR}
-
 
357
#define BMPPUTC(a) if(fputc(a,fp)==EOF) BMPERROR;
-
 
358
#define HEADERSIZE 54
-
 
359
 
-
 
360
int R_SaveAsBmp(void  *d, int width, int height, 
-
 
361
		unsigned long (*gp)(void *, int, int), int bgr, FILE *fp) 
-
 
362
{
-
 
363
  unsigned long  col, palette[256];
-
 
364
  int i, j, r, ncols, mid, high, low, withpalette;
-
 
365
  int bfOffBits, bfSize, biBitCount, biClrUsed , pad;
-
 
366
  unsigned short wrd;
-
 
367
  unsigned long dwrd;
-
 
368
  long lng;
-
 
369
  DECLARESHIFTS;
-
 
370
 
-
 
371
  /* Have we less than 256 different colors? */
-
 
372
  ncols = mid = 0;
-
 
373
  withpalette = 1;
-
 
374
  for (i=0; i<256 ; i++) palette[i] = 0;
-
 
375
  for (i = 0; (i < height) && withpalette ; i++) {
-
 
376
    for (j = 0; (j < width) && withpalette ; j++) {
-
 
377
      col = gp(d,i,j) & 0xFFFFFFUL ;
-
 
378
      /* binary search the palette: */
-
 
379
      low = 0;  
-
 
380
      high = ncols - 1;
-
 
381
      while (low <= high) {
-
 
382
	mid = (low + high)/2;
-
 
383
	if ( col < palette[mid] ) high = mid - 1;
-
 
384
	else if ( col > palette[mid] ) low  = mid + 1;
-
 
385
	else break;
-
 
386
      }
-
 
387
      if (high < low) {
-
 
388
	/* didn't find colour in palette, insert it: */
-
 
389
	if (ncols >= 256) {
-
 
390
	  withpalette = 0;
-
 
391
	} else {
-
 
392
	  for (r = ncols; r > low; r--)
-
 
393
	    palette[r] = palette[r-1] ;
-
 
394
	  palette[low] = col;
-
 
395
	  ncols ++;
-
 
396
	}
-
 
397
      }
-
 
398
    }
-
 
399
  }
-
 
400
  /* Compute some part of the header */
-
 
401
  if (withpalette) {
-
 
402
    bfOffBits = HEADERSIZE + 4 * 256;
-
 
403
    bfSize = bfOffBits + width * height ;
-
 
404
    biBitCount = 8;
-
 
405
    biClrUsed = 256;
-
 
406
  } else {
-
 
407
    bfOffBits = HEADERSIZE ;
-
 
408
    bfSize = bfOffBits + 3 * width * height ;
-
 
409
    biBitCount = 24;
-
 
410
    biClrUsed = 0;
-
 
411
  }
-
 
412
 
-
 
413
  /* write the header */
-
 
414
  BMPW(0x4D42); /* bfType must be "BM" */
-
 
415
  BMPDW(bfSize); /*bfSize*/ 
-
 
416
  BMPW(0);BMPW(0); /* bfReserved1 and bfReserved2 must be 0*/
-
 
417
  BMPDW(bfOffBits); /* bfOffBits */
-
 
418
  BMPDW(40);	/* biSize */
-
 
419
  BMPLONG(width); /* biWidth */
-
 
420
  BMPLONG(height); /* biHeight */
-
 
421
  BMPW(1);	/* biPlanes - must be 1 */
-
 
422
  BMPW(biBitCount); /* biBitCount */
-
 
423
  BMPDW(0); /* biCompression=BI_RGB */
-
 
424
  BMPDW(0); /* biSizeImage (with BI_RGB not needed)*/
-
 
425
  BMPLONG(0); /* XPels/M <- used only by Windows?*/
-
 
426
  BMPLONG(0); /* XPels/M */
-
 
427
  BMPDW(biClrUsed); /* biClrUsed */
-
 
428
  BMPDW(0) ; /* biClrImportant All colours are important */
-
 
429
 
-
 
430
  /* and now the pixels */
-
 
431
  if (withpalette) {
-
 
432
    /* 8 bit image; write the palette */
-
 
433
    for ( i=0; i<256; i++) {
-
 
434
      col = palette[i];
-
 
435
      BMPPUTC(GETBLUE(col));
-
 
436
      BMPPUTC(GETGREEN(col));
-
 
437
      BMPPUTC(GETRED(col));
-
 
438
      BMPPUTC(0);
-
 
439
    }
-
 
440
    /* Rows must be padded to 4-byte boundary */
-
 
441
    for ( pad=0 ; ((width+pad) & 3) != 0; pad++);
-
 
442
    /* and then the pixels */
-
 
443
    for (i=height-1 ; i>=0 ; i--) { 
-
 
444
      for ( j=0 ; j<width ; j++) {
-
 
445
	col = gp(d, i, j)&0xFFFFFFUL;
-
 
446
	/* binary search the palette (the colour must be there): */
-
 
447
	low = 0;  high = ncols - 1;
-
 
448
	while (low <= high) {
-
 
449
	  mid = (low + high)/2;
-
 
450
	  if      (col < palette[mid]) high = mid - 1;
-
 
451
	  else if (col > palette[mid]) low  = mid + 1;
-
 
452
	  else break;
-
 
453
	}
-
 
454
	BMPPUTC(mid);
-
 
455
      }
-
 
456
      for (j=0; j<pad; j++) BMPPUTC(0);
-
 
457
    }
-
 
458
  } else {
-
 
459
    /* 24 bits image */
-
 
460
    BMPDW(0); /* null bmiColors */
-
 
461
    for ( pad=0 ; ((3*width+pad) & 3) != 0; pad++); /*padding*/    
-
 
462
    for (i=height-1 ; i>=0 ; i--) { 
-
 
463
      for ( j=0 ; j<width ; j++) {
-
 
464
	col = gp(d, i, j);
-
 
465
	BMPPUTC(GETBLUE(col));
-
 
466
	BMPPUTC(GETGREEN(col));
-
 
467
	BMPPUTC(GETRED(col));
-
 
468
      }
-
 
469
      for (j=0; j<pad; j++) BMPPUTC(0);
-
 
470
    }
-
 
471
  }
-
 
472
  return 1;
-
 
473
}
-
 
474
 
-
 
475
 
-
 
476
 
345
 
477
 
346
 
478
 
347
 
479
 
348
 
480
 
349
 
481