⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 img.c

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * img.c -  * * Copyright (c) 1998 *      Transvirtual Technologies, Inc.  All rights reserved. * * See the file "license.terms" for information on usage and redistribution  * of this file.  */#include "toolkit.h"/* interfaces of image conversion functions */Image* readGifFile ( int fd );Image* readGifData ( unsigned char*, long len );Image* readJpegFile ( int fd );Image* readJpegData ( unsigned char*, long len );Image* readPngFile ( int fd );Image* readPngData ( unsigned char*, long len );/************************************************************************************ * own auxiliary funcs */Image*createImage ( int width, int height ){  Image * img = AWT_CALLOC( 1, sizeof( Image));  img->trans  = -1;     /* default to no alpha */  img->width = width;   /* we need to (temp) store them for subsequent X image creation */  img->height = height;  return img;}static intcreateShmXImage ( Toolkit* X, Image* img, int depth, int isMask ){#if defined(USE_XSHM_EXTENSION)  Visual  *vis = DefaultVisual( X->dsp, DefaultScreen( X->dsp));  XShmSegmentInfo* shmi = (XShmSegmentInfo*) AWT_MALLOC( sizeof(XShmSegmentInfo));  XImage *xim;  unsigned int    nBytes;  if ( isMask ) {	xim = XShmCreateImage( X->dsp, vis, depth, XYBitmap, 0, shmi, img->width, img->height);  }  else {    xim = XShmCreateImage( X->dsp, vis, depth, ZPixmap, 0, shmi, img->width, img->height);  }  nBytes = xim->bytes_per_line * img->height;  shmi->shmid = shmget( IPC_PRIVATE, nBytes, (IPC_CREAT | 0777));  /*   * It is essential to check if shmget failed, because shared memory is usually   * a scarce resource   */  if ( shmi->shmid == -1 ) {	XShmDetach( X->dsp, shmi);	xim->data = 0;	XDestroyImage( xim);	AWT_FREE( shmi);	X->shm = SUSPEND_SHM;	return 0;  }  xim->data = shmi->shmaddr = shmat( shmi->shmid, 0, 0);  shmi->readOnly = False;  XShmAttach( X->dsp, shmi);  /*   * make sure it will be freed automatically once the attachment count comes   * down to 0 (either by explicit imgFreeImage or by process termination)   */  shmctl( shmi->shmid, IPC_RMID, 0);  if ( isMask ) {	memset( xim->data, 0xff, nBytes);	img->shmiMask = shmi;	img->xMask = xim;  }  else {	img->shmiImg = shmi;	img->xImg = xim;  }  return 1;#else  return 0;#endif}static voiddestroyShmXImage ( Toolkit* X, Image* img, int isMask ){#if defined(USE_XSHM_EXTENSION)  XShmSegmentInfo *shmi;  XImage          *xim;  if ( isMask ) {	shmi = img->shmiMask;	xim  = img->xMask;	img->shmiMask = 0;  }  else {	shmi = img->shmiImg;	xim  = img->xImg;	img->shmiImg = 0;  }  XShmDetach( X->dsp, shmi);  xim->data = 0;  XDestroyImage( xim);  /* we created it as 'deleted', so we just have to detach here */  shmdt( shmi->shmaddr);  AWT_FREE( shmi);  /* if we have suspended shm, give it a try again */  if ( X->shm == SUSPEND_SHM )	X->shm = USE_SHM;#endif}voidcreateXImage ( Toolkit* X, Image* img ){  int bitmap_pad;  int bytes_per_line;  int bytes_per_pix;  unsigned int nPix;  char *data;  Visual *vis  = DefaultVisual( X->dsp, DefaultScreen( X->dsp));  int    depth = DefaultDepth(  X->dsp, DefaultScreen( X->dsp));	  if ( depth <= 8)	      bytes_per_pix = 1;  else if ( depth <= 16)  bytes_per_pix = 2;  else			          bytes_per_pix = 4;  bytes_per_line = bytes_per_pix * img->width;  bitmap_pad = bytes_per_pix * 8;  nPix = img->width * img->height;  if ( (X->shm == USE_SHM) && (nPix > X->shmThreshold) && (img->alpha == 0) ) {	if ( createShmXImage( X, img, depth, False) ){	  DBG( AWT_IMG, printf("alloc Shm: %p %p %p (%dx%d) \n", img, img->xImg, img->shmiImg,					  img->width, img->height));	  return;	}  }  data = AWT_CALLOC( nPix, bytes_per_pix);  img->xImg = XCreateImage( X->dsp, vis, depth, ZPixmap, 0,							data, img->width, img->height, bitmap_pad, bytes_per_line);  DBG( AWT_IMG, printf( "alloc: %p %p (%dx%d)\n", img, img->xImg, img->width, img->height));}voidcreateXMaskImage ( Toolkit* X, Image* img ){  int     bytes_per_line;  unsigned int nBytes, nPix;  char    *data;  Visual  *vis = DefaultVisual( X->dsp, DefaultScreen( X->dsp));  bytes_per_line = (img->width + 7) / 8;  nPix   = img->width * img->height;  nBytes = bytes_per_line * img->height;  if ( (X->shm == USE_SHM) && (nPix > X->shmThreshold) ) {	if ( createShmXImage( X, img, 1, True) ){	  DBG( AWT_IMG, printf( "alloc Shm mask: %p %p %p (%dx%d) \n", img, img->xMask, img->shmiMask,					  img->width, img->height));	  return;	}  }  data = AWT_MALLOC( nBytes);  memset( data, 0xff, nBytes);  img->xMask = XCreateImage( X->dsp, vis, 1, XYBitmap, 0,							 data, img->width, img->height, 8, bytes_per_line );  DBG( AWT_IMG, printf( "alloc mask: %p %p (%dx%d)\n", img, img->xMask, img->width, img->height));}voidcreateAlphaImage ( Toolkit* X, Image *img ){  int nBytes = img->width * img->height;  img->alpha = AWT_MALLOC( sizeof( AlphaImage));  img->alpha->width  = img->width;  img->alpha->height = img->height;  img->alpha->buf = AWT_MALLOC( nBytes);  memset( img->alpha->buf, 0xff, nBytes);}/* * For images with a full alpha channel, check if we really need an alpha byte for * each pel, or if a mask bitmap (alpha 0x00 / 0xff) will be sufficient */intneedsFullAlpha ( Toolkit* X, Image *img, double threshold ){  int i, j, a;  int n = 0, max;  if ( !img->alpha ) return 0;  max = (img->width * img->height) * threshold;  for ( i=0; i<img->height; i++ ) {	for ( j=0; j<img->width; j++ ) {	  a = GetAlpha( img->alpha, j, i);	  if ( (a != 0) && (a != 0xff) ) {		if ( ++n > max )		  return 1;	  }	}  }  return 0;}/* * A full alpha image channel is way slower than using a mask bitmap (= 0 / 0xff alpha). * This function provides a simple alpha-to-mask translation *//* also used in imgpng */voidreduceAlpha ( Toolkit* X, Image* img, int threshold ){  int i, j, a;  if ( !img->alpha )	return;  createXMaskImage( X, img);  for ( i=0; i<img->height; i++ ) {	for ( j=0; j<img->width; j++ ) {	  a = GetAlpha( img->alpha, j, i);	  if ( a < threshold ) {		XPutPixel( img->xImg, j, i, 0);		XPutPixel( img->xMask, j, i, 0);	  }	}  }  AWT_FREE( img->alpha->buf);  AWT_FREE( img->alpha);  img->alpha = 0;}static inline intinterpolate ( int ul, int ur, int ll, int lr, double dx, double dy ){  double u = ul + (double)(ur - ul) * dx;  double l = ll + (double)(lr - ll) * dx;  return (int) (u + (l - u) * dy  + 0.5);}static unsigned intgetScaledAlpha ( Toolkit* X, Image* img, int x, int y, double dx, double dy ){  int   ul, ur, ll, lr, a;  int   xi = (dx) ? x+1 : x;  int   yi = (dy) ? y+1 : y;  if ( img->alpha ) {	ul = GetAlpha( img->alpha, x, y);	ur = GetAlpha( img->alpha, xi, y);	ll = GetAlpha( img->alpha, x, yi);	lr = GetAlpha( img->alpha, xi,yi);	a = (unsigned int) interpolate( ul, ur, ll, lr, dx, dy);	return a;  }  return 0xff;}static longgetScaledPixel ( Toolkit* X, Image* img, int x, int y, double dx, double dy ){  unsigned long  ul, ur, ll, lr;  int            ulR, urR, llR, lrR, ulG, urG, llG, lrG, ulB, urB, llB, lrB, r, g, b;  int            xi = (dx) ? x+1 : x;  int            yi = (dy) ? y+1 : y;  if ( img->xMask ) {	ul = XGetPixel( img->xMask, x, y);	ur = XGetPixel( img->xMask, xi, y);	ll = XGetPixel( img->xMask, x, yi);	lr = XGetPixel( img->xMask, xi,yi);		if ( !interpolate( ul, ur, ll, lr, dx, dy) )	  return -1;  }  ul = XGetPixel( img->xImg, x, y);  ur = XGetPixel( img->xImg, xi, y);  ll = XGetPixel( img->xImg, x, yi);  lr = XGetPixel( img->xImg, xi,yi);  if ( (ul == ur) && (ll == ul) && (lr == ll) ) {	rgbValues( X, ul, &r, &g, &b);  }  else {	rgbValues( X, ul, &ulR, &ulG, &ulB);	rgbValues( X, ur, &urR, &urG, &urB);	rgbValues( X, ll, &llR, &llG, &llB);	rgbValues( X, lr, &lrR, &lrG, &lrB);	r = interpolate( ulR, urR, llR, lrR, dx, dy);	g = interpolate( ulG, urG, llG, lrG, dx, dy);	b = interpolate( ulB, urB, llB, lrB, dx, dy);  }  return pixelValue( X, (r << 16) | (g << 8) | b);}voidinitScaledImage ( Toolkit* X, Image *tgt, Image *src,				  int dx0, int dy0, int dx1, int dy1,				  int sx0, int sy0, int sx1, int sy1 ){  double         xScale, yScale, sX, sY, sxDelta, syDelta;  int            dx, dy, dxInc, dyInc, sx, sy;  long           c;  dxInc = (dx1 > dx0) ? 1 : -1;  dyInc = (dy1 > dy0) ? 1 : -1;  dx1 += dxInc;  dy1 += dyInc;  xScale = (double) (dx1 - dx0) / (double) (sx1 - sx0 +1);  yScale = (double) (dy1 - dy0) / (double) (sy1 - sy0 +1);  for ( dy=dy0; dy != dy1; dy += dyInc ) {	sY = sy0 + (dy - dy0) / yScale;	sy = (int) sY;	syDelta = (sy < (sy1-1)) ? sY - sy : 0;	for ( dx=dx0; dx != dx1; dx += dxInc ) {	  sX = sx0 + (dx - dx0) / xScale;	  sx = (int) sX;	  sxDelta = (sx < (sx1-1)) ? sX - sx : 0;	  if ( (c = getScaledPixel( X, src, sx, sy, sxDelta, syDelta)) != -1 ){  		XPutPixel( tgt->xImg, dx, dy, c);		if ( src->alpha )		  PutAlpha( tgt->alpha, dx, dy, getScaledAlpha( X, src, sx, sy, sxDelta, syDelta));	  }	  else {		XPutPixel( tgt->xMask, dx, dy, 0);		XPutPixel( tgt->xImg, dx, dy, 0);	  }	}  }}/************************************************************************************ * exported native methods */void*Java_java_awt_Toolkit_imgCreateImage ( JNIEnv* env, jclass clazz, jint width, jint height ){  Image *img = createImage( width, height);  createXImage( X, img);  return img;}void*Java_java_awt_Toolkit_imgCreateScreenImage ( JNIEnv* env, jclass clazz, jint width, jint height ){  Image  *img = createImage( width, height);  int    depth = DefaultDepth(  X->dsp, DefaultScreen( X->dsp));  img->pix  = XCreatePixmap( X->dsp, X->root, width, height, depth);  return img;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -