this basically is an experimental thing, i am reading a caption watermark from a BMP
image through extracting and putting together the Least Significant bits of an image.
I have created an embedder to do this, and my caption extractor is an XPCOM component.
When I extract captions from a watermarked BMP image, everything works fine. But when I
read a non-watermarked image, it causes memory leaks.

By the way, the first 32 bytes after the BMP header are used to form a 32bit integer
which tells where the offset of the last watermarked byte is.

here's the extractWatermark method, thanks.

-> johanna

/* string extractWatermark (in string url); */
NS_IMETHODIMP Extractor::ExtractWatermark(const char *url, char **_retval)
{
        PRUint32 bytes, success;
        PRInt32 n, compType, bmpType; 
        PRInt32 lastPixel, i, j, iByte, tBit, caplen, k;
        char c, cBit, cByte;
        char *buffer, *caption;
        char *msg1 = "EX4369";
        char *msg2 = "EX4353";
        char *msg3 = "EX4113";

        nsCOMPtr<nsIServiceManager> servMan;
        nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));
        if (NS_FAILED(rv)) return rv;

        nsCOMPtr<nsIIOService> ioService;
        rv = servMan->GetServiceByContractID(
                "@mozilla.org/network/io-service;1",
                NS_GET_IID(nsIIOService),
                getter_AddRefs(ioService));
        if(NS_FAILED(rv)) return rv;

        nsCOMPtr<nsIURI> uri;
        nsEmbedCString uriString(url);
        rv = ioService -> NewURI(uriString, nsnull, nsnull, getter_AddRefs(uri));
        if(NS_FAILED(rv)) return rv;

        nsCOMPtr<nsIChannel> channel;
        rv = ioService -> NewChannelFromURI(uri, getter_AddRefs(channel));
        if(NS_FAILED(rv)) return rv;

        // bytes = iStream.available();
        nsCOMPtr<nsIInputStream> iStream;
        rv = channel -> Open(getter_AddRefs(iStream));
        rv = iStream -> Available(&bytes);

        buffer = (char*)nsMemory::Alloc(bytes);
    if (!buffer) return NS_ERROR_OUT_OF_MEMORY;

        // success = iStream.read(buffer, bytes)
        rv = iStream -> Read(buffer, bytes, &success);

        // check for 'BM' in header or 0x4D42, 19778

        bmpType = (((PRInt32)buffer[0])&0xff)|
                ((((PRInt32)buffer[1])&0xff)<<8);

        // check for compression type, 24 or 8 bit bmp

        compType = (((PRInt32)buffer[30])&0xff)|
                ((((PRInt32)buffer[31])&0xff)<<8)|
                ((((PRInt32)buffer[32])&0xff)<<16)|
                ((((PRInt32)buffer[33])&0xff)<<24);
        
        if((bmpType == 19778) && (compType == 0))
        {
                // get the offset of the first pixel byte
                n = (((PRInt32)buffer[10])&0xff)|
                        ((((PRInt32)buffer[11])&0xff)<<8)|
                        ((((PRInt32)buffer[12])&0xff)<<16)|
                        ((((PRInt32)buffer[13])&0xff)<<24);

                /*      the first 32 pixel bytes form a 32-bit integer
                        which is the offset of the last pixel that contains
                        information
                */
                if(n>0 && (n<(PRInt32)(bytes-n-4)))
                {
                        j=31;
                        lastPixel = lastPixel & 0x0;
        
                        for(i = n; i <= n + 31; i++, j--)
                        {
                                iByte = (((PRInt32)buffer[i]) & 0xff);
                                tBit = iByte & 0x1;
                                tBit = tBit << j;
                                lastPixel = lastPixel | tBit;
                        }
                
                        caplen = lastPixel / 8;
        
                        caption = (char*)nsMemory::Alloc(caplen + 1);
                        if(!caption)
                        {
                                nsMemory::Free(buffer);
                                return NS_ERROR_OUT_OF_MEMORY;
                        }

                //Read the next pixel(at i) up to lastPixel
                //j - used to shift the bits and to tell if we have filled a byte
                //k - tells the current offset within caption
                //c - character

                        j = 8;
                        k = 0;
                        c = c & 0x0;
                        for(; i <= lastPixel; i++)
                        {
                                j--;
                                cByte = ((buffer[i]) & 0xff);
                                cBit = (cByte & 0x1) << j;
                                // shift the bit to the left j times
                                c = c | cBit;
                
                                // if one byte is complete
                                if(j == 0)
                                {
                                                caption[k] = c;
                                        j = 8;
                                        c = c & 0x0; // reset to 0
                                        k++; // increment offset
                                }
                        }
                        *_retval = caption;
                }// end of if
                else
                {
                        *_retval = msg2;
                }
        }//end of if
        else
        {
                *_retval = msg1;
        }

        nsMemory::Free(buffer);

    return NS_OK;
};
-------------------------------------------------------------------------------------------
***Protect your PC from local E-Mail Application security holes***
***Maintain your Privacy - MS Passport Free***
***Anti SPAM "Whitelist" feature***

http://www.xmail.net Web E-Mail, accessible anywhere, 128 bit SSL Secure

Voice Messages, Voice Calls (VoIP), Video Conferencing, Live Chat, 
XMail Messenger, Personal Web Hosting, Private Disk Storage,  
Calendar, Bookmarks, Forwarding, Virtual Mail Map Aliasing

XMail Premium: 20 - 250MB Storage, 20MB Messages, SMTP, POP3, Ad Free
Starting at $9.95 per year
-------------------------------------------------------------------------------------------
Anonymous Web Surfing http://www.snoopblocker.com
Search http://www.teradex.com
_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to