[iOS] load UIImage as Texture

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

[iOS] load UIImage as Texture

Post by IrrlichtForiOS »

Is it possible to load an texture based on an UIImage Object?
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

Nobody knows an answer?
Last edited by IrrlichtForiOS on Mon Nov 02, 2015 6:55 am, edited 1 time in total.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [iOS] load UIImage as Texture

Post by mongoose7 »

I don't do GUII, but if you look in IGUIImage.h you will see there is a method getImage that returns ITexture*. Also, since you had to add the image anyway, you must already have it?
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

mongoose7 wrote:I don't do GUII, but if you look in IGUIImage.h you will see there is a method getImage that returns ITexture*. Also, since you had to add the image anyway, you must already have it?
GUII has nothing todo with iOS's UIKit UIImage class.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

After I've found http://irrlicht.sourcearchive.com/docum ... 41a06.html I implemented the following methods.

Retrieve an Texture...

Code: Select all

 
        core::dimension2d<u32> retTexSize;
               
        void* data;
        core::dimension2d<u32> size;
        
        bool success = frame->getImageData(&data, &size);
        
        if (success) {
            loadImage->loadImageForBackground(data, size, retTexSize);
        }
 
getImageData()

Code: Select all

 
bool FAAppFrame::getImageData(void *data, core::dimension2d<u32> *size)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:"PATH"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        
        NSData *imageData = [NSData dataWithContentsOfFile:filePath];
        
        if (!imageData) {
            
            return false;
        }
        
        data = (void *)[imageData bytes];
        
        *size = core::dimension2d<u32>(512, 512);
        
        return true;
    }
    
    return false;
}
loadImageForBackground

Code: Select all

bool E3DCLoadImage::loadImageForBackground(void* data, core::dimension2d<u32> size, core::dimension2d<u32> &retTexSize)
{
    video::IImage *texImg = driver->createImageFromData(video::ECF_R8G8B8, size, data, false, true ); // fails with EXC_BAD_ACCESS
 
    video::ITexture *tex = driver->addTexture(L"datatexture", texImg);
The method that fails is the CImage raw-data constructor

Code: Select all

 
//! Constructor from raw data
CImage::CImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size, void* data,
            bool ownForeignMemory, bool deleteForeignMemory, bool compressed, bool mipMaps)
: Data(0), Size(size), Format(format), IsCompressed(compressed), HasMipMaps(mipMaps), DeleteMemory(deleteForeignMemory)
{
    if (ownForeignMemory)
    {
        Data = (u8*)0xbadf00d;
        initData();
        Data = (u8*)data;
    }
    else
    {
        Data = 0;
        initData();
        memcpy(Data, data, Size.Height * Pitch); // EXC_BAD_ACCESS
    }
}
 
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

:( Always no solution yet...
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [iOS] load UIImage as Texture

Post by mongoose7 »

Do you get any warnings? Look, the problem is in *your* code so you have to be prepared to debug it. For example,

Code: Select all

void* data;
bool success = frame->getImageData(&data, &size);
but

Code: Select all

bool FAAppFrame::getImageData(void *data, core::dimension2d<u32> *size)
and

Code: Select all

data = (void *)[imageData bytes];
In summary, the pointer defined by "void *data'" is not updated, so the SIGSEGV is expected.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

mongoose7 wrote:Do you get any warnings? Look, the problem is in *your* code so you have to be prepared to debug it. For example,

Code: Select all

void* data;
bool success = frame->getImageData(&data, &size);
but

Code: Select all

bool FAAppFrame::getImageData(void *data, core::dimension2d<u32> *size)
and

Code: Select all

data = (void *)[imageData bytes];
In summary, the pointer defined by "void *data'" is not updated, so the SIGSEGV is expected.
Can you explain me why you think, data will not be updated?

EDIT: I've changed my code to:

Code: Select all

irr::u8 data;
        core::dimension2d<u32> size;
        
        bool success = frame->getCurrentFassadenBildData(&data, &size);

Code: Select all

bool FAAppFrame::getCurrentFassadenBildData(irr::u8 *data, core::dimension2d<u32> *size)
and

Code: Select all

data = (u8 *)[imageData bytes];
but nothing has changed...

in the CImage constructor data has the following value..

Code: Select all

(void *) data = 0x000000016fdf7090
EDITEDIT:

These are the iOS data size and the calculated irrlicht size

Code: Select all

 
15586683 (iOS)
23970816 (Irrlicht)
I don't really know, why the size is calculated absolutely wrong... The Image Resolution is correct, maybe the Pitch value is wrong?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [iOS] load UIImage as Texture

Post by mongoose7 »

"data = <value>" only changes the local pointer. To change the pointer in the outer function you need "*data = <value>". You also need to change the function prototype.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

mongoose7 wrote:"data = <value>" only changes the local pointer. To change the pointer in the outer function you need "*data = <value>". You also need to change the function prototype.
Hi,

I've changed my function now to

Code: Select all

bool FAAppFrame::getCurrentFassadenBildData(void *data, core::dimension2d<u32> *size)
and impemented malloc an mempy in this function

Code: Select all

 
NSUInteger len = [imageData length];
        
data = malloc(len);
        
memcpy(data, [imageData bytes], len);
 
Nothing has changed yet... what am i doing wrong? The data i give into CImage contructor is set, but it's crashing by copying it.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [iOS] load UIImage as Texture

Post by mongoose7 »

Code: Select all

bool FAAppFrame::getCurrentFassadenBildData(void **data, core::dimension2d<u32> *size)

Code: Select all

        
*data = malloc(len);
memcpy(*data, [imageData bytes], len);
 
I can't help you any more - you're going to have to learn to code before you can make any progress. As an old C programmer, I usually prefer

Code: Select all

void *getCurrentDada(int width, int height)
{
    int len = width*height;
    void *data = malloc(len);
    memcpy(data, bytes, len);
    return data;
}
but that's the kind of guy I am.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

mongoose7 wrote:

Code: Select all

bool FAAppFrame::getCurrentFassadenBildData(void **data, core::dimension2d<u32> *size)

Code: Select all

        
*data = malloc(len);
memcpy(*data, [imageData bytes], len);
 
I can't help you any more - you're going to have to learn to code before you can make any progress. As an old C programmer, I usually prefer

Code: Select all

void *getCurrentDada(int width, int height)
{
    int len = width*height;
    void *data = malloc(len);
    memcpy(data, bytes, len);
    return data;
}
but that's the kind of guy I am.
Thanks for your patience to try with helping... but all these ways are not working together with iOS + Irrlicht. The CImage constructor is always crashing. This has nothing to do with that i'm not abled to code. C++ is not my mother tongue as Objective-C is.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

One step forward...

Post by IrrlichtForiOS »

So...after some struggel with this topic i'm at the Point, where i can load an image, but the resulting image is really noisy, black and strange...

Code: Select all

 
// Start loading the image
void *data = frame->getCurrentFassadenBildData(&size);
        
        if (data != nullptr) {
            loadImage->loadImageForBackground(data, size, retTexSize);
        }
 

Code: Select all

 
// return image data as void *
void *  FAAppFrame::getCurrentFassadenBildData(core::dimension2d<u32> *size) {
 
     //.....
 
        void *data = FAAppFrame::getDataByBytesAndSize((void *)[imageData bytes], (int)[imageData length]);
        
        *size = core::dimension2d<u32>(resW, resH);
        
        return data;
}
 

Code: Select all

 
// copy iOS-data into CPP data...
void * FAAppFrame::getDataByBytesAndSize(void *bytes, int len)
{
    void *data = malloc(len);
    
    memcpy(data, bytes, len);
    
    return data;
}

Code: Select all

 
// Create image CImage from data
loadImageForBackground(void *data, core::dimension2d<u32> size, core::dimension2d<u32> &retTexSize)
{
    /*!
     *  @link http://irrlicht.sourcearchive.com/documentation/1.7.1plus-pdfsg1-1/classirr_1_1video_1_1IVideoDriver_a425d60f2fcad42d8a79c33c587f41a06.html
     */
    video::IImage *texImg = driver->createImageFromData(video::ECF_R8G8B8, size, data, false, true);
 
Does anybody know how to get rid of this noise?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [iOS] load UIImage as Texture

Post by mongoose7 »

Do you know how big an image is? If it is R8G8B8, then the size is WxHx3, not WxH.
Do you know the format of the original data? Maybe it is not R8G8B8. Maybe it is B8G8R8 or A8R8G8B8. Maybe it is JPEG.
IrrlichtForiOS
Posts: 61
Joined: Mon Oct 08, 2012 1:46 pm

Re: [iOS] load UIImage as Texture

Post by IrrlichtForiOS »

mongoose7 wrote:Do you know how big an image is? If it is R8G8B8, then the size is WxHx3, not WxH.
Do you know the format of the original data? Maybe it is not R8G8B8. Maybe it is B8G8R8 or A8R8G8B8. Maybe it is JPEG.
Therefore i'm using NSData's 'lenght' property, which is related to these factors.
Post Reply