PtSetResource

Hello,

how can i set the Resource for a Widget (Especially ImageArea)?
when i try to set the Resource with the third parameter that is not a pointer, then i get the Memory fault error.

int callback2(PtWidget_t* w, void* data, PtCallbackInfo_t* cbInfo)
{
	std::cout << "callback2" << std::endl;
	CircularBuffer* circB = (CircularBuffer*) data;

	PhImage_t img;
	memset(&img,0,sizeof(PhImage_t));

	img.type = Pg_IMAGE_DIRECT_8888;
	img.size.h = 240;
	img.size.w = 640;
	img.alpha = 0;
	img.bpl = 5120;
	img.colors = 0;
	std::cout << "getBuf" << std::endl;
	img.image = circB->getBuf();
	img.flags |= Ph_RELEASE_IMAGE_ALL;
		
	PtSetResource(w, Pt_ARG_IMAGEAREA_IMAGE, &img, 0);

	return Pt_CONTINUE;
}

but even when i create a PhImage_t Pointer and load it with an existing bmp file,

...
PhImage_t* img = PxLoadImage("/home/write.bmp", NULL);
img->flags |= Ph_RELEASE_IMAGE_ALL;
PtSetResource(w, Pt_ARG_IMAGEAREA_IMAGE, img,0);
...

then it works.
But when i dont load an image and want to change the data, i cant. (like img->size.w = 640;).
then i get the Memory fault error.


int callback2(PtWidget_t* w, void* data, PtCallbackInfo_t* cbInfo)
{
	std::cout << "callback2" << std::endl;
	CircularBuffer* circB = (CircularBuffer*) data;

	PhImage_t* img;
	memset(img,0,sizeof(PhImage_t));

	img->type = Pg_IMAGE_DIRECT_8888;
	img->size.h = 240;
	img->size.w = 640;
	img->alpha = 0;
	img->bpl = 5120;
	img->colors = 0;
	std::cout << "getBuf" << std::endl;
	img->image = circB->getBuf();
	img->flags |= Ph_RELEASE_IMAGE_ALL;

	PtSetResource(w, Pt_ARG_IMAGEAREA_IMAGE, img, 0);

	return Pt_CONTINUE;
}

now, how can i set the resource without to savind the pic to a bitmap file and then loading it?

i hope somebody can help me :slight_smile:

with best regards

nudels

You are leaving out a lot of information here. What kind of widget are you setting the IMAGE AREA to?

What does circB->getBuf() return? It better be a memory area that’s 5120x240 bytes.

Hey :slight_smile:

sry for that :slight_smile:

my Widget is a PtImageArea.
and getBuf() returns a char pointer.


char* CircularBuffer::getBuf()
{
	// Prüfen ob schon ein Bild abgelegt wurde
	if(_anz>0)
	{
		// Wenn ja, dann Prüfen ob der Zeiger schon über dem Buffer
		// rausgelaufen ist
		if(_posGet < MAXBUFFER)
		{
			// Wenn nein, dann lade das Bild und gib es Zurück
			_pos = _posGet;
			_anz--;
			_posGet++;
			_ptr = _ringBuffer[_pos].buff;
			return (char*)_ptr;
		}
		else
		{
			// Wenn ja, dann setze ihn wieder auf dem Anfang des Buffers,
			// lade das Bild und gib dieses Zurück
			_posGet = 0;
			_pos = _posGet;
			_anz--;
			_posGet++;
			_ptr = _ringBuffer[_pos].buff;
			return (char*)_ptr;
		}
	}
	else {
		return 0;
	}
}

with best regards

nudels

Yes, well it’s a char pointer, but to what? How many bytes? If it’s not large enough, it explains your problem.

its a pointer to my imagedata.

when i add a callback to my imageArea and init a background worker in this callback, like this:

/**
 * Callbackfunction  
*/
int activate(PtWidget_t*, void* data ,PtCallbackInfo_t*)
{
	// Erstellen eines Background Workers
	PtWorkProcId_t* id;
	// Definition der Background-Funktion
	PtWorkProcF_t my_work_func;
	// Die Backgroundfunktion hinzufügen und data übergeben
	id = PtAppAddWorkProc(NULL, my_work_func, data);

	return Pt_CONTINUE;
}

// Backgroundworker
int my_work_func(void* data)
{

	daten* _dat = (daten*) data;
	PtArg_t arg[6];
	int n = 0;
	// Lese aus dem CircularBuffer und schreibe das Bild in das Image
	if(NULL != (_dat->_img->image = _dat->_c->getBuf()))
	{
		// Erstelle ein neues ImageArea
		PtSetArg( &arg[n++], Pt_ARG_DIM, &_dat->_img->size, 0 );
		PtSetArg( &arg[n++], Pt_ARG_IMAGEAREA_IMAGE, _dat->_img, 0);

		PtWidget_t* area = PtCreateWidget( PtImageArea, _dat->_w, n, arg );
		if(PtRealizeWidget(area) == -1)
		{
			PtExit(EXIT_FAILURE);
		}
	}

	return Pt_CONTINUE;
}

and daten is a struct.

struct daten{
	PtWidget_t* _widget;
	CircularBuffer* _circBuf;
	PhImage_t* _img;
};

then it works, but in this code i allways create a new widget, and i think this is a problem, that i have sometimes memory fault / Overrun.

but when i try this code:

//Callback
int activate(PtWidget_t*, void* data ,PtCallbackInfo_t*)
{
	// Erstellen eines Background Workers
	PtWorkProcId_t* id;
	// Definition der Background-Funktion
	PtWorkProcF_t my_work_func;
	// Die Backgroundfunktion hinzufügen und data übergeben
	id = PtAppAddWorkProc(NULL, my_work_func, data);

	return Pt_CONTINUE;
}


//Baclgroundworker
int my_work_func(void* data)
{

	daten* _dat = (daten*) data;
  // Lese aus dem CircularBuffer und schreibe das Bild in das Image
	if(NULL != (_dat->_img->image = _dat->_c->getBuf()))
	{
		// Setze das Bild als Resource für das ImageArea
		PtSetResource(_dat->_w, Pt_ARG_IMAGEAREA_IMAGE, _dat->_img, 0);
	}

	return Pt_CONTINUE;
}

then it doesn’t work :frowning: i only get the first image and then i get memory fault error :confused:

with best regards

nudels

Well I’ve looked over all the code you’ve posted and I can’t see where you allocate the buffer.
I’ve mentioned twice the possibility that it is the incorrect allocation of size of this buffer that
could be the problem. I don’t know what else to say.