ObjectARX
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Saving cloned image to disk

1 REPLY 1
Reply
Message 1 of 2
Anonymous
383 Views, 1 Reply

Saving cloned image to disk

I'm trying to copy some pixel from one image to a new one. the code executes fine but I can't seem to save it to disk, it gives me an AccessViolationException and autocad carshes.

here is my code :

 

int Functions::Crop(void* img, int rectangle[])
	{
		//read image
		Atil::Image* image = (Atil::Image*)img;

		//create empty image to hold the pixels
		Atil::ImagePixel initialImage;
		initialImage.setToZero();
		initialImage.type = image->dataModel().pixelType();//Atil::DataModelAttributes::kRgba;
		initialImage.value.rgba = 0xff000000;
		
		Atil::Size size(rectangle[2], rectangle[3]);
		/*const Atil::RgbModel *pDm = new Atil::RgbModel(
			Atil::RgbModelAttributes::k4Channels, Atil::DataModelAttributes::kBlueGreenRedAlpha);*/

		Atil::Image* cropped = new Atil::Image(size, &image->dataModel(), initialImage);

		//copy pixels
		Atil::Offset upperLeft1(rectangle[0],rectangle[1]);
		Atil::ImageContext *pImgContext1
			= image->createContext(
			Atil::ImageContext::kRead,
			size,
			upperLeft1
			);

		Atil::Offset upperLeft(0,0);
		Atil::ImageContext *pImgContext2
			= cropped->createContext(
			Atil::ImageContext::kWrite,
			size,
			upperLeft
			);
		
		
		int row=0,col = 0;
		for (row=upperLeft1.y;row< size.height+upperLeft1.y;++row)
		{
			for (col=upperLeft1.x;col<size.width+upperLeft1.x;++col)
				{
				try
				{
					Atil::ImagePixel pixel = pImgContext1->getPixel(col,row);
					pImgContext2->putPixel(col-upperLeft1.x,row-upperLeft1.y,pixel);
				}
				catch(Atil::ATILException* e)
				{
					delete e;
					return 1;
				}
			}
		}
		
		

		//save image
		
		try
                {
			Atil::RowProviderInterface* pPipe = cropped->read(cropped->size(), Atil::Offset(0,0));
			Atil::ImageFormatCodec *pCodec = NULL;
			pCodec = new JfifFormatCodec();
			Atil::FileWriteDescriptor *pFileWriteDescriptor = new Atil::FileWriteDescriptor(pCodec);
			wchar_t pFileName[1024] = L"C:\\img.jpg";
			Atil::FileSpecifier fs(Atil::StringBuffer(lstrlen(pFileName) + 1,
				(const Atil::Byte *) pFileName, Atil::StringBuffer::kASCII),
				Atil::FileSpecifier::kFilePath);
			pFileWriteDescriptor->setFileSpecifier(fs);
			pFileWriteDescriptor->createImageFrame(pPipe->dataModel(), pPipe->size());
			pFileWriteDescriptor->writeImageFrame(pPipe);
			delete pFileWriteDescriptor;
			delete pPipe;
			delete pCodec;
                }
                catch (Atil::ATILException* e)
                {
	          delete e;
	          return 0;
                }


		//clean up
		delete pImgContext1;
		delete pImgContext2;
		delete cropped;
		return 1;
	}

 can somebody please points me to what I'm doing wrong.

1 REPLY 1
Message 2 of 2
Balaji_Ram
in reply to: Anonymous

Hi Youssef,

 

Sorry for the delay.

 

Have you tried the code provided in this blog post to write the image to a file ?

 

Look for the "writeImageFile" method in this blog post.

 

http://adndevblog.typepad.com/autocad/2013/01/capturing-a-screen-shot-using-objectarx.html

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost