discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Rendering grayscale on opengl using nsbitmaprep


From: indieAN
Subject: Rendering grayscale on opengl using nsbitmaprep
Date: Fri, 11 Feb 2011 08:47:21 +0530

Hi, 
  I trying to render a gray scale image (contains 2 channels luminosity
and alpha) using opengl. Actually this image is a png image. I am
decoding it using nsbitmaprep. But only garbage  gets rendered. Can any
body tell whats wrong with following code. Or their is bug in
nsbitmapImagerep for rendering grayscale?


-(id) initPremultipliedATextureWithBitmap:(NSBitmapImageRep*)bitmap
pixelsWide:(NSUInteger)POTWide pixelsHigh:(NSUInteger)POTHigh
{

    short bytesPerPixel = [bitmap bitsPerPixel]>>3;//2
        unsigned char * data = calloc(1, POTHigh  * POTWide * 4);
        const unsigned char * originaldata = [bitmap bitmapData];



    NSSize size=NSMakeSize([bitmap pixelsWide],[bitmap
pixelsHigh]); //64*64
    NSUInteger bytesPerRow = [bitmap bytesPerRow];//128
    NSUInteger pixelPerRow =  bytesPerRow / bytesPerPixel;//64
    CCTexture2DPixelFormat      pixelFormat;
    
    NSUInteger inputPixelPerRow = POTWide * 4;
    
    
    
     int spp = [bitmap samplesPerPixel];  //2
    
   
#define FLIPPED_BITMAP 0    
     int i;

     for (i = 0; i < size.height ; i++) 
    {

    
#if FLIPPED_BITMAP    
     int vrow=(int)size.height -i-1;
#else
     int vrow=i;
#endif          
      
     
      if(spp == 2)
      {
  

        unsigned char * dest =  data + (vrow * POTWide * 4);
        const unsigned char * src =  originaldata + (i * bytesPerRow);
        
        int k=0;
        for(k=0; k<size.width ; k++) 
        {
        
                *(dest +k*4+3)=*(src+k*2+1); //alpha    
                
                 *(dest +k*4) = *(src+k*2+2);//r
                 *(dest +k*4 + 1) = *(src+k*2);//g
                 *(dest +k*4 + 2) = *(src+k*2);//b
                 
        }
      }
       else 
       memcpy(data + (vrow * POTWide * 4), originaldata + (i *
bytesPerRow), bytesPerRow);
   }
    
    
      
        self = [self initWithData:data pixelFormat:pixelFormat
pixelsWide:POTWide pixelsHigh:POTHigh contentSize:size
                        unpackRowLength:POTWide];
     
     
        return self;
};

Thanks in advance.
-indieAN 
https://github.com/ANindie/cocos2d-GNUstep





reply via email to

[Prev in Thread] Current Thread [Next in Thread]