Ejemplo n.º 1
0
void _HYPlatformGraphicPane::_EndDraw	 (void)
{
	UnlockPixels (GetGWorldPixMap(thePane));
	SetGWorld 	 (savedPort,savedDevice);
	RGBForeColor (&saveFG);
	RGBBackColor (&saveBG);
}				
Ejemplo n.º 2
0
wxMemoryDC::~wxMemoryDC()
{
    if ( m_selected.Ok() )
    {
        UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
    }
};
Ejemplo n.º 3
0
void EraseRectAndAlpha(GWorldPtr gWorld, Rect *pRect)
{
	PixMapHandle	pixMap = GetGWorldPixMap(gWorld);
	long			rows;
	Ptr				rowBaseAddr;


    LockPixels(pixMap);
	rows = pRect->bottom - pRect->top;
    
    rowBaseAddr = GetPixBaseAddr(pixMap) + (GetPixRowBytes(pixMap) & 0x3fff) * pRect->top + pRect->left * GetPixDepth(pixMap) / 8;
	do
	{
		long	cols;
		UInt32	*baseAddr;
		
		cols = pRect->right - pRect->left;
		baseAddr = (UInt32*)rowBaseAddr;
		rowBaseAddr += (**pixMap).rowBytes & 0x3fff;
		do
		{
			*baseAddr++ = 0;
		} while (--cols);
	} while (--rows);

    UnlockPixels(pixMap);

} // EraseRectAndAlpha
Ejemplo n.º 4
0
static void ROM_UnsetVideoMode(_THIS, SDL_Surface *current)
{
	/* Free the current window, if any */
	if ( SDL_Window != nil ) {
		GWorldPtr memworld;
		
		/* Handle OpenGL support */
		Mac_GL_Quit(this);

		memworld = (GWorldPtr)GetWRefCon(SDL_Window);
		if ( memworld != nil ) {
			UnlockPixels(GetGWorldPixMap(memworld));
			DisposeGWorld(memworld);
		}
		if ( (current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
#if USE_QUICKTIME
			EndFullScreen(fullscreen_ctx, nil);
			SDL_Window = nil;
#else
			ROM_ShowMenuBar(this);
#endif
		}
	}
	current->pixels = NULL;
	current->flags &= ~(SDL_HWSURFACE|SDL_FULLSCREEN);
}
Ejemplo n.º 5
0
void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
{
    if ( m_selected.Ok() )
    {
        UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
    }
    m_selected = bitmap;
    if (m_selected.Ok())
    {
        if ( m_selected.GetHBITMAP() )
        {
            m_macPort = (GrafPtr) m_selected.GetHBITMAP() ;
            LockPixels( GetGWorldPixMap(  (CGrafPtr)  m_macPort ) ) ;
            wxMask * mask = bitmap.GetMask() ;
            if ( mask )
            {
                m_macMask = mask->GetMaskBitmap() ;
            }
            SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ;
            CopyRgn( (RgnHandle) m_macBoundaryClipRgn ,(RgnHandle)  m_macCurrentClipRgn ) ;
            m_ok = TRUE ;
        }
        else
        {
            m_ok = FALSE;
        }
    }
    else
    {
        m_ok = FALSE;
    }
}
Ejemplo n.º 6
0
// left here for compatability
ImageHandle EncodeTheRect(PixMapHandle thePixMap,Rect *theRect,short clearCol,short shadowCol)
{
	EncodeRec		theEncode=emptyEncodeRec;
	BCRec			theRec;
	
	LockPixels(thePixMap);
	theEncode.theRec=&theRec;

	theRec.world=0L;
	theRec.pixMap=thePixMap;
	theRec.baseAddr=(unsigned char *)GetPixBaseAddr(thePixMap);
	theRec.rowBytes=(**thePixMap).rowBytes&0x3FFF;

	theEncode.theImage=0L;
	theEncode.theRect=theRect;
	theEncode.clearCol=clearCol;
	theEncode.shadowCol=shadowCol;
	theEncode.shrinkHandle=true;
	theEncode.shrinkRect=true;

	EckerEncodeTheRect(&theEncode);
	
	UnlockPixels(thePixMap);
	
	return theEncode.theImage;
}
Ejemplo n.º 7
0
TLevelWriter3gp::~TLevelWriter3gp()
{
#if 0
if (m_pixmap) 
  UnlockPixels(m_pixmap);
if (m_compressedData)
  DisposeHandle(m_compressedData);
if (m_gworld)
  DisposeGWorld(m_gworld);
#endif

	QDErr err;

	if (m_videoMedia)
		if ((err = EndMediaEdits(m_videoMedia)) != noErr) {
		} // throw TImageException(getFilePath(), "can't end edit media");

	if (m_videoTrack)
		if ((err = InsertMediaIntoTrack(m_videoTrack, 0, 0,
										GetMediaDuration(m_videoMedia), fixed1))) {
		} // throw TImageException(getFilePath(), "can't insert media into track");

	short resId = movieInDataForkResID;
	if (m_movie) {
		FSSpec fspec;
		long myFlags = 0L;
		OSErr myErr = noErr;
		//UCHAR myCancelled = FALSE;

		const char *pStr = toString(m_path.getWideString()).c_str();
		getFSSpecFromPosixPath(pStr, &fspec, true);

		myFlags = createMovieFileDeleteCurFile; // |
												//movieFileSpecValid | movieToFileOnlyExport;

		myErr = ConvertMovieToFile(
			m_movie,				// the movie to convert
			NULL,					// all tracks in the movie
			&fspec,					// the output file
			'3gpp',					// the output file type
			FOUR_CHAR_CODE('TVOD'), // the output file creator
			smSystemScript,			// the script
			&resId,					// no resource ID to be returned
			myFlags,				// export flags
			m_myExporter);			// no specific exp
	}

	DisposeHandle(m_hMovieData);
	DisposeHandle(m_dataRef);
	if (m_hSoundMovieData)
		DisposeHandle(m_hSoundMovieData);

	if (m_refNum)
		CloseMovieFile(m_refNum);
	DisposeMovie(m_movie);
}
Ejemplo n.º 8
0
	void UnlockGraphics(void)
	{
		if (m_locked_context == nil)
			return;

		MCGContextRelease(m_locked_context);
		m_locked_context = nil;
		
		UnlockPixels();
	}
Ejemplo n.º 9
0
void 
Tk_FreePixmap(
    Display *display,		/* Display. */
    Pixmap pixmap)     		/* Pixmap to destroy */
{
    MacDrawable *macPix = (MacDrawable *) pixmap;
    PixMapHandle pixels;

    display->request++;
    pixels = GetGWorldPixMap(macPix->portPtr);
    UnlockPixels(pixels);
    DisposeGWorld(macPix->portPtr);
    ckfree((char *) macPix);
}
Ejemplo n.º 10
0
static void QT_EndAddVideoSamplesToMedia (void)
{
	SCCompressSequenceEnd(qtdata->theComponent);

	UnlockPixels(qtexport->thePixMap);
	if (qtexport->theGWorld)
		DisposeGWorld (qtexport->theGWorld);

	if (qtexport->ibuf)
		IMB_freeImBuf(qtexport->ibuf);

	if (qtexport->ibuf2)
		IMB_freeImBuf(qtexport->ibuf2);
} 
Ejemplo n.º 11
0
// A handy routine which will return the colour of a pixel in a pixmap
// It's not effiecient enough to be called a lot as it works out some values every time even
// though they don't change.
short GPixelColour(PixMapHandle thePixMap,short x,short y)
{
	Ptr		pixBase;
	short	rowBytes,colour;

	LockPixels(thePixMap);
	
	pixBase=GetPixBaseAddr(thePixMap);
	rowBytes=(**thePixMap).rowBytes & 0x3fff;
	
	colour=(unsigned char)*(pixBase + y * rowBytes + x);
	
	UnlockPixels(thePixMap);
	
	return colour;
}
Ejemplo n.º 12
0
void mac_setfont(GWorldPtr world, Str255 fontname)
{
	GDHandle	oldGD;
	GWorldPtr	oldGW;
	
	GetGWorld(&oldGW, &oldGD);
	LockPixels(world->portPixMap);
	{
		short		fontID;
		SetGWorld( world, 0);
		GetFNum(fontname, &fontID);
		TextFont(fontID);
		TextSize(14);
		TextFace(extend/*|bold*/);
	}
	SetGWorld(oldGW, oldGD);
	UnlockPixels(world->portPixMap);
}
Ejemplo n.º 13
0
PRIVATE GWorldPtr
gworld_from_pict (PicHandle ph)
{
  GWorldPtr retval;

  retval = NULL;
  if (ph)
    {
      CGrafPtr save_port;
      GDHandle save_device;
      Rect r;
      OSErr err;

      GetGWorld (&save_port, &save_device);
      save_port = MR (save_port);
      save_device = MR (save_device);
      r = HxX (ph, picFrame);
      err = NewGWorld (&retval, 32, &r, NULL, NULL, keepLocal);
      if (retval)
	{
	  PixMapHandle pm;

	  retval = MR (retval);
	  SetGWorld (retval, NULL);
	  pm = GetGWorldPixMap (retval);
	  LockPixels (pm);
	  DrawPicture (ph, &r);
#if 0
#warning THIS INTERFERES WITH PICT PASTING
	  {
	    char *p;

	    EraseRect (&r);
	    p = GetPixBaseAddr (pm);
	    memset (p, 0x00, 4 * RECT_HEIGHT(&r) * RECT_WIDTH (&r));
	    memset (p, 0xFF, 4 * RECT_HEIGHT(&r) * RECT_WIDTH (&r) / 2);
	  }
#endif
	  UnlockPixels (pm);
	}
      SetGWorld (save_port, save_device);
    }
  return retval;
}
Ejemplo n.º 14
0
void dev_draw_text_gmode(PixMapHandle pixmap, int x, int y, const char* s, int len,
		int pmask, int mode, int fgcolor, int bgcolor, int ton_mode)
{			//pixmap must be already locked
	GDHandle	oldGD;
	GWorldPtr	oldGW;
	int		color, trans, width;
	Rect		rect= {0,0,16,32},
			destrect;
	
	GetGWorld(&oldGW, &oldGD);
	LockPixels(charbufWorld->portPixMap);
	SetGWorld(charbufWorld,0);
		trans=0;
		if( fgcolor==trans ) trans++;
		if( bgcolor==trans ) trans++;
		if( fgcolor==trans ) trans++;
		
		color= ( (mode&2)? bgcolor:trans );
		dev_box(charbufWorld->portPixMap, rect, color, 0xFF);
		
		color= ( (mode&1)? fgcolor:trans );
		charbufWorld->fgColor= color;
		TextMode(srcOr);
		MoveTo(0,13);
		DrawText(s,0,len);
		if( ton_mode==2 ){
			expand_horizontality(charbufWorld->portPixMap, len*8, 16);
		}
		
		width= len*8;
		if( ton_mode==2 ) width*=2;
		
		rect.right=width;
		destrect.left=x; destrect.top=y;
		destrect.right=x+width; destrect.bottom=destrect.top+16;
		
		MyCopyBits(charbufWorld->portPixMap, pixmap,
			rect, destrect, 0x11/*trans*/, trans, pmask,
			0, 0, NULL);
		
		
	SetGWorld(oldGW, oldGD);
	UnlockPixels(charbufWorld->portPixMap);
}
Ejemplo n.º 15
0
PRIVATE SDL_Surface *
surface_from_gworld (GWorldPtr gp)
{
  SDL_Surface *retval;

  if (!gp)
    retval = NULL;
  else
    {
      int pixels_per_line;
      int n_lines;
      PixMapHandle pm;
      enum { A = 0x00000000,
	     R = 0x0000FF00,
	     G = 0x00FF0000,
	     B = 0xFF000000 };
      mac_pixel32 *ip;
      sdl_pixel24 *op;
      Rect r;

      pm = GetGWorldPixMap (gp);
      LockPixels (pm);

      r = PIXMAP_BOUNDS (pm);
      n_lines = RECT_HEIGHT (&r);
      pixels_per_line = RECT_WIDTH (&r);
      retval = SDL_AllocSurface (SDL_SWSURFACE, pixels_per_line, n_lines, 32,
				 R, G, B, A);
      SDL_LockSurface (retval);
      op = SDL_Surface_pixels (retval);
      ip = (typeof (ip)) GetPixBaseAddr (pm);
      memcpy (op, ip, n_lines * pixels_per_line * sizeof *ip);
#if 0
#warning THIS IS BROKEN
      memset (op, 0x00, 4 * n_lines * pixels_per_line);
      memset (op, 0xFF, 4 * n_lines * pixels_per_line / 2);
#endif
      SDL_UnlockSurface (retval);
      UnlockPixels (pm);
    }

  return retval;
}
Ejemplo n.º 16
0
void _HYPlatformGraphicPane::_CopyToClipboard	(void)
{
	_HYGraphicPane* parent = (_HYGraphicPane*)this;
	#ifdef TARGET_API_MAC_CARBON
		ClearCurrentScrap();
	#else
		ZeroScrap();
	#endif
	Rect  bRect;
	
	bRect.left 			= bRect.top = 0;
	bRect.right 		= parent->w;
	bRect.bottom 		= parent->h;

	PicHandle	 pic 	= OpenPicture (&bRect);
	
	GrafPtr      topPort;
	GetPort		 (&topPort);
	
	LockPixels (GetGWorldPixMap(thePane));
	#ifdef OPAQUE_TOOLBOX_STRUCTS 
		CopyBits (GetPortBitMapForCopyBits(thePane),GetPortBitMapForCopyBits(topPort),
				&bRect,&bRect,srcCopy,(RgnHandle)nil);
	#else
		CopyBits ((BitMap*)*GetGWorldPixMap(thePane),
				  (BitMap*)&(topPort->portBits),&bRect,&bRect,
	    		   srcCopy,(RgnHandle)nil);
	#endif
	UnlockPixels (GetGWorldPixMap(thePane));
    			   
	ClosePicture ();
	HLock	((Handle)pic);		
						
	#ifdef TARGET_API_MAC_CARBON
		ScrapRef		 theScrapRef;
		GetCurrentScrap(&theScrapRef);
		PutScrapFlavor(theScrapRef, 'PICT', kScrapFlavorMaskNone,GetHandleSize((Handle)pic),*pic);
	#else
		PutScrap (GetHandleSize((Handle)pic),'PICT',*pic);
	#endif
	KillPicture (pic);	
}		
Ejemplo n.º 17
0
	bool LockGraphics(MCRegionRef p_area, MCGContextRef& r_context)
	{
		MCGRaster t_raster;
		if (LockPixels(p_area, t_raster))
		{
			if (MCGContextCreateWithRaster(t_raster, m_locked_context))
			{
				// Set origin
				MCGContextTranslateCTM(m_locked_context, -m_locked_area.x, -m_locked_area.y);
				// Set clipping rect
				MCGContextClipToRect(m_locked_context, MCRectangleToMCGRectangle(m_locked_area));
				
				r_context = m_locked_context;
				
				return true;
			}

			UnlockPixels();
		}

		return false;
	}
Ejemplo n.º 18
0
short FindGWUnusedCol(GWorldPtr theWorld)
{
	Ptr				pixBase;
	short			rowBytes,colour,colCount[255],xCount,yCount;
	short			width,height;
	PixMapHandle	thePixMap=GetGWorldPixMap(theWorld);

	LockPixels(thePixMap);
	
	pixBase=GetPixBaseAddr(thePixMap);
	rowBytes=(**thePixMap).rowBytes & 0x3fff;
	width=(theWorld->portRect.right)-(theWorld->portRect.left);
	height=(theWorld->portRect.bottom)-(theWorld->portRect.top);
		
	// clear all the counts
	for(xCount=0; xCount<=255; xCount++)
		colCount[xCount]=0;
	
	for (yCount=0; yCount<height; yCount++)
	{
		for(xCount=0; xCount<width; xCount++)
		{
			colour=(unsigned char)*(pixBase + yCount * rowBytes + xCount);
			colCount[colour]++;
		}
	}
	
	UnlockPixels(thePixMap);

	// scan through for a 0 use col
	for(xCount=0; xCount<=255; xCount++)
	{
		if (colCount[xCount]==0)
			return xCount;
	}

	return -1; // not found
}
Ejemplo n.º 19
0
RgnHandle getWindowStructureRegion(WindowRef window, RgnHandle structureRegion)
{
    /*------------------------------------------------------
        Define the structural region of our window.
    --------------------------------------------------------*/
    static RgnHandle pictureRgn=NULL;
    static Rect pictureRect;
    Rect windowRect;
    
    SetEmptyRgn(structureRegion);
    
    if(!pictureRgn){//haven't Cached our region yet
        PicHandle myPicture=GetPicture(kMaskPictureID);
        GrafPtr	origPort;
        GDHandle origDev;
        GWorldPtr pictMask;
        PixMapHandle maskBitMap;
        GetGWorld(&origPort,&origDev);
        pictureRgn=NewRgn();
        pictureRect=(*myPicture)->picFrame;
        NewGWorld(&pictMask,1,&pictureRect,NULL,NULL,0);
        maskBitMap=GetPortPixMap(pictMask);
        LockPixels(maskBitMap);
        SetGWorld(pictMask,NULL);
        EraseRect(&pictureRect);
        DrawPicture(myPicture,&pictureRect);
        BitMapToRegion(pictureRgn,(BitMap*)*maskBitMap);//use the mask to create a region
        InsetRgn(pictureRgn,1,1);
        SetGWorld(origPort,origDev);
        UnlockPixels(maskBitMap);
        DisposeGWorld(pictMask);
        ReleaseResource((Handle)myPicture);
    }
    getCurrentPortBounds(&windowRect);//how big is the window
    CopyRgn(pictureRgn,structureRegion);//make a copy of our cached region
    MapRgn(structureRegion,&pictureRect,&windowRect);//scale it to our actual window size
    return structureRegion;
}
Ejemplo n.º 20
0
osg::Image* QuicktimeImportExport::doImport(unsigned char* data, unsigned int sizeData, const std::string& fileTypeHint)
{
    GWorldPtr gworld = 0;
    OSType pixelFormat;
    int rowStride;
    GraphicsImportComponent gicomp = 0;
    Rect rectImage;
    GDHandle origDevice = 0;
    CGrafPtr origPort = 0;
    ImageDescriptionHandle desc = 0;
    int depth = 32;
    unsigned int xsize, ysize;
    unsigned char* imageData = 0;
    
    // Data Handle for file data ( & load data from file )
    Handle dataRef = getPtrDataRef(data, sizeData, fileTypeHint);
    
    try {
        OSErr err = noErr;
        
        // GraphicsImporter - Get Importer for our filetype
        GetGraphicsImporterForDataRef(dataRef, 'ptr ', &gicomp);

        // GWorld - Get Texture Info
        err = GraphicsImportGetNaturalBounds(gicomp, &rectImage);
        if (err != noErr) {
            throw QTImportExportException(err, "GraphicsImportGetNaturalBounds failed");
            
        }
        xsize = (unsigned int)(rectImage.right - rectImage.left);
        ysize = (unsigned int)(rectImage.bottom - rectImage.top);

        // ImageDescription - Get Image Description
        err = GraphicsImportGetImageDescription(gicomp, &desc);
        if (err != noErr) {
            throw QTImportExportException(err, "GraphicsImportGetImageDescription failed");
        }

        // ImageDescription - Get Bit Depth
        HLock(reinterpret_cast<char **>(desc));
        

        // GWorld - Pixel Format stuff
        pixelFormat = k32ARGBPixelFormat; // Make sure its forced...NOTE: i'm pretty sure this cannot be RGBA!

        // GWorld - Row stride
        rowStride = xsize * 4; // (width * depth_bpp / 8)

        // GWorld - Allocate output buffer
        imageData = new unsigned char[rowStride * ysize];

        // GWorld - Actually Create IT!
        QTNewGWorldFromPtr(&gworld, pixelFormat, &rectImage, 0, 0, 0, imageData, rowStride);
        if (!gworld) {
            throw QTImportExportException(-1, "QTNewGWorldFromPtr failed");
        }

        // Save old Graphics Device and Graphics Port to reset to later
        GetGWorld (&origPort, &origDevice);

        // GraphicsImporter - Set Destination GWorld (our buffer)
        err = GraphicsImportSetGWorld(gicomp, gworld, 0);
        if (err != noErr) {
            throw QTImportExportException(err, "GraphicsImportSetGWorld failed");
        }

        // GraphicsImporter - Set Quality Level
        err = GraphicsImportSetQuality(gicomp, codecLosslessQuality);
        if (err != noErr) {
            throw QTImportExportException(err, "GraphicsImportSetQuality failed");
        }

        // Lock pixels so that we can draw to our memory texture
        if (!GetGWorldPixMap(gworld) || !LockPixels(GetGWorldPixMap(gworld))) {
            throw QTImportExportException(0, "GetGWorldPixMap failed");
        }
        
           
        //*** Draw GWorld into our Memory Texture!
        GraphicsImportDraw(gicomp);
 
        // Clean up
        UnlockPixels(GetGWorldPixMap(gworld));
        SetGWorld(origPort, origDevice); // set graphics port to offscreen (we don't need it now)
        DisposeGWorld(gworld);
        CloseComponent(gicomp);
        DisposeHandle(reinterpret_cast<char **>(desc));
        DisposeHandle(dataRef);
    } 
    catch (QTImportExportException& e) 
    {
        setError(e.what());
        
        if (gworld) {
            UnlockPixels(GetGWorldPixMap(gworld));
            SetGWorld(origPort, origDevice); // set graphics port to offscreen (we don't need it now)
            DisposeGWorld(gworld);
        }
        if (gicomp)
            CloseComponent(gicomp);
        if (desc) 
            DisposeHandle(reinterpret_cast<char **>(desc));
        
        if (imageData) 
            delete[] imageData;
        if (dataRef)
            DisposeHandle(dataRef);
            
        return NULL;
    }
    

    
    unsigned int bytesPerPixel = depth / 8;
    unsigned int glpixelFormat;
    switch(bytesPerPixel) {
        case 3 :
            glpixelFormat = GL_RGB;
            break;
        case 4 :
            glpixelFormat = GL_RGBA;
            break;
        default :
            delete[] imageData;
            setError("unknown pixelformat");
            return NULL;
            break;
    }
    
    unsigned char* swizzled = pepareBufferForOSG(imageData, bytesPerPixel, xsize, ysize);
    
    delete[] imageData;
 
    osg::Image* image = new osg::Image();
    image->setFileName(fileTypeHint.c_str());
    image->setImage(xsize,ysize,1,
        bytesPerPixel,
        glpixelFormat,
        GL_UNSIGNED_BYTE,
        swizzled,
        osg::Image::USE_NEW_DELETE );
    
 
    return image;
}
Ejemplo n.º 21
0
int main2()
{
	mpeg3_t *file;
	int i, result = 0;
	unsigned char *output, **output_rows;
	float *audio_output_f;
	short *audio_output_i;
	long 			total_samples = 0;
	Rect 			sourceRect;
	OSErr           error;
	PixMapHandle 	hPixmap;
    Ptr       		gBaseLocation;
	long			targetRowBytes;
	
	file = mpeg3_open("randomAlien.mpg");
	if(file)
	{

		mpeg3_set_cpus(file, 1);
  		//audio_output_f = (float *) memoryAllocate(1,BUFSIZE * sizeof(float)); 
		//audio_output_i = (short *) memoryAllocate(1,BUFSIZE * sizeof(short));
 		//mpeg3_set_sample(file, 11229518, 0); 
        //result = mpeg3_read_audio(file, audio_output_f, 0, 0, BUFSIZE, 0); 
    	// result = mpeg3_read_audio(file, 0, audio_output_i, 1, BUFSIZE, 0);
 		// fwrite(audio_output_i, BUFSIZE, 1, stdout);

  		//mpeg3_set_frame(file, 1000, 0);
  		
		sourceRect.top = 0;
		sourceRect.left = 0;
		sourceRect.bottom = mpeg3_video_height(file, 0);
		sourceRect.right = mpeg3_video_width(file, 0);

		error = NewGWorld (&gpGWOffScreen, 32, &sourceRect, NULL, NULL,  keepLocal);
		if (error != noErr)
			{
		    DebugStr ("\pUnable to allocate off screen image");
		 	}
	 
		hPixmap = GetGWorldPixMap (gpGWOffScreen);
		error = LockPixels (hPixmap);
		gBaseLocation = GetPixBaseAddr(hPixmap);
		targetRowBytes = ((**hPixmap).rowBytes & 0x3FFF)/4;

  		output_rows = (unsigned char **) memoryAllocate(1,sizeof(unsigned char*) * mpeg3_video_height(file, 0));
  		for(i = 0; i < mpeg3_video_height(file, 0); i++)
  			output_rows[i] = (unsigned char*) gBaseLocation + i * targetRowBytes*4;

		for (i=0;i < mpeg3_video_frames(file, 0);i++) {
			result = mpeg3_read_frame(file, 
 					output_rows, 
 					0, 
 					0, 
 					mpeg3_video_width(file, 0), 
					mpeg3_video_height(file, 0), 
 					mpeg3_video_width(file, 0), 
 					mpeg3_video_height(file, 0), 
					MPEG3_RGBAF8888, 
 					0);
  	    	CopyBits (GetPortBitMapForCopyBits(gpGWOffScreen), GetPortBitMapForCopyBits(GetWindowPort(pWindow)), &sourceRect, &sourceRect, srcCopy, NULL); 
		}
		UnlockPixels (hPixmap);
		DisposeGWorld(gpGWOffScreen);
        memoryFree(output_rows);
        
		fprintf(stderr, "Audio streams: %d\n", mpeg3_total_astreams(file));
		for(i = 0; i < mpeg3_total_astreams(file); i++)
		{
			 fprintf(stderr, "  Stream %d: channels %d sample rate %d total samples %ld\n", 
				i, 
				mpeg3_audio_channels(file, i), 
				mpeg3_sample_rate(file, i),
				mpeg3_audio_samples(file, i));
		}
		fprintf(stderr, "Video streams: %d\n", mpeg3_total_vstreams(file));
		for(i = 0; i < mpeg3_total_vstreams(file); i++)
		{
			fprintf(stderr, "  Stream %d: width %d height %d frame rate %0.3f total frames %ld\n", 
				i, 
				mpeg3_video_width(file, i), 
				mpeg3_video_height(file, i), 
				mpeg3_frame_rate(file, i),
				mpeg3_video_frames(file, i));
		}
		
		mpeg3_close(file);
	}
/*
 *  PsychQTGetTextureFromMovie() -- Create an OpenGL texture map from a specific videoframe from given movie object.
 *
 *  win = Window pointer of onscreen window for which a OpenGL texture should be created.
 *  moviehandle = Handle to the movie object.
 *  checkForImage = true == Just check if new image available, false == really retrieve the image, blocking if necessary.
 *  timeindex = When not in playback mode, this allows specification of a requested frame by presentation time.
 *              If set to -1, or if in realtime playback mode, this parameter is ignored and the next video frame is returned.
 *  out_texture = Pointer to the Psychtoolbox texture-record where the new texture should be stored.
 *  presentation_timestamp = A ptr to a double variable, where the presentation timestamp of the returned frame should be stored.
 *
 *  Returns true (1) on success, false (0) if no new image available, -1 if no new image available and there won't be any in future.
 */
int PsychQTGetTextureFromMovie(PsychWindowRecordType *win, int moviehandle, int checkForImage, double timeindex, PsychWindowRecordType *out_texture, double *presentation_timestamp)
{
	static TimeValue myNextTimeCached = -2;
	static TimeValue nextFramesTimeCached = -2;
    TimeValue		myCurrTime;
    TimeValue		myNextTime;
    TimeValue       nextFramesTime=0;
    short		myFlags;
    OSType		myTypes[1];
    OSErr		error = noErr;
    Movie               theMovie;
    CVOpenGLTextureRef newImage = NULL;
    QTVisualContextRef  theMoviecontext;
    unsigned int failcount=0;
    float lowerLeft[2];
    float lowerRight[2];    
    float upperRight[2];    
    float upperLeft[2];
    GLuint texid;
    Rect rect;
    float rate;
    double targetdelta, realdelta, frames;
	PsychRectType outRect;

    if (!PsychIsOnscreenWindow(win)) {
        PsychErrorExitMsg(PsychError_user, "Need onscreen window ptr!!!");
    }
    
    // Activate OpenGL context of target window:
    PsychSetGLContext(win);

    // Explicitely disable Apple's Client storage extensions. For now they are not really useful to us.
    glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
    
    if (moviehandle < 0 || moviehandle >= PSYCH_MAX_MOVIES) {
        PsychErrorExitMsg(PsychError_user, "Invalid moviehandle provided.");
    }
    
    if ((timeindex!=-1) && (timeindex < 0 || timeindex >= 10000.0)) {
        PsychErrorExitMsg(PsychError_user, "Invalid timeindex provided.");
    }
    
    if (NULL == out_texture && !checkForImage) {
        PsychErrorExitMsg(PsychError_internal, "NULL-Ptr instead of out_texture ptr passed!!!");
    }
    
    // Fetch references to objects we need:
    theMovie = movieRecordBANK[moviehandle].theMovie;
    theMoviecontext = movieRecordBANK[moviehandle].QTMovieContext;

    if (theMovie == NULL) {
        PsychErrorExitMsg(PsychError_user, "Invalid moviehandle provided. No movie associated with this handle.");
    }

    // Check if end of movie is reached. Rewind, if so...
    if (IsMovieDone(theMovie) && movieRecordBANK[moviehandle].loopflag > 0) {
        if (GetMovieRate(theMovie)>0) {
            GoToBeginningOfMovie(theMovie);
        } else {
            GoToEndOfMovie(theMovie);
        }
    }
    
    // Is movie actively playing (automatic async playback, possibly with synced sound)?
    // If so, then we ignore the 'timeindex' parameter, because the automatic playback
    // process determines which frames should be delivered to PTB when. This function will
    // simply wait or poll for arrival/presence of a new frame that hasn't been fetched
    // in previous calls.
    if (0 == GetMovieRate(theMovie)) {
        // Movie playback inactive. We are in "manual" mode: No automatic async playback,
        // no synced audio output. The user just wants to manually fetch movie frames into
        // textures for manual playback in a standard Matlab-loop.

		// First pass - checking for new image?
		if (checkForImage) {
			// Image for specific point in time requested?
			if (timeindex >= 0) {
				// Yes. We try to retrieve the next possible image for requested timeindex.
				myCurrTime = (TimeValue) ((timeindex * (double) GetMovieTimeScale(theMovie)) + 0.5f);
			}
			else {
				// No. We just retrieve the next frame, given the current movie time.
				myCurrTime = GetMovieTime(theMovie, NULL);
			}
            
			// Retrieve timeindex of the closest image sample after myCurrTime:
			myFlags = nextTimeStep + nextTimeEdgeOK;	// We want the next frame in the movie's media.
			myTypes[0] = VisualMediaCharacteristic;		// We want video samples.
			GetMovieNextInterestingTime(theMovie, myFlags, 1, myTypes, myCurrTime, FloatToFixed(1), &myNextTime, &nextFramesTime);
			error = GetMoviesError();
			if (error != noErr) {
				PsychErrorExitMsg(PsychError_internal, "Failed to fetch texture from movie for given timeindex!");
			}
			
			// Found useful event?
			if (myNextTime == -1) {
				if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-WARNING: Bogus timevalue in movie track for movie %i. Trying to keep going.\n", moviehandle);
				
				// No. Just push timestamp to current time plus a little bit in the hope
				// this will get us unstuck:
				myNextTime = myCurrTime + (TimeValue) 1;
				nextFramesTime = (TimeValue) 0;
			}
			
			if (myNextTime != myNextTimeCached) {
				// Set movies current time to myNextTime, so the next frame will be fetched from there:
				SetMovieTimeValue(theMovie, myNextTime);
				
				// nextFramesTime is the timeindex to which we need to advance for retrieval of next frame: (see code below)
				nextFramesTime=myNextTime + nextFramesTime;
				
				if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Current timevalue in movie track for movie %i is %lf secs.\n", moviehandle, (double) myNextTime / (double) GetMovieTimeScale(theMovie));
				if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Next timevalue in movie track for movie %i is %lf secs.\n", moviehandle, (double) nextFramesTime / (double) GetMovieTimeScale(theMovie));
				
				// Cache values for 2nd pass:
				myNextTimeCached = myNextTime;
				nextFramesTimeCached = nextFramesTime;
			}
			else {
				// Somehow got stuck? Do nothing...
				if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Seem to be a bit stuck at timevalue [for movie %i] of %lf secs. Nudging a bit forward...\n", moviehandle, (double) myNextTime / (double) GetMovieTimeScale(theMovie));
				// Nudge the timeindex a bit forware in the hope that this helps:
				SetMovieTimeValue(theMovie, GetMovieTime(theMovie, NULL) + 1);
			}
		}
		else {
			// This is the 2nd pass: Image fetching. Use cached values from first pass:
			// Caching in a static works because we're always called immediately for 2nd
			// pass after successfull return from 1st pass, and we're not multi-threaded,
			// i.e., don't need to be reentrant or thread-safe here:
			myNextTime = myNextTimeCached;
			nextFramesTime = nextFramesTimeCached;
			myNextTimeCached = -2;
		}
	}
    else {
        // myNextTime unavailable if in autoplayback-mode:
        myNextTime=-1;
    }
    
    // Presentation timestamp requested?
    if (presentation_timestamp) {
        // Already available?
        if (myNextTime==-1) {
            // Retrieve the exact presentation timestamp of the retrieved frame (in movietime):
            myFlags = nextTimeStep + nextTimeEdgeOK;            // We want the next frame in the movie's media.
            myTypes[0] = VisualMediaCharacteristic;		// We want video samples.
                                                                // We search backward for the closest available image for the current time. Either we get the current time
                                                                // if we happen to fetch a frame exactly when it becomes ready, or we get a bit earlier timestamp, which is
                                                                // the optimal presentation timestamp for this frame:
            GetMovieNextInterestingTime(theMovie, myFlags, 1, myTypes, GetMovieTime(theMovie, NULL), FloatToFixed(-1), &myNextTime, NULL);
        }
        // Convert pts (in Quicktime ticks) to pts in seconds since start of movie and return it:
        *presentation_timestamp = (double) myNextTime / (double) GetMovieTimeScale(theMovie);
    }

    // Allow quicktime visual context task to do its internal bookkeeping and cleanup work:
    if (theMoviecontext) QTVisualContextTask(theMoviecontext);

    // Perform decompress-operation:
    if (checkForImage) MoviesTask(theMovie, 0);
    
    // Should we just check for new image? If so, just return availability status:
    if (checkForImage) {
        if (PSYCH_USE_QT_GWORLDS) {
            // We use GWorlds. In this case we either suceed immediately due to the
            // synchronous nature of GWorld rendering, or we fail completely at end
            // of non-looping movie:
            if (IsMovieDone(theMovie) && movieRecordBANK[moviehandle].loopflag == 0) {
                // No new frame available and there won't be any in the future, because this is a non-looping
                // movie that has reached its end.
                return(-1);
            }
            
            // Is this the special case of a movie without video, but only sound? In that case,
			// we always return a 'false' because there ain't no image to return.
			if (movieRecordBANK[moviehandle].QTMovieGWorld == NULL) return(false);
			
			// Success!
            return(true);
        }
        
        // Code which uses QTVisualContextTasks...
        if (QTVisualContextIsNewImageAvailable(theMoviecontext, NULL)) {
            // New frame ready!
            return(true);
        }
        else if (IsMovieDone(theMovie) && movieRecordBANK[moviehandle].loopflag == 0) {
            // No new frame available and there won't be any in the future, because this is a non-looping
            // movie that has reached its end.
            return(-1);
        }
        else {
            // No new frame available yet:
            return(false);
        }
    }
    
    if (!PSYCH_USE_QT_GWORLDS) {
        // Blocking wait-code for non-GWorld mode:
        // Try up to 1000 iterations for arrival of requested image data in wait-mode:
        failcount=0;
        while ((failcount < 1000) && !QTVisualContextIsNewImageAvailable(theMoviecontext, NULL)) {
            PsychWaitIntervalSeconds(0.005);
            MoviesTask(theMovie, 0);
            failcount++;
        }
        
        // No new frame available and there won't be any in the future, because this is a non-looping
        // movie that has reached its end.
        if ((failcount>=1000) && IsMovieDone(theMovie) && (movieRecordBANK[moviehandle].loopflag == 0)) {
            return(-1);
        }
        
        // Fetch new OpenGL texture with the new movie image frame:
        error = QTVisualContextCopyImageForTime(theMoviecontext, kCFAllocatorDefault, NULL, &newImage);
        if ((error!=noErr) || newImage == NULL) {
            PsychErrorExitMsg(PsychError_internal, "OpenGL<->Quicktime texture fetch failed!!!");
        }
    
        // Disable client storage, if it was enabled:
        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
        
        // Build a standard PTB texture record:    
        CVOpenGLTextureGetCleanTexCoords (newImage, lowerLeft, lowerRight, upperRight, upperLeft);
        texid = CVOpenGLTextureGetName(newImage);
        
        // Assign texture rectangle:
        PsychMakeRect(outRect, upperLeft[0], upperLeft[1], lowerRight[0], lowerRight[1]);    
        
        // Set texture orientation as if it were an inverted Offscreen window: Upside-down.
        out_texture->textureOrientation = (CVOpenGLTextureIsFlipped(newImage)) ? 3 : 4;

        // Assign OpenGL texture id:
        out_texture->textureNumber = texid;
        
        // Store special texture object as part of the PTB texture record:
        out_texture->targetSpecific.QuickTimeGLTexture = newImage;
    }
    else {
        // Synchronous texture fetch code for GWorld rendering mode:
        // At this point, the GWorld should contain the source image for creating a
        // standard OpenGL texture:
        
        // Disable client storage, if it was enabled:
        glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
        
        // Build a standard PTB texture record:    

        // Assign texture rectangle:
        GetMovieBox(theMovie, &rect);

        // Hack: Need to extend rect by 4 pixels, because GWorlds are 4 pixels-aligned via
        // image row padding:
        rect.right = rect.right + 4;
        PsychMakeRect(out_texture->rect, rect.left, rect.top, rect.right, rect.bottom);    
        
        // Set NULL - special texture object as part of the PTB texture record:
        out_texture->targetSpecific.QuickTimeGLTexture = NULL;

        // Set texture orientation as if it were an inverted Offscreen window: Upside-down.
        out_texture->textureOrientation = 3;
        
        // Setup a pointer to our GWorld as texture data pointer:
        out_texture->textureMemorySizeBytes = 0;

		// Quicktime textures are aligned on 4 Byte boundaries:
		out_texture->textureByteAligned = 4;

        // Lock GWorld:
        if(!LockPixels(GetGWorldPixMap(movieRecordBANK[moviehandle].QTMovieGWorld))) {
            // Locking surface failed! We abort.
            PsychErrorExitMsg(PsychError_internal, "PsychQTGetTextureFromMovie(): Locking GWorld pixmap surface failed!!!");
        }
        
        // This will retrieve an OpenGL compatible pointer to the GWorlds pixel data and assign it to our texmemptr:
        out_texture->textureMemory = (GLuint*) GetPixBaseAddr(GetGWorldPixMap(movieRecordBANK[moviehandle].QTMovieGWorld));
            
        // Let PsychCreateTexture() do the rest of the job of creating, setting up and
        // filling an OpenGL texture with GWorlds content:
        PsychCreateTexture(out_texture);
        
        // Undo hack from above after texture creation: Now we need the real width of the
        // texture for proper texture coordinate assignments in drawing code et al.
        rect.right = rect.right - 4;
        PsychMakeRect(outRect, rect.left, rect.top, rect.right, rect.bottom);    

        // Unlock GWorld surface. We do a glFinish() before, for safety reasons...
        //glFinish();
        UnlockPixels(GetGWorldPixMap(movieRecordBANK[moviehandle].QTMovieGWorld));

        // Ready to use the texture... We're done.
    }
    
	// Normalize texture rectangle and assign it:
	PsychNormalizeRect(outRect, out_texture->rect);
	
    rate = FixedToFloat(GetMovieRate(theMovie));
    
    // Detection of dropped frames: This is a heuristic. We'll see how well it works out...
    if (rate && presentation_timestamp) {
        // Try to check for dropped frames in playback mode:

        // Expected delta between successive presentation timestamps:
        targetdelta = 1.0f / (movieRecordBANK[moviehandle].fps * rate);

        // Compute real delta, given rate and playback direction:
        if (rate>0) {
            realdelta = *presentation_timestamp - movieRecordBANK[moviehandle].last_pts;
            if (realdelta<0) realdelta = 0;
        }
        else {
            realdelta = -1.0 * (*presentation_timestamp - movieRecordBANK[moviehandle].last_pts);
            if (realdelta<0) realdelta = 0;
        }
        
        frames = realdelta / targetdelta;
        // Dropped frames?
        if (frames > 1 && movieRecordBANK[moviehandle].last_pts>=0) {
            movieRecordBANK[moviehandle].nr_droppedframes += (int) (frames - 1 + 0.5);
        }

        movieRecordBANK[moviehandle].last_pts = *presentation_timestamp;
    }
    
    // Manually advance movie time, if in fetch mode:
    if (0 == GetMovieRate(theMovie)) {
        // We are in manual fetch mode: Need to manually advance movie time to next
        // media sample:
		if (nextFramesTime == myNextTime) {
			// Invalid value? Try to hack something that gets us unstuck:
			myNextTime = GetMovieTime(theMovie, NULL);
			nextFramesTime = myNextTime + (TimeValue) 1;
		}

        SetMovieTimeValue(theMovie, nextFramesTime);        
    }
    
    // Check if end of movie is reached. Rewind, if so...
    if (IsMovieDone(theMovie) && movieRecordBANK[moviehandle].loopflag > 0) {
        if (GetMovieRate(theMovie)>0) {
            GoToBeginningOfMovie(theMovie);
        } else {
            GoToEndOfMovie(theMovie);
        }
    }

    return(TRUE);
}
Ejemplo n.º 23
0
static OSErr
data_proc (SGChannel c, Ptr p, long len, long *offset, long chRefCon,
    TimeValue time, short writeType, long refCon)
{
  GstOSXVideoSrc *self;
  gint fps_n, fps_d;
  GstClockTime duration, timestamp, latency;
  CodecFlags flags;
  ComponentResult err;
  PixMapHandle hPixMap;
  Rect portRect;
  int pix_rowBytes;
  void *pix_ptr;
  int pix_height;
  int pix_size;

  self = GST_OSX_VIDEO_SRC (refCon);

  if (self->buffer != NULL) {
    gst_buffer_unref (self->buffer);
    self->buffer = NULL;
  }

  err = DecompressSequenceFrameS (self->dec_seq, p, len, 0, &flags, NULL);
  if (err != noErr) {
    GST_ERROR_OBJECT (self, "DecompressSequenceFrameS returned %d", (int) err);
    return err;
  }

  hPixMap = GetGWorldPixMap (self->world);
  LockPixels (hPixMap);
  GetPortBounds (self->world, &portRect);
  pix_rowBytes = (int) GetPixRowBytes (hPixMap);
  pix_ptr = GetPixBaseAddr (hPixMap);
  pix_height = (portRect.bottom - portRect.top);
  pix_size = pix_rowBytes * pix_height;

  GST_DEBUG_OBJECT (self, "num=%5d, height=%d, rowBytes=%d, size=%d",
      self->seq_num, pix_height, pix_rowBytes, pix_size);

  fps_n = FRAMERATE;
  fps_d = 1;

  duration = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
  latency = duration;

  timestamp = gst_clock_get_time (GST_ELEMENT_CAST (self)->clock);
  timestamp -= gst_element_get_base_time (GST_ELEMENT_CAST (self));
  if (timestamp > latency)
    timestamp -= latency;
  else
    timestamp = 0;

  self->buffer = gst_buffer_new_and_alloc (pix_size);
  GST_BUFFER_OFFSET (self->buffer) = self->seq_num;
  GST_BUFFER_TIMESTAMP (self->buffer) = timestamp;
  memcpy (GST_BUFFER_DATA (self->buffer), pix_ptr, pix_size);

  self->seq_num++;

  UnlockPixels (hPixMap);

  return noErr;
}
Ejemplo n.º 24
0
bool MCImageBitmapToPICT(MCImageBitmap *p_bitmap, MCMacSysPictHandle &r_pict)
{
#ifdef LIBGRAPHICS_BROKEN
	bool t_success = true;
	
	Pixmap drawdata = nil, drawmask = nil;
	MCBitmap *maskimagealpha = nil;
	
	t_success = MCImageSplitPixmaps(p_bitmap, drawdata, drawmask, maskimagealpha);
	
	if (!t_success)
		return false;

	Rect t_rect;
	SetRect(&t_rect, 0, 0, p_bitmap->width, p_bitmap->height);
	
	GWorldPtr t_old_gworld;
	GDHandle t_old_gdevice;
	GetGWorld(&t_old_gworld, &t_old_gdevice);

	PixMapHandle t_draw_pixmap;
	t_draw_pixmap = GetGWorldPixMap((CGrafPtr)drawdata -> handle . pixmap);
	
	GWorldPtr t_img_gworld;
	t_img_gworld = NULL;
	if (t_success)
	{
		QDErr t_err;
		t_err = NewGWorld(&t_img_gworld, 32, &t_rect, NULL, NULL, 0);
		if (t_err != noErr)
			t_success = false;
	}
	
	if (t_success)
	{
		SetGWorld(t_img_gworld, GetGDevice());
		
		PenMode(srcCopy);
		ForeColor(blackColor);
		BackColor(whiteColor);
		
		if (maskimagealpha != NULL)
		{
			GWorldPtr t_alpha_gworld;
			if (NewGWorldFromPtr(&t_alpha_gworld, 8, &t_rect, GetCTable(40), NULL, 0, maskimagealpha -> data, maskimagealpha -> bytes_per_line) == noErr)
			{
				const BitMap *t_dst_bits;
				t_dst_bits = GetPortBitMapForCopyBits(t_img_gworld);
				
				const BitMap *t_src_bits;
				t_src_bits = GetPortBitMapForCopyBits((CGrafPtr)drawdata -> handle . pixmap);
				
				const BitMap *t_mask_bits;
				t_mask_bits = GetPortBitMapForCopyBits(t_alpha_gworld);
				
				EraseRect(&t_rect);
				
				CopyDeepMask(t_src_bits, t_mask_bits, t_dst_bits, &t_rect, &t_rect, &t_rect, srcCopy, NULL);
			}
		}
		else if (drawmask != NULL)
		{
			PixMapHandle t_mask_pixmap;
			t_mask_pixmap = GetGWorldPixMap((CGrafPtr)drawmask -> handle . pixmap);
			
			EraseRect(&t_rect);
			
			const BitMap *t_dst_bits;
			t_dst_bits = GetPortBitMapForCopyBits(t_img_gworld);
			
			const BitMap *t_src_bits;
			LockPixels(t_draw_pixmap);
			t_src_bits = (BitMap *)*t_draw_pixmap;
			
			const BitMap *t_mask_bits;
			LockPixels(t_mask_pixmap);
			t_mask_bits = (BitMap *)*t_mask_pixmap;
			
			CopyMask(t_src_bits, t_mask_bits, t_dst_bits, &t_rect, &t_rect, &t_rect);
			
			UnlockPixels(t_mask_pixmap);
			
			UnlockPixels(t_draw_pixmap);
		}
		else
		{
			const BitMap *t_dst_bits;
			t_dst_bits = GetPortBitMapForCopyBits(t_img_gworld);
			
			const BitMap *t_src_bits;
			LockPixels(t_draw_pixmap);
			t_src_bits = (BitMap *)*t_draw_pixmap;
			CopyBits(t_src_bits, t_dst_bits, &t_rect, &t_rect, srcCopy, NULL);
			UnlockPixels(t_draw_pixmap);
		}
	}
	
	PicHandle t_handle;
	t_handle = NULL;
	if (t_success)
	{
		OpenCPicParams t_params;
		t_params . srcRect = t_rect;
		t_params . hRes = 72 << 16;
		t_params . vRes = 72 << 16;
		t_params . version = -2;
		t_params . reserved1 = 0;
		t_params . reserved2 = 0;
		t_handle = OpenCPicture(&t_params);
		if (t_handle == NULL)
			t_success = false;
	}

	if (t_success)
	{
		GWorldPtr t_pict_gworld;
		GDHandle t_pict_gdevice;
		GetGWorld(&t_pict_gworld, &t_pict_gdevice);
		
		PenMode(srcCopy);
		ForeColor(blackColor);
		BackColor(whiteColor);
		
		const BitMap *t_dst_bits;
		t_dst_bits = GetPortBitMapForCopyBits(t_pict_gworld);

		const BitMap *t_src_bits;
		t_src_bits = GetPortBitMapForCopyBits(t_img_gworld);
		CopyBits(t_src_bits, t_dst_bits, &t_rect, &t_rect, srcCopy, NULL);
		
		ClosePicture();
	}
	
	if (t_img_gworld != NULL)
		DisposeGWorld(t_img_gworld);
	
	SetGWorld(t_old_gworld, t_old_gdevice);

	MCscreen->freepixmap(drawdata);
	MCscreen->freepixmap(drawmask);
	if (maskimagealpha != nil)
		MCscreen->destroyimage(maskimagealpha);
	
	if (t_success)
		r_pict = (MCMacSysPictHandle)t_handle;
	
	return t_success;
#else
	return false;
#endif
}
Ejemplo n.º 25
0
PRIVATE GWorldPtr
gworld_from_surface (SDL_Surface *surfp)
{
  GWorldPtr retval;

  retval = NULL;

  if (surfp)
    {
      QDErr err;
      int surf_depth;
      CTabHandle ctab;

      ctab = NULL;

      surf_depth = SDL_Surface_depth (surfp);
      switch (surf_depth)
	{
	case 8:
	  ctab = ctab_from_surface (surfp);
	  break;
	case 32:
	  break;
	default:
	  warning_unexpected ("surf_depth = %d", surf_depth);
	  surf_depth = 0;
	  break;
	}

      if (surf_depth)
	{
	  int n_lines;
	  int pixels_per_line;
	  Rect r;

	  n_lines = SDL_Surface_height (surfp);
	  pixels_per_line = SDL_Surface_width (surfp);

	  r.top = CWC (0);
	  r.left = CWC (0);
	  r.bottom = CW (n_lines);
	  r.right = CW (pixels_per_line);
	  {
	    CGrafPtr save_port;
	    GDHandle save_device;

	    GetGWorld (&save_port, &save_device);
	    save_port = MR (save_port);
	    save_device = MR (save_device);
	    err = NewGWorld (&retval, surf_depth, &r, ctab, NULL, keepLocal);
	    SetGWorld (save_port, save_device);
	  }
	  if (retval)
	    {
	      PixMapHandle pm;

	      retval = MR (retval);
	      pm = GetGWorldPixMap (retval);
	      LockPixels (pm);
	      SDL_LockSurface (surfp);

	      switch (surf_depth)
		{
		case 8:
		  {
		    uint8 *ip, *eip;
		    uint8 *op;
		    int rowbytes;
		    int pitch;

		    pitch = SDL_Surface_pitch (surfp);
		    rowbytes = PIXMAP_ROWBYTES (pm);

		    ip = SDL_Surface_pixels (surfp);
		    op = (typeof (op)) GetPixBaseAddr (pm);
		    eip = ip + n_lines * pitch;
		    for (; ip != eip; ip += pitch, op += rowbytes)
		      memcpy (op, ip, rowbytes);
		    break;
		  }

		case 32:
		  {
		    sdl_pixel24 *ip;
		    mac_pixel32 *op;
		  
		    op = (typeof (op)) GetPixBaseAddr (pm);
		    ip = SDL_Surface_pixels (surfp);

		    memcpy (op, ip, n_lines * pixels_per_line * sizeof *op);
		    
		    break;
		  }
		default:
		  warning_unexpected ("surf_depth = %d", surf_depth);
		  break;
		}
	      SDL_UnlockSurface (surfp);
	      UnlockPixels (pm);
	    }
	}
    }
  return retval;
}
Ejemplo n.º 26
0
GLuint platformLoadTextureFile(string theFilename, bool generateMipmaps, int *width, int *height)
{
#ifdef QuickTimeInstalled
	GLuint					textureName;				// the "name" by which OpenGL knows the texture
	unsigned char 			*pImageBuffer;				// the buffer that contains the image data 
	GWorldPtr				pGWorld;					// a gworld to load the image into
	
	int						imageDepth;
	
	FSSpec 					fsspecImage;				// FSSpec of the image to load
#ifdef __APPLE__	
	CFURLRef				imageURL;
	FSRef					imagefsRef;
#endif
	
	OSStatus				err = noErr;				// err return value
	long					rowStride;					// length, in bytes, of a pixel row in the image
	GraphicsImportComponent giComp;						// component for importing image
	MatrixRecord			matrix;
	Rect 					rectImage;					// rectangle of source image
	PixMapHandle			hPixMap;					// handle to image pix map
	ImageDescriptionHandle	hImageDesc;					// handle to image description used to get image depth

	long					imageWidth;
	long					imageHeight;
	float					imageAspect;
	long					textureWidth;
	long					textureHeight;



	// get the full path to the texture file
	string fileLocation;
	fileLocation = pathToResourceDirectory() + theFilename;



#ifdef __APPLE__	
	// create a URL to the file from the C string
	imageURL = CFURLCreateWithBytes (kCFAllocatorDefault, (UInt8 *)fileLocation.c_str(), fileLocation.length(), kCFStringEncodingASCII, NULL);
	if (imageURL == NULL)
	{
		cout << "error getting URL for image file (image file may not exist): " << theFilename << endl;
		return 0;
	}


	// get a FSRef from the URL
	if (!CFURLGetFSRef(imageURL, &imagefsRef))
	{
		cout << "error getting FSRef for image file:: " << theFilename << endl;
		CFRelease(imageURL);
		return 0;
	}
	

	// get the FSSpec from the FSRef
	if (FSGetCatalogInfo (&imagefsRef, kFSCatInfoNone, NULL, NULL, &fsspecImage, NULL))
	{
		cout << "error getting FSSpec for image file: " << theFilename << endl;
		CFRelease(imageURL);
		return 0;
	}
	
	
	// release the URL (not needed any more)
	CFRelease(imageURL);
#endif

#ifdef _WIN32
	// get an FSSpec from the pathname
	if (NativePathNameToFSSpec ((char *)fileLocation.c_str(), &fsspecImage, kErrorIfFileNotFound))
	{
		cout << "error getting FSSpec for image file: " << fileLocation << endl;
		return 0;
	}
#endif


	// save the onscreen graphics port
	GDHandle	origDevice;
	CGrafPtr	origPort;
	GetGWorld (&origPort, &origDevice);


	// get an importer for the file
	err = GetGraphicsImporterForFileWithFlags (&fsspecImage, &giComp, kDontUseValidateToFindGraphicsImporter);
	if (err != noErr) return 0;

	
	// get the image bounds
	err = GraphicsImportGetNaturalBounds (giComp, &rectImage); 
	if (err != noErr) return 0;


	// create a handle for the image description and lock it
	hImageDesc = (ImageDescriptionHandle) NewHandle (sizeof (ImageDescriptionHandle)); 
	HLock ((Handle) hImageDesc);


	// retrieve the image description
	err = GraphicsImportGetImageDescription (giComp, &hImageDesc); 
	if (err != noErr) return 0;
	
	
	// find width and height
	imageWidth = (int) (rectImage.right - rectImage.left);
	imageHeight = (int) (rectImage.bottom - rectImage.top);

	
	// now calculate the aspect ratio (width/height), used to restore image correctly
	imageAspect = ((float) imageWidth) / ((float) imageHeight);

	
	// get the image's pixel depth
	imageDepth = (**hImageDesc).depth;

	
	// find nearest acceptable texture size (width and height) for the image
	textureWidth = GetTextureDimFromImageDim (imageWidth);
	textureHeight = GetTextureDimFromImageDim (imageHeight);
	
	
	// pass the optimised (and scaled) size back out to the caller
	*width = textureWidth;
	*height = textureHeight;


	// set texture rectangle for creation of GWorld
#ifdef __APPLE__
	SetRect (&rectImage, 0, 0, (int) textureWidth, (int) textureHeight);
#endif
#ifdef _WIN32
	MacSetRect (&rectImage, 0, 0, (int) textureWidth, (int) textureHeight);
#endif

	// set stride in bytes width of image * 4 bytes per pixel
	rowStride = textureWidth * 4;
	
	// build new buffer exact size of image (stride * height)
	pImageBuffer = (unsigned char *) NewPtr (rowStride * textureHeight); 


	// check we got the buffer we wanted
	if (pImageBuffer == NULL)
	{
		// failed - release the component and return an error
		CloseComponent(giComp);
		return 0;
	}

	
	// create a new gworld using our unpadded buffer, setting the pixel type correctly for the expected image depth
#ifdef __BIG_ENDIAN__
	QTNewGWorldFromPtr (&(pGWorld), k32ARGBPixelFormat, &rectImage, NULL, NULL, 0, pImageBuffer, rowStride);
#else
	QTNewGWorldFromPtr (&(pGWorld), k32RGBAPixelFormat, &rectImage, NULL, NULL, 0, pImageBuffer, rowStride);
#endif
	
	
	// could we allocate the GWorld?
	if (pGWorld == NULL)
	{
		// failed - release the buffer, component and return an error
		DisposePtr ((Ptr) pImageBuffer);
		CloseComponent(giComp);
		return 0;
	}


	// build a transformation matrix to scale the image to the texture size
	// this also flips the image horizontally, so that texture coordinates map correctly
	float	horizontalScale = (float) textureWidth / (float) imageWidth;
	float	verticalScale = (float) textureHeight / (float) imageHeight;

	SetIdentityMatrix (&matrix);
	ScaleMatrix (&matrix, X2Fix(horizontalScale), X2Fix(-verticalScale), 0, 0);
	TranslateMatrix (&matrix, 0, X2Fix((float)textureHeight));


	// set the matrix as the importer matrix
	err = GraphicsImportSetMatrix(giComp, &matrix); 

	// set the destination of the importer component
	if (err == noErr) err = GraphicsImportSetGWorld (giComp, pGWorld, NULL);
	
	// ensure lossless decompression (if the CODEC supports this)
	if (err == noErr) err = GraphicsImportSetQuality(giComp, codecLosslessQuality); 

	if (err != noErr)
	{
		// failed - release the GWorld, buffer, component and return an error
		DisposeGWorld (pGWorld);
		DisposePtr ((Ptr) pImageBuffer);
		CloseComponent(giComp);
		return 0;
	}

	// get the address of the GWorld's pixmap
	hPixMap = GetGWorldPixMap (pGWorld);
	
	// if everything looks good draw the image to the locked pixmap
	if ((hPixMap) && (LockPixels (hPixMap)))
		GraphicsImportDraw (giComp);
	else
	{
		// the pixmap doesn't exist, or we couldn't lock it
		// release the GWorld, buffer, component and return an error
		DisposeGWorld (pGWorld);
		DisposePtr ((Ptr) pImageBuffer);
		CloseComponent(giComp);
		return 0;
	}


	// for images without an alpha channel, initialise the alpha bytes since QuickTime won't
	if (imageDepth < 32)
	{
#ifdef __BIG_ENDIAN__
		for( unsigned char *p = pImageBuffer; p < pImageBuffer + (rowStride * textureHeight); p+=4)
			*p = 0xFF;
#else
		for( unsigned char *p = pImageBuffer+3; p < pImageBuffer + (rowStride * textureHeight) +3; p+=4)
			*p = 0xFF;
#endif
	}

	// unlock the pixmap
	UnlockPixels (hPixMap);
	
	
	// dump the component
	CloseComponent(giComp);

	
	// set image width in groups (pixels), accounts for border this ensures proper image alignment row to row
	glPixelStorei (GL_UNPACK_ROW_LENGTH, textureWidth);


	// generate a "name" for the texture
	glGenTextures (1, &textureName);
	
	
	// create the texture in OpenGL
	glBindTexture(GL_TEXTURE_2D, textureName);



	// tell OpenGL about the texture and have GLU build mipmaps
#ifdef __BIG_ENDIAN__
	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pImageBuffer);
#else
	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImageBuffer);
#endif
	if (generateMipmaps)
#ifdef __BIG_ENDIAN__
	if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, textureWidth, textureHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pImageBuffer) != 0)
#else
	if (gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE, pImageBuffer) != 0)
#endif
	
	
	// good time to check for errors?
	openGLCheckError();
	

	// finished with the GWorld but not the image buffer (it's used by OpenGL as the "live" image buffer)
	DisposeGWorld (pGWorld);

	// restore the current graphics port we saved earlier
	SetGWorld(origPort, origDevice);


	// all done - return the texture name
	return textureName; 
#endif // QuickTimeInstalled
}
Ejemplo n.º 27
0
TQ3TextureObject
QutTexture_CreateTextureObjectFromPixmap(PixMapHandle		thePixMap,
									TQ3PixelType	pixelType,
									TQ3Boolean		wantMipMaps)
{	TQ3Uns32				x, y, theWidth, theHeight, rowBytes, pixelBytes;
	TQ3TextureObject		qd3dTextureObject = NULL;
	TQ3StorageObject		qd3dMemoryStorage;
	TQ3StoragePixmap		qd3dPixMap;
	TQ3Mipmap				qd3dMipMap;
	UInt16					*pixelPtr;
	UInt8					*baseAddr;
	OSType					pixelFormat;
	TQ3Endian				byteOrder;


	// Get the details we need from the PixMap
	NoPurgePixels(thePixMap);
	LockPixels(thePixMap);

	theWidth   = (*thePixMap)->bounds.right  - (*thePixMap)->bounds.left;
	theHeight  = (*thePixMap)->bounds.bottom - (*thePixMap)->bounds.top;
	rowBytes   = (*thePixMap)->rowBytes & 0x7FFF;
	pixelBytes = (*thePixMap)->pixelSize / 8;
	baseAddr   = (UInt8 *) GetPixBaseAddr(thePixMap);
#if OLDPIXMAPSTRUCT
	pixelFormat = 0;
#else
	pixelFormat = (*thePixMap)->pixelFormat;
#endif


	// If this is a 16 bit alpha channel texture, set the alpha bits.
	// We assume that black is transparent.
	if (pixelType == kQ3PixelTypeARGB16)
		{
		for (y = 0; y < theHeight; y++)
			{
			for (x = 0; x < theWidth; x++)
				{
				pixelPtr = (UInt16 *) (baseAddr + (y * rowBytes) + (x * 2));
				if (*pixelPtr != 0x0000)
					*pixelPtr |= (1 << 15);
				}
			}
		}


	// Set the byte order
	if ( (pixelFormat == k32BGRAPixelFormat) ||
		(pixelFormat == k16LE555PixelFormat) )
	{
		byteOrder = kQ3EndianLittle;
	}
	else
	{
		byteOrder = kQ3EndianBig;
	}


	// Create a storage object based on the GWorld
	qd3dMemoryStorage = Q3MemoryStorage_New(baseAddr, theHeight * rowBytes);
	if (qd3dMemoryStorage != NULL)
		{
		// Create the appropriate type of texture. Note that if mip-maps are
		// required, we create a QD3D PixMap. This might seem back to front,
		// but QD3D automatically creates mip-maps for PixMaps.
		//
		// If mip-maps are not required, we create a QD3D MipMap by hand and
		// signal that we don't want any other mip-maps to be created.
		//
		// We need to work around a bug in Q3MipmapTexture_New: if the rowByte
		// for the pixel exactly matches the size of each row, the texture is
		// distorted - we can fix this by using Q3PixmapTexture_New instead. 
		if (wantMipMaps || (rowBytes == theWidth * pixelBytes))
			{
			// Create a PixMap from the GWorld data
			qd3dPixMap.image        = qd3dMemoryStorage;
			qd3dPixMap.width		= theWidth;
			qd3dPixMap.height		= theHeight;
			qd3dPixMap.rowBytes		= rowBytes;
			qd3dPixMap.pixelSize	= (pixelType == kQ3PixelTypeARGB32 || pixelType == kQ3PixelTypeRGB32) ? 32 : 16;
			qd3dPixMap.pixelType	= pixelType;
			qd3dPixMap.bitOrder		= byteOrder;
			qd3dPixMap.byteOrder	= byteOrder;

			qd3dTextureObject = Q3PixmapTexture_New(&qd3dPixMap);
			}
	
		else
			{
			// Create a MipMap from the GWorld data
			qd3dMipMap.image         = qd3dMemoryStorage;
			qd3dMipMap.useMipmapping = kQ3False;
			qd3dMipMap.pixelType	 = pixelType;
			qd3dMipMap.bitOrder		 = byteOrder;
			qd3dMipMap.byteOrder	 = byteOrder;
			qd3dMipMap.reserved		 = 0;

			qd3dMipMap.mipmaps[0].width    = theWidth;
			qd3dMipMap.mipmaps[0].height   = theHeight;
			qd3dMipMap.mipmaps[0].rowBytes = rowBytes;
			qd3dMipMap.mipmaps[0].offset   = 0;
		
			qd3dTextureObject = Q3MipmapTexture_New(&qd3dMipMap);
			}
		Q3Object_Dispose(qd3dMemoryStorage);
		
		}



	// Clean up and return
	UnlockPixels(thePixMap);
	return(qd3dTextureObject);
}
Ejemplo n.º 28
0
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {

  if (nlhs < 0) { mexErrMsgTxt("Too few output arguments."); return; }
  if (nlhs > 1) { mexErrMsgTxt("Too many output arguments."); return; }
  if (nrhs < 1) { mexErrMsgTxt("Too few input arguments."); return; }
  if (nrhs > 3) { mexErrMsgTxt("Too many input arguments."); return; }

  TimeValue duration;
  TimeRecord myTimeRecord;
  Rect bounds;
  OSErr result = 0;
  short resRefNum = -1;
  short actualResId = DoTheRightThing;
  FSSpec theFSSpec;
  GWorldPtr offWorld;
  Movie theMovie = nil;
  MovieController thePlayer = nil;
  MovieDrawingCompleteUPP myDrawCompleteProc;
  long frame_end;
  long myStep = 1;
  char location[PATH_BUFFER_SIZE];
  long frame_count;
  mwSize cdims[2];

  mxGetString(prhs[0], location, PATH_BUFFER_SIZE);

  if (nrhs > 2) {
    frame_start = rint(mxGetScalar(prhs[1]));
    frame_end = rint(mxGetScalar(prhs[2]));
  } else if (nrhs > 1) {
    frame_start = 1;
    frame_end = rint(mxGetScalar(prhs[1]));
  } else {
    frame_start = 1;
    frame_end = 0;
  }

  if (frame_start < 1) {
    mexErrMsgTxt("Error: the starting frame must be positive\n"); 
    return; 
  }

  if (frame_end < 0) {
    mexErrMsgTxt("Error: the ending frame must be positive\n"); 
    return; 
  }

  if (frame_end != 0 && frame_end < frame_start) {
    mexErrMsgTxt("Error: the ending frame must not be less than the starting frame\n"); 
    return; 
  }

  myDrawCompleteProc = NewMovieDrawingCompleteUPP(DrawCompleteProc);

  EnterMovies();            

  if (NativePathNameToFSSpec(location, &theFSSpec, 0) ||
      OpenMovieFile(&theFSSpec, &resRefNum, 0) ||
      NewMovieFromFile(&theMovie, resRefNum, &actualResId, 0, 0, 0)) {
    mexErrMsgTxt("Error: failed to open movie\n"); 
    return; 
  }

  if (resRefNum != -1) CloseMovieFile(resRefNum);

  GetMovieBox(theMovie, &bounds);
  QTNewGWorld(&offWorld, k32ARGBPixelFormat, &bounds, NULL, NULL, 0);
  LockPixels(GetGWorldPixMap(offWorld));
  SetGWorld(offWorld, NULL);

  thePlayer = NewMovieController(theMovie, &bounds, mcTopLeftMovie | mcNotVisible);
  SetMovieGWorld(theMovie, offWorld, NULL);
  SetMovieActive(theMovie, true);
  SetMovieDrawingCompleteProc(theMovie, movieDrawingCallWhenChanged, myDrawCompleteProc, (long) offWorld);
  GetMovieTime(theMovie, &myTimeRecord);
  duration = GetMovieDuration(theMovie);

  // Compute the number of frames for allocation of output structure
  frame_count = 0;
  while ((frame_end == 0 || frame_count < frame_end) && GetMovieTime(theMovie, NULL) < duration) {
    frame_count++;
    MCDoAction(thePlayer, mcActionStep, (Ptr) myStep);
  }
  SetMovieTime(theMovie, &myTimeRecord);
  
  // Ignore frames greater than those in the file
  if (frame_end == 0 || frame_count < frame_end) frame_end = frame_count;
  
  cdims[0] = frame_end - frame_start + 1; // Indices are one-based
  cdims[1] = 1; 

  plhs[0] = mxCreateCellArray(2, cdims);

  // Step through the movie and save the frame when in the chosen interval
  // Note: the step size seems to be handled as a short internally. 
  //       Using anything greater than 32758 will seek to an incorrect frame
  frame_num = 1;
  while (frame_num <= frame_end) {
    MCDoAction(thePlayer, mcActionStep, (Ptr) myStep);
    if (frame_num >= frame_start) {
      MCIdle(thePlayer);
      mxSetCell(plhs[0], frame_num - frame_start, framedata);
    }
    frame_num++;
  }

  UnlockPixels(GetGWorldPixMap (offWorld));
  DisposeGWorld(offWorld);
  DisposeMovieController (thePlayer);
  DisposeMovie(theMovie);
  DisposeMovieDrawingCompleteUPP(myDrawCompleteProc);
  ExitMovies();

  return;
}
Ejemplo n.º 29
0
//-----------------------------------------------------------------------------
//		QutTexture_CreateGWorldFromPICT : Create a GWorld from a PICT.
//-----------------------------------------------------------------------------
GWorldPtr
QutTexture_CreateGWorldFromPICT(PicHandle thePicture, TQ3PixelType pixelType)
{	GWorldPtr			deepGWorld, theGWorld;
 	Boolean				shouldDither;
 	GDHandle			saveDevice;
	CGrafPtr			savePort;
	TQ3Uns32			theDepth;
	OSErr				theErr;



	// Work out the depth of GWorld, and if it should be dithered
	if (pixelType == kQ3PixelTypeARGB16 || pixelType == kQ3PixelTypeRGB16)
		theDepth = 16;
	else
		theDepth = 32;

	shouldDither = (theDepth == 16);



	// Create a GWorld to hold the image data
	theErr = NewGWorld(&theGWorld, theDepth, &(*thePicture)->picFrame, NULL, NULL,
		useTempMem | kQ3NativeEndianPixMap );
	if (theErr != noErr || theGWorld == NULL)
		return(NULL);



	// If we should dither, create a deep GWorld
	if (theDepth == 16 && shouldDither)
		theErr = NewGWorld(&deepGWorld, 32, &(*thePicture)->picFrame, NULL, NULL,
			useTempMem | kQ3NativeEndianPixMap );
	else
		deepGWorld = NULL;
		


	// If we have a deep GWorld, draw the image into that and then
	// copy it into the output GWorld. If we don't have a deep GWorld,
	// just draw it directly into the output GWorld.
	if (deepGWorld != NULL)
		{
		// Draw the picture into the deep GWorld
		GetGWorld(&savePort, &saveDevice);
		SetGWorld(deepGWorld, NULL);
		LockPixels(GetGWorldPixMap(deepGWorld));

		DrawPicture(thePicture, &(*thePicture)->picFrame);

		UnlockPixels(GetGWorldPixMap(deepGWorld));
		SetGWorld(savePort, saveDevice);



		// Copy this into the output GWorld
		GetGWorld(&savePort, &saveDevice);
		SetGWorld(theGWorld, NULL);
		LockPixels(GetGWorldPixMap(theGWorld));

		CopyBits(GetPortBitMapForCopyBits(deepGWorld),
				 GetPortBitMapForCopyBits(theGWorld),
				 &(*thePicture)->picFrame,
				 &(*thePicture)->picFrame,
				 ditherCopy, NULL);

		UnlockPixels(GetGWorldPixMap(theGWorld));
		SetGWorld(savePort, saveDevice);


		// Clean up
		DisposeGWorld(deepGWorld);
		deepGWorld = NULL;
		}
	else
		{
		// Draw the picture into the output GWorld
		GetGWorld(&savePort, &saveDevice);
		SetGWorld(theGWorld, NULL);
		LockPixels(GetGWorldPixMap(theGWorld));

		DrawPicture(thePicture, &(*thePicture)->picFrame);

		UnlockPixels(GetGWorldPixMap(theGWorld));
		SetGWorld(savePort, saveDevice);
		}
		
	return(theGWorld);
}
Ejemplo n.º 30
0
short LoadMapPICT(
	PicHandle 			pict,
	unsigned long 		mapID,
	unsigned long 		mapSizeX,
	unsigned long 		mapSizeY,
	TQ3StoragePixmap 	*bMap)
{
	unsigned long 			*textureMap;
	unsigned long			*textureMapAddr;
	unsigned long 			*pictMap;
	unsigned long			pictMapAddr;
	register unsigned long 	row;
	register unsigned long 	col;
	Rect 					rectGW;
	GWorldPtr 				pGWorld;
	PixMapHandle 			hPixMap;
	unsigned long 			pictRowBytes;
	QDErr					err;
	GDHandle				oldGD;
	GWorldPtr				oldGW;
	short					success;
	
	mapID;		/* unused argument */
	
	/* save current port */
	GetGWorld(&oldGW, &oldGD);

	/* create the GWorld */
	SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);

	err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
	if (err != noErr)
		return 0;

	success = 1;
	
	hPixMap = GetGWorldPixMap(pGWorld);
	pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
	pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
	
	/* put the PICT into the window */
	SetGWorld(pGWorld, nil);
	
	LockPixels(hPixMap);
	EraseRect(&rectGW);
	DrawPicture(pict, &rectGW);
		
	/* allocate an area of memory for the texture */
	textureMap = (unsigned long *)malloc(mapSizeX * mapSizeY * sizeof(unsigned long));
	if (textureMap == NULL) {
		success = 0;
		goto bail;
	}
	/* bMap->image = (char *)textureMap; */

	/* copy the PICT into the texture */
	textureMapAddr = textureMap;
	for (row = 0L; row < mapSizeY; row++) {
		pictMap = (unsigned long *)(pictMapAddr + (pictRowBytes * row));
		for (col = 0L; col < mapSizeX; col++) {
			*textureMap++ = (*pictMap++ | 0xff000000L);
		}
	}
		
	bMap->image = Q3MemoryStorage_New((const unsigned char *)textureMapAddr, 
								  mapSizeX * mapSizeY * sizeof(unsigned long));
								  
	if (bMap->image == NULL) {
		/* error */
		success = 0;
		goto bail;
	}

	UnlockPixels(hPixMap);
	
	bMap->width 	= mapSizeX;
	bMap->height	= mapSizeY;
	bMap->rowBytes 	= bMap->width * 4;
	bMap->pixelSize = 32;
	bMap->pixelType	= kQ3PixelTypeRGB32;
	bMap->bitOrder	= kQ3EndianBig;
	bMap->byteOrder	= kQ3EndianBig;
	
	/* Free junk */
bail:

	SetGWorld(oldGW, oldGD);
	
	DisposeGWorld(pGWorld);
	if (textureMapAddr != NULL)
		free(textureMapAddr);
	
	return success;
}