예제 #1
0
// returns the allocated buffer
CoolImage *AllocBuffer(long w,long h,int fd)
{
	CoolImage *i=(CoolImage*)malloc(sizeof(*i));


	if (!i) return 0;

	// the width/height are always what we're passed in
	i->width=w;
	i->height=h;

	// our blit type 0 is a straight memory blit
	if (blittype==0)
	{
		//i->pitch=w*2;
		i->pitch=w * deep;
		if (i->buffer=(unsigned char*)malloc(w*h*deep))
			return i;
	}else
	if (blittype==1)
	{
		i->pitch=w * deep;
		if (i->buffer=(unsigned char*)PgShmemCreate(w*h*deep,NULL))
		    return i;
	}else
	if (blittype==2)
	{
		if (i->ctx=PdCreateOffscreenContext(Pg_IMAGE_DIRECT_888,w,h,Pg_OSC_MEM_PAGE_ALIGN))
		{
			i->pitch=i->ctx->pitch;
			//i->ctx->pitch = 1024 * 2;
			i->buffer=(unsigned char *)PdGetOffscreenContextPtr(i->ctx);
			return i;
		}
	}

	// if we fail, free the CoolImage structure, and return 0
	free(i);
	return 0;
}
예제 #2
0
/** ----------------------------------------------------------------
 * Initialize the nsImagePh object
 * @param aWidth - Width of the image
 * @param aHeight - Height of the image
 * @param aDepth - Depth of the image
 * @param aMaskRequirements - A mask used to specify if alpha is needed.
 * @result NS_OK if the image was initied ok
 */
nsresult nsImagePh :: Init(PRInt32 aWidth, PRInt32 aHeight, PRInt32 aDepth,nsMaskRequirements aMaskRequirements)
{
	int type = -1;

	mImageFlags = 0;

	if (mImageBits != nsnull)
	{
		if( mImageFlags & IMAGE_SHMEM ) PgShmemDestroy( mImageBits );
		else delete [] mImageBits;
		mImageBits = nsnull;
	}

	if (mAlphaBits != nsnull)
	{
		if( mImageFlags & ALPHA_SHMEM ) PgShmemDestroy( mAlphaBits );
		else delete [] mAlphaBits;
		mAlphaBits = nsnull;
	}

	if( mPhImageZoom ) {
		if( mImageFlags & ZOOM_SHMEM ) PgShmemDestroy( mPhImageZoom->image );
		else free( mPhImageZoom->image );
		if( mPhImageZoom->mask_bm )
			free( mPhImageZoom->mask_bm );
		free( mPhImageZoom );
		mPhImageZoom = NULL;
		}
  
  SetDecodedRect(0,0,0,0);  //init
 
  switch (aDepth)
    {
        case 24:
            type = Pg_IMAGE_DIRECT_888;
            mNumBytesPixel = 3;
            break;
//      case 16:
//          type = Pg_IMAGE_DIRECT_555;
//          mNumBytesPixel = 2;
//          break;
      case 8:
//          type = Pg_IMAGE_PALETTE_BYTE;
//          mNumBytesPixel = 1;
//          break;
        default:
            NS_ASSERTION(PR_FALSE, "unexpected image depth");
            return NS_ERROR_UNEXPECTED;
            break;
    }
 
	mWidth = aWidth;
	mHeight = aHeight;
	mDepth = aDepth;

	/* Allocate the Image Data */
	PRInt32 image_size = mNumBytesPixel * mWidth * mHeight;

	/* TODO: don't allow shared memory contexts if the graphics driver isn't a local device */

  if (image_size >= IMAGE_SHMEM_THRESHOLD)
  {
		mImageBits = (PRUint8 *) PgShmemCreate( image_size, NULL );
		mImageFlags |= IMAGE_SHMEM;
  }
  else
  {
		mImageBits = new PRUint8[ image_size ];
	 	memset( mImageBits, 0, image_size );
	}

	switch(aMaskRequirements)
	{
		default:
		case nsMaskRequirements_kNoMask:
			mAlphaBits = nsnull;
			mAlphaWidth = 0;
			mAlphaHeight = 0;
			mAlphaRowBytes = 0;
			break;

		case nsMaskRequirements_kNeeds1Bit:
			{
			mAlphaRowBytes = (aWidth + 7) / 8;
			mAlphaDepth = 1;

			int alphasize = mAlphaRowBytes * aHeight;
			mAlphaBits = new PRUint8[ alphasize ];
			memset( mAlphaBits, 0, alphasize );

			mAlphaWidth = aWidth;
			mAlphaHeight = aHeight;
			}
			break;

		case nsMaskRequirements_kNeeds8Bit:
			{
			mAlphaRowBytes = aWidth;
			mAlphaDepth = 8;

			int alphasize = mAlphaRowBytes * aHeight;
			if( alphasize > IMAGE_SHMEM_THRESHOLD ) {
				mAlphaBits = ( PRUint8 * ) PgShmemCreate( alphasize, NULL );
				mImageFlags |= ALPHA_SHMEM;
				}
			else mAlphaBits = new PRUint8[ alphasize ];
			memset( mAlphaBits, 0, alphasize );

			mAlphaWidth = aWidth;
			mAlphaHeight = aHeight;
			}
			break;
	}

	// mPhImage.image_tag = PtCRC( (char *)mImageBits, image_size );
	mPhImage.image = (char *)mImageBits;
	mPhImage.size.w = mWidth;
	mPhImage.size.h = 0;
	mRowBytes = mPhImage.bpl = mNumBytesPixel * mWidth;
	mPhImage.type = type;
	if (aMaskRequirements == nsMaskRequirements_kNeeds1Bit)
	{
		mPhImage.mask_bm = (char *)mAlphaBits;
		mPhImage.mask_bpl = mAlphaRowBytes;
	}

  	return NS_OK;
}