Exemple #1
1
int main( int argc, char **argv )
{
    if ( argc < 2 ) {
	qWarning( "Usage:\n\t%s [--images] files", argv[0] );
	return 1;
    }

    QFile output;
    bool  images = FALSE;
    output.open( IO_WriteOnly, stdout );
    QTextStream out( &output );

    EmbedImageList list_image;
    QList<Embed> list;
    list.setAutoDelete( TRUE );
    list_image.setAutoDelete( TRUE );

  // Embed data for all input files

    out << "/* Generated by qembed */\n";
    srand( time(0) );
    long l = rand();
    out << "#ifndef _" << l << endl;
    out << "#define _" << l << endl;

    for ( int i=1; i<argc; i++ ) {
	QString arg = argv[i];
	if ( arg == "--images" ) {
	    if ( !images ) {
		out << "#include <qimage.h>\n";
		out << "#include <stdlib.h>\n";
		images = TRUE;
	    }
	} else {
	    QFile f( argv[i] );
	    if ( !f.open(IO_ReadOnly) ) {
		qWarning( "Cannot open file %s, ignoring it", argv[i] );
		continue;
	    }
	    QByteArray a( f.size() );
	    if ( f.readBlock(a.data(), f.size()) != (int)f.size() ) {
		qWarning( "Cannot read file %s, ignoring it", argv[i] );
		f.close();
		continue;
	    }
	    if ( images ) {
		QImage img;
		if ( !img.loadFromData(a) ) {
		    qWarning( "Cannot read image from file %s, ignoring it", argv[i] );
		    f.close();
		    continue;
		}
		EmbedImage *e = new EmbedImage;
		e->width = img.width();
		e->height = img.height();
		e->depth = img.depth();
		e->numColors = img.numColors();
		e->colorTable = new QRgb[e->numColors];
		e->alpha = img.hasAlphaBuffer();
		memcpy(e->colorTable, img.colorTable(), e->numColors*sizeof(QRgb));
		QFileInfo fi(argv[i]);
		e->name = fi.baseName();
		e->cname = convertFileNameToCIdentifier( e->name.latin1() );
		list_image.append( e );
		QString s;
		if ( e->depth == 32 ) {
		    out << s.sprintf( "static const QRgb %s_data[] = {",
				   (const char *)e->cname );
		    embedData( (QRgb*)img.bits(), e->width*e->height, &output );
		} else {
		    if ( e->depth == 1 )
			img = img.convertBitOrder(QImage::BigEndian);
		    out << s.sprintf( "static const unsigned char %s_data[] = {",
				   (const char *)e->cname );
		    embedData( img.bits(), img.numBytes(), &output );
		}
		out << "\n};\n\n";
		if ( e->numColors ) {
		    out << s.sprintf( "static const QRgb %s_ctable[] = {",
				   (const char *)e->cname );
		    embedData( e->colorTable, e->numColors, &output );
		    out << "\n};\n\n";
		}
	    } else {
		Embed *e = new Embed;
		e->size = f.size();
		e->name = argv[i];
		e->cname = convertFileNameToCIdentifier( argv[i] );
		list.append( e );
		QString s;
		out << s.sprintf( "static const unsigned int  %s_len = %d;\n",
			       (const char *)e->cname, e->size );
		out << s.sprintf( "static const unsigned char %s_data[] = {",
			       (const char *)e->cname );
		embedData( a, &output );
		out << "\n};\n\n";
	    }

	    f.close();
	}
    }

  // Generate summery

    if ( list.count() > 0 ) {
	out << "#include <qcstring.h>\n";
	if ( !images )
	    out << "#include <qdict.h>\n";

	out << "static struct Embed {\n"
	       "    unsigned int         size;\n"
	       "    const unsigned char *data;\n"
	       "    const char          *name;\n"
	       "} embed_vec[] = {\n";
	Embed *e = list.first();
	while ( e ) {
	    out << "    { " << e->size << ", " << e->cname << "_data, "
		 << "\"" << e->name << "\" },\n";
	    e = list.next();
	}
	out << "    { 0, 0, 0 }\n};\n";

	out << "\n"
	    "inline const QByteArray& qembed_findData(const char* name)\n"
	    "{\n"
	    "    static QDict<QByteArray> dict;\n"
	    "    QByteArray* ba = dict.find(name);\n"
	    "    if ( !ba ) {\n"
	    "        for (int i=0; embed_vec[i].data; i++) {\n"
	    "    	if ( 0==strcmp(embed_vec[i].name, name) ) {\n"
	    "    	    ba = new QByteArray;\n"
	    "    	    ba->setRawData( (char*)embed_vec[i].data,\n"
	    "    			    embed_vec[i].size );\n"
	    "    	    break;\n"
	    "    	}\n"
	    "        }\n"
	    "        if ( !ba ) {\n"
	    "            static QByteArray dummy;\n"
	    "            return dummy;\n"
	    "        }\n"
	    "    }\n"
	    "    return *ba;\n"
	    "}\n\n";
    }

    if ( list_image.count() > 0 ) {
	out << "static struct EmbedImage {\n"
	       "    int width, height, depth;\n"
	       "    const unsigned char *data;\n"
	       "    int numColors;\n"
	       "    const QRgb *colorTable;\n"
	       "    bool alpha;\n"
	       "    const char *name;\n"
	       "} embed_image_vec[] = {\n";
	list_image.sort();
	EmbedImage *e = list_image.first();
	while ( e ) {
	    out << "    { "
		<< e->width << ", "
		<< e->height << ", "
		<< e->depth << ", "
		<< "(const unsigned char*)" << e->cname << "_data, "
		<< e->numColors << ", ";
	    if ( e->numColors )
		out << e->cname << "_ctable, ";
	    else
		out << "0, ";
	    if ( e->alpha )
		out << "TRUE, ";
	    else
		out << "FALSE, ";
	    out << "\"" << e->name << "\" },\n";
	    e = list_image.next();
	}
	out << "};\n";

	out << "\n"
	    "static int cmpEmbedImage(const void *a, const void *b)\n"
	    "{\n"
	    "    const EmbedImage* ea = (const EmbedImage*)a;\n"
	    "    const EmbedImage* eb = (const EmbedImage*)b;\n"
	    "    return strcmp(ea->name,eb->name);\n"
	    "}\n"
	    "inline const QImage& qembed_findImage(const char* name)\n"
	    "{\n"
	    "    EmbedImage key; key.name = name;\n"
	    "    EmbedImage* r = (EmbedImage*)bsearch( &key, embed_image_vec,\n"
	    "        sizeof(embed_image_vec)/sizeof(EmbedImage), sizeof(EmbedImage), cmpEmbedImage );\n"
	    "    QImage* img;\n"
	    "    if ( r ) {\n"
	    "        img = new QImage((uchar*)r->data,\n"
	    "                            r->width,\n"
	    "                            r->height,\n"
	    "                            r->depth,\n"
	    "                            (QRgb*)r->colorTable,\n"
	    "                            r->numColors,\n"
	    "                            QImage::BigEndian\n"
	    "                    );\n"
	    "        if ( r->alpha )\n"
	    "            img->setAlphaBuffer(TRUE);\n"
	    "    } else {\n"
	    "        static QImage dummy;\n"
	    "        img = &dummy;\n"
	    "    }\n"
	    "    return *img;\n"
	    "}\n\n";
    }

    out << "#endif" << endl;

    return 0;
}
    void writePNG(const QImage& image)
    {
        info_ptr->channels = 4;
        png_set_sig_bytes(png_ptr, 8); // Pretend we already wrote the sig
        png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
                     8, image.hasAlphaBuffer()
                     ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
                     0, 0, 0);
        png_write_info(png_ptr, info_ptr);
        if ( !image.hasAlphaBuffer() )
            png_set_filler(png_ptr, 0,
                           QImage::systemByteOrder() == QImage::BigEndian ?
                           PNG_FILLER_BEFORE : PNG_FILLER_AFTER);
        //if ( QImage::systemByteOrder() == QImage::BigEndian ) {
        //png_set_swap_alpha(png_ptr);
        //}
        if ( QImage::systemByteOrder() == QImage::LittleEndian ) {
            png_set_bgr(png_ptr);
        }

        png_bytep* row_pointers;
        uint height = image.height();
        uchar** jt = image.jumpTable();
        row_pointers=new png_bytep[height];
        uint y;
        for (y=0; y<height; y++) {
            row_pointers[y]=jt[y];
        }
        png_write_image(png_ptr, row_pointers);
        delete [] row_pointers;
        png_write_end(png_ptr, info_ptr);
        end_png();
        begin_png();
    }
void Uic::embed( QTextStream& out, const char* project, const QStringList& images )
{

    QString cProject = convertToCIdentifier( project );

    QStringList::ConstIterator it;
    out << "/****************************************************************************\n";
    out << "** Image collection for project '" << project << "'.\n";
    out << "**\n";
    out << "** Generated from reading image files: \n";
    for ( it = images.begin(); it != images.end(); ++it )
	out << "**      " << *it << "\n";
    out << "**\n";
    out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
    out << "**      by: The User Interface Compiler ($Id: embed.cpp 2 2005-11-16 15:49:26Z dmik $)\n";
    out << "**\n";
    out << "** WARNING! All changes made in this file will be lost!\n";
    out << "****************************************************************************/\n";
    out << "\n";

    out << "#include <qimage.h>\n";
    out << "#include <qdict.h>\n";
    out << "#include <qmime.h>\n";
    out << "#include <qdragobject.h>\n";
    out << "\n";

    QPtrList<EmbedImage> list_image;
    list_image.setAutoDelete( TRUE );
    int image_count = 0;
    for ( it = images.begin(); it != images.end(); ++it ) {
	QImage img;
	if ( !img.load( *it ) ) {
	    fprintf( stderr, "uic: cannot load image file %s\n", (*it).latin1() );
	    continue;
	}
	EmbedImage *e = new EmbedImage;
	e->width = img.width();
	e->height = img.height();
	e->depth = img.depth();
	e->numColors = img.numColors();
	e->colorTable = new QRgb[e->numColors];
	e->alpha = img.hasAlphaBuffer();
	memcpy(e->colorTable, img.colorTable(), e->numColors*sizeof(QRgb));
	QFileInfo fi( *it );
	e->name = fi.fileName();
	e->cname = QString("image_%1").arg( image_count++);
	list_image.append( e );
	out << "// " << *it << "\n";
	QString s;
	if ( e->depth == 1 )
	    img = img.convertBitOrder(QImage::BigEndian);
	out << s.sprintf( "static const unsigned char %s_data[] = {",
			  (const char *)e->cname );
#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
	e->compressed =
#endif
	    embedData( out, img.bits(), img.numBytes() );
	out << "\n};\n\n";
	if ( e->numColors ) {
	    out << s.sprintf( "static const QRgb %s_ctable[] = {",
			      (const char *)e->cname );
	    embedData( out, e->colorTable, e->numColors );
	    out << "\n};\n\n";
	}
    }

    if ( !list_image.isEmpty() ) {
	out << "static struct EmbedImage {\n"
	    "    int width, height, depth;\n"
	    "    const unsigned char *data;\n"
#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
	    "    ulong compressed;\n"
#endif
	    "    int numColors;\n"
	    "    const QRgb *colorTable;\n"
	    "    bool alpha;\n"
	    "    const char *name;\n"
	    "} embed_image_vec[] = {\n";
	EmbedImage *e = list_image.first();
	while ( e ) {
	    out << "    { "
		<< e->width << ", "
		<< e->height << ", "
		<< e->depth << ", "
		<< "(const unsigned char*)" << e->cname << "_data, "
#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
		<< e->compressed << ", "
#endif
		<< e->numColors << ", ";
	    if ( e->numColors )
		out << e->cname << "_ctable, ";
	    else
		out << "0, ";
	    if ( e->alpha )
		out << "TRUE, ";
	    else
		out << "FALSE, ";
	    out << "\"" << e->name << "\" },\n";
	    e = list_image.next();
	}
#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
	out << "    { 0, 0, 0, 0, 0, 0, 0, 0, 0 }\n};\n";
#else
	out << "    { 0, 0, 0, 0, 0, 0, 0, 0 }\n};\n";
#endif

	out << "\n"
	    "static QImage uic_findImage( const QString& name )\n"
	    "{\n"
	    "    for ( int i=0; embed_image_vec[i].data; i++ ) {\n"
	    "	if ( QString::fromUtf8(embed_image_vec[i].name) == name ) {\n"
#ifndef QT_NO_IMAGE_COLLECTION_COMPRESSION
	    "	    QByteArray baunzip;\n"
	    "	    baunzip = qUncompress( embed_image_vec[i].data, \n"
	    "		embed_image_vec[i].compressed );\n"
	    "	    QImage img((uchar*)baunzip.data(),\n"
	    "			embed_image_vec[i].width,\n"
	    "			embed_image_vec[i].height,\n"
	    "			embed_image_vec[i].depth,\n"
	    "			(QRgb*)embed_image_vec[i].colorTable,\n"
	    "			embed_image_vec[i].numColors,\n"
	    "			QImage::BigEndian\n"
	    "		);\n"
	    "	    img = img.copy();\n"
#else
	    "	    QImage img((uchar*)embed_image_vec[i].data,\n"
	    "			embed_image_vec[i].width,\n"
	    "			embed_image_vec[i].height,\n"
	    "			embed_image_vec[i].depth,\n"
	    "			(QRgb*)embed_image_vec[i].colorTable,\n"
	    "			embed_image_vec[i].numColors,\n"
	    "			QImage::BigEndian\n"
	    "		);\n"
#endif
	    "	    if ( embed_image_vec[i].alpha )\n"
	    "		img.setAlphaBuffer(TRUE);\n"
	    "	    return img;\n"
	    "        }\n"
	    "    }\n"
	    "    return QImage();\n"
	    "}\n\n";

	out << "class MimeSourceFactory_" << cProject << " : public QMimeSourceFactory\n";
	out << "{\n";
	out << "public:\n";
	out << "    MimeSourceFactory_" << cProject << "() {}\n";
	out << "    ~MimeSourceFactory_" << cProject << "() {}\n";
	out << "    const QMimeSource* data( const QString& abs_name ) const {\n";
	out << "\tconst QMimeSource* d = QMimeSourceFactory::data( abs_name );\n";
	out << "\tif ( d || abs_name.isNull() ) return d;\n";
	out << "\tQImage img = uic_findImage( abs_name );\n";
	out << "\tif ( !img.isNull() )\n";
	out << "\t    ((QMimeSourceFactory*)this)->setImage( abs_name, img );\n";
	out << "\treturn QMimeSourceFactory::data( abs_name );\n";
	out << "    };\n";
	out << "};\n\n";

	out << "static QMimeSourceFactory* factory = 0;\n";
	out << "\n";

	out << "void qInitImages_" << cProject << "()\n";
	out << "{\n";
	out << "    if ( !factory ) {\n";
	out << "\tfactory = new MimeSourceFactory_" << cProject << ";\n";
	out << "\tQMimeSourceFactory::defaultFactory()->addFactory( factory );\n";
	out << "    }\n";
	out << "}\n\n";

	out << "void qCleanupImages_" << cProject << "()\n";
	out << "{\n";
	out << "    if ( factory ) {\n";
	out << "\tQMimeSourceFactory::defaultFactory()->removeFactory( factory );\n";
	out << "\tdelete factory;\n";
	out << "\tfactory = 0;\n";
	out << "    }\n";
	out << "}\n\n";

	out << "class StaticInitImages_" << cProject << "\n";
	out << "{\n";
	out << "public:\n";
	out << "    StaticInitImages_" << cProject << "() { qInitImages_" << cProject << "(); }\n";
	out << "#if defined(Q_OS_SCO) || defined(Q_OS_UNIXWARE)\n";
	out << "    ~StaticInitImages_" << cProject << "() { }\n";
	out << "#else\n";
	out << "    ~StaticInitImages_" << cProject << "() { qCleanupImages_" << cProject << "(); }\n";
	out << "#endif\n";
	out << "};\n\n";

	out << "static StaticInitImages_" << cProject << " staticImages;\n";
    }
}
void
get_transformed_pixmap(QPixmap* originalPixmap,
		       QPixmap* destPixmap,
		       int src_x, int src_y, 
		       int src_width, int src_height,
		       int transform,
		       bool hasAlpha) {

    QImage originalImage = originalPixmap->convertToImage();

    if ( hasAlpha ) {
	// Qt's handling of the alpha channel in the conversion
	// process between QPixmap and QImage is buggy.
	// If the pixmap's pixels only have alpha values 0x00 and 0xFF
	// then the resulting QImage from conversion will return
	// false for hasAlphaBuffer().
	// so we set our own flag instead of depending on Qt to 
	// maintain alpha information.
	originalImage.setAlphaBuffer(TRUE);
    }

    /*Qt gives us this useful API that returns a section of a QImage*/
    QImage sectionImage  = originalImage.copy(src_x, src_y, 
					      src_width, src_height);
    /* Skip this pixel-by-pixel copy if there is no transform */
    if (0 != transform) {
	QImage sectionImage32bpp = sectionImage.convertDepth(32);
	QImage processedImage;
	
	
	int nXOriginSrc = 0;
	int nYOriginSrc = 0;
	int nWidth      = src_width;
	int nHeight     = src_height;
	
	/*scan length of the source image*/
	int imageWidth  = src_width;
	/*number of rows of the source image*/
	int imageHeight = src_height;
	
	int imgLen;
	int srcImgLen;
	
	int t_width;
	int t_height;
	
	int srcX;
	int srcY;
	int xStart;
	int yStart;
	int xIncr;
	int yIncr;
	int destX;
	int destY;
	int yCounter;
	int xCounter;
	
	int srcIndex;
	int destIndex;
	
	uchar* srcBits     = NULL;
	uchar* destBits    = NULL;
	
	uchar* srcBitsPtr  = NULL;
	uchar* destBitsPtr = NULL;
	
	
	/* set dimensions of image being created,
	   depending on transform */
	if (transform & TRANSFORM_INVERTED_AXES) {
	    t_width  = src_height;
	    t_height = src_width;
	} else {
	    t_width  = src_width;
	    t_height = src_height;
	}
	
	/* width * height * 4 gives us the size of a 32 bpp image */
	imgLen = nWidth * nHeight << 2;
	srcImgLen = imageWidth  * imageHeight << 2;
	
	/* Qt specific */
	processedImage.create(t_width, t_height, 32);
	
	srcBits  = sectionImage32bpp.bits();
	destBits = processedImage.bits();
	/* ----------- */
	
	if (transform & TRANSFORM_Y_FLIP) {
	    yStart = nHeight-1;
	    yIncr = -1;
	} else {
	    yStart = 0;
	    yIncr = +1;
	}
	
	if (transform & TRANSFORM_X_FLIP) {
	    xStart = nWidth-1;
	    xIncr = -1;
	} else {
	    xStart = 0;
	    xIncr = +1;
	}
	
	srcBitsPtr  = srcBits;
	destBitsPtr = destBits;
	
	
	/* increment srcX,Y regular. increment destX,Y according to transform.
	   this makes handling of mask and alpha values easier */
	
	for (srcY = nYOriginSrc, destY = yStart, yCounter = 0; 
	     yCounter < nHeight; 
	     srcY++, destY+=yIncr, yCounter++) {
	    
	    /* in the current implementation we have source bitmap
	       dimension as the width of the image and the height of the region
	       destination bitmap is of the dimensions of the region */
	    
	    for (srcX = nXOriginSrc, destX = xStart, xCounter = 0; 
		 xCounter < nWidth; 
		 srcX++, destX+=xIncr, xCounter++) {
		
		if ( transform & TRANSFORM_INVERTED_AXES ) {
		    destIndex =  ( ( (destX) * t_width) + (destY) );
		} else {
		    destIndex =  ( ( (destY) * t_width) + (destX) );
		}
		
		destBitsPtr =  destBits + (destIndex * 4) ;
		
		srcIndex = (((srcY) * imageWidth) + (srcX));
		srcBitsPtr = srcBits + (srcIndex * 4);
		
		
		/* copy the pixel that is pointed to */
		*((int *)destBitsPtr) = *((int *)srcBitsPtr);
		
	    } /*for x*/
	    
	} /* for y */
	
	
	/* ---------- */

	if(TRUE == sectionImage.hasAlphaBuffer() ) {
	    processedImage.setAlphaBuffer(TRUE);
	} else {
	    processedImage.setAlphaBuffer(FALSE);
	}
	
	destPixmap->convertFromImage(processedImage);
    } else {
	/* No transform, just copy the image sub-section */
	destPixmap->convertFromImage(sectionImage);
    }
}
extern "C" void gxpport_decodeimmutable_to_platformbuffer
(unsigned char* srcBuffer, long length, 
 unsigned char** ret_dataBuffer, long* ret_length,
 gxutl_native_image_error_codes* creationErrorPtr) {

    gxutl_image_format format;
    MIDP_ERROR err;
    unsigned int w, h;

    err = gxutl_image_get_info(srcBuffer, (unsigned int)length,
			       &format, &w, &h);
    
    switch (err) {

    case MIDP_ERROR_NONE:
	break; /* continue */

    case MIDP_ERROR_IMAGE_CORRUPTED:
	*creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR;
	return;
    
    default:
	*creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR;
	return;
    }
		     
    switch (format) {

    case GXUTL_IMAGE_FORMAT_RAW:
        /* already in RAW format. make a copy */
	{
	    unsigned char* dataBuffer = (unsigned char*) midpMalloc(length);
            
	    if (NULL == dataBuffer) {
		*creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR;
	    } else {
                
		memcpy(dataBuffer, srcBuffer, length);
                
		*ret_dataBuffer = dataBuffer;
		*ret_length = length;

		*creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR;
	    }
	}
        break;

    case GXUTL_IMAGE_FORMAT_JPEG:
    case GXUTL_IMAGE_FORMAT_PNG:
        {
            QImage qimage;
            
            if (qimage.loadFromData((uchar*)srcBuffer, 
                                    (unsigned int)length)) {
                int imgWidth  = qimage.width();
                int imgHeight = qimage.height();
                
                if ((0 == imgWidth) || (0 == imgHeight)) {
                    *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR;
                } else {
                    QImage image;
                    if (IMAGE_DEPTH != qimage.depth()) {
                        image = qimage.convertDepth(IMAGE_DEPTH);
                    } else {
                        image = qimage;
                    }
                    
                    *ret_length = offsetof(gxutl_image_buffer_raw, data)
				+ image.numBytes();

                    gxutl_image_buffer_raw *dataBuffer =
			(gxutl_image_buffer_raw *) midpMalloc(*ret_length);
                    
                    if (NULL == dataBuffer) {
                        *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR;
                    } else {
                        dataBuffer->width    = (unsigned int)imgWidth;
                        dataBuffer->height   = (unsigned int)imgHeight;
                        dataBuffer->hasAlpha = (unsigned int)image.hasAlphaBuffer();
                        
                        memcpy(dataBuffer->header, gxutl_raw_header, 4);
                        memcpy(dataBuffer->data, image.bits(), image.numBytes());
                        
                        *ret_dataBuffer = (unsigned char *)dataBuffer;
                        *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR;
                    }
                }
            } else {
                *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR;
            }
        }
        break;
    
    default:
        *creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR;
        break;

    } /* switch (format) */
}
extern "C" void gxpport_get_immutable_argb
(gxpport_image_native_handle immutableImagePtr,
 jint* rgbBuffer, int offset, int scanLength,
 int x, int y, int width, int height,
 gxutl_native_image_error_codes* errorPtr) {

    _Platform_ImmutableImage* immutableImage = 
        (_Platform_ImmutableImage*)immutableImagePtr;

    if (NULL == immutableImage || immutableImage->qimage->isNull()) {
      /* not a valid image should not happen, log this */
      return ;
    }

    QImage   image;
    int      curOffset = offset;      /* current offset in output array */
    int      curX, curY;
    unsigned int r,g,b;
    QRgb     pixel;

    if (IMAGE_DEPTH != immutableImage->qimage->depth()) {
        image = immutableImage->qimage->convertDepth(IMAGE_DEPTH);
    } else {
        image = *immutableImage->qimage;
    }

    if (image.hasAlphaBuffer() == TRUE) {

        for (curY = y; curY < y + height; curY++) {
            for (curX = x; curX < x + width; curX++) {
                
                // Obtain the R,G,B
                pixel = image.pixel(curX, curY);
                
                r = qRed(pixel) ;
                g = qGreen(pixel);
                b = qBlue(pixel);
            
                rgbBuffer[curOffset] = (qAlpha(pixel) << 24)
                    | ((r & 0xff) << 16) 
                    | ((g & 0xff) << 8) 
                    |  (b & 0xff);
                
                curOffset++;
            }

            curOffset += (scanLength - width);
        }
        
    } else {
        
        for (curY = y; curY < y + height; curY++) {
            for (curX = x; curX < x + width; curX++) {
                
                // Obtain the R,G,B
                pixel = image.pixel(curX, curY);
                
                r = qRed(pixel) ;
                g = qGreen(pixel);
                b = qBlue(pixel);
                
                rgbBuffer[curOffset] = (0xff << 24)
                    | ((r & 0xff) << 16) 
                    | ((g & 0xff) << 8) 
                    |  (b & 0xff);
                
                curOffset++;
            }
            
            curOffset += (scanLength - width);
        }
        
    }

    *errorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR;
}
Exemple #7
0
int main( int argc, char **argv )
{
    if ( argc < 2 ) {
	qWarning( "Usage:\n\t%s [--images] files", argv[0] );
	return 1;
    }

    QFile output;
    bool output_hdr = FALSE;
    bool images = FALSE;
    output.open( IO_WriteOnly, stdout );
    QTextStream out( &output );

    QPtrList<EmbedImage> list_image;
    QPtrList<Embed> list;
    list.setAutoDelete( TRUE );
    list_image.setAutoDelete( TRUE );

    long l = rand();
    out << "#ifndef _QEMBED_" << l << endl;
    out << "#define _QEMBED_" << l << endl;

    QStringList args;
    for ( int i = 1; i < argc; ++i ) {
	QString file( argv[i] );
#ifdef Q_WS_WIN
	// Since wildcards are not expanded automatically for us on Windows, we need to do 
	// it ourselves
	if ( file.contains( '*' ) || file.contains( '?' ) ) {
	    QDir d;
	    const QFileInfoList *fiList = d.entryInfoList( file, QDir::Files );
	    QFileInfoListIterator it(*fiList);
	    while ( it.current() ) {
		args << (*it)->filePath();
		++it;
	    }
	} else
#endif
	    args << file;
    }
    for ( QStringList::Iterator it = args.begin(); it != args.end(); ++it ) {
	QString arg = (*it);
	if ( arg == "--images" ) {
	    if ( !images ) {
		out << "#include <qimage.h>\n";
		out << "#include <qdict.h>\n";
		images = TRUE;
	    }
	} else {
	    QFile f( *it );
	    if ( !f.open(IO_ReadOnly) ) {
		qWarning( "Cannot open file %s, ignoring it", (*it).latin1() );
		continue;
	    }
	    QByteArray a( f.size() );
	    if ( f.size() == 0
		 || f.readBlock(a.data(), f.size()) != (int)f.size() ) {
		qWarning( "Cannot read file %s, ignoring it", (*it).latin1() );
		continue;
	    }
	    if ( images ) {
		QImage img;
		if ( !img.loadFromData(a) ) {
		    qWarning( "Cannot read image from file %s, ignoring it", (*it).latin1() );
		    continue;
		}
		EmbedImage *e = new EmbedImage;
		e->width = img.width();
		e->height = img.height();
		e->depth = img.depth();
		e->numColors = img.numColors();
		e->colorTable = new QRgb[e->numColors];
		e->alpha = img.hasAlphaBuffer();
		memcpy(e->colorTable, img.colorTable(), e->numColors*sizeof(QRgb));
		QFileInfo fi( (*it) );
		e->name = fi.baseName();
		e->cname = convertFileNameToCIdentifier( e->name.latin1() );
		list_image.append( e );
		QString s;
		if ( e->depth == 32 ) {
		    out << s.sprintf( "static const QRgb %s_data[] = {",
				   (const char *)e->cname );
		    embedData( (QRgb*)img.bits(), e->width*e->height, &output );
		} else {
		    if ( e->depth == 1 )
			img = img.convertBitOrder(QImage::BigEndian);
		    out << s.sprintf( "static const unsigned char %s_data[] = {",
				   (const char *)e->cname );
		    embedData( img.bits(), img.numBytes(), &output );
		}
		out << "\n};\n\n";
		if ( e->numColors ) {
		    out << s.sprintf( "static const QRgb %s_ctable[] = {",
				   (const char *)e->cname );
		    embedData( e->colorTable, e->numColors, &output );
		    out << "\n};\n\n";
		}
	    } else {
		Embed *e = new Embed;
		e->size = f.size();
		e->name = (*it);
		e->cname = convertFileNameToCIdentifier( (*it) );
		list.append( e );
		QString s;
		out << s.sprintf( "static const unsigned int  %s_len = %d;\n",
			       (const char *)e->cname, e->size );
		out << s.sprintf( "static const unsigned char %s_data[] = {",
			       (const char *)e->cname );
		embedData( a, &output );
		out << "\n};\n\n";
	    }
	    if ( !output_hdr ) {
		output_hdr = TRUE;
		out << header;
	    }
	}
    }

    if ( list.count() > 0 ) {
	out << "#include <qcstring.h>\n";
	if ( !images )
	    out << "#include <qdict.h>\n";

	out << "static struct Embed {\n"
	       "    unsigned int size;\n"
	       "    const unsigned char *data;\n"
	       "    const char *name;\n"
	       "} embed_vec[] = {\n";
	Embed *e = list.first();
	while ( e ) {
	    out << "    { " << e->size << ", " << e->cname << "_data, "
		 << "\"" << e->name << "\" },\n";
	    e = list.next();
	}
	out << "    { 0, 0, 0 }\n};\n";

	out << "\n"
"static const QByteArray& qembed_findData( const char* name )\n"
"{\n"
"    static QDict<QByteArray> dict;\n"
"    QByteArray* ba = dict.find( name );\n"
"    if ( !ba ) {\n"
"	for ( int i = 0; embed_vec[i].data; i++ ) {\n"
"	    if ( strcmp(embed_vec[i].name, name) == 0 ) {\n"
"		ba = new QByteArray;\n"
"		ba->setRawData( (char*)embed_vec[i].data,\n"
"				embed_vec[i].size );\n"
"		dict.insert( name, ba );\n"
"		break;\n"
"	    }\n"
"	}\n"
"	if ( !ba ) {\n"
"	    static QByteArray dummy;\n"
"	    return dummy;\n"
"	}\n"
"    }\n"
"    return *ba;\n"
"}\n\n";
    }

    if ( list_image.count() > 0 ) {
	out << "static struct EmbedImage {\n"
	       "    int width, height, depth;\n"
	       "    const unsigned char *data;\n"
	       "    int numColors;\n"
	       "    const QRgb *colorTable;\n"
	       "    bool alpha;\n"
	       "    const char *name;\n"
	       "} embed_image_vec[] = {\n";
	EmbedImage *e = list_image.first();
	while ( e ) {
	    out << "    { "
		<< e->width << ", "
		<< e->height << ", "
		<< e->depth << ", "
		<< "(const unsigned char*)" << e->cname << "_data, "
		<< e->numColors << ", ";
	    if ( e->numColors )
		out << e->cname << "_ctable, ";
	    else
		out << "0, ";
	    if ( e->alpha )
		out << "TRUE, ";
	    else
		out << "FALSE, ";
	    out << "\"" << e->name << "\" },\n";
	    e = list_image.next();
	}
	out << "    { 0, 0, 0, 0, 0, 0, 0, 0 }\n};\n";

	out << "\n"
"static const QImage& qembed_findImage( const QString& name )\n"
"{\n"
"    static QDict<QImage> dict;\n"
"    QImage* img = dict.find( name );\n"
"    if ( !img ) {\n"
"	for ( int i = 0; embed_image_vec[i].data; i++ ) {\n"
"	    if ( strcmp(embed_image_vec[i].name, name.latin1()) == 0 ) {\n"
"		img = new QImage((uchar*)embed_image_vec[i].data,\n"
"			    embed_image_vec[i].width,\n"
"			    embed_image_vec[i].height,\n"
"			    embed_image_vec[i].depth,\n"
"			    (QRgb*)embed_image_vec[i].colorTable,\n"
"			    embed_image_vec[i].numColors,\n"
"			    QImage::BigEndian );\n"
"		if ( embed_image_vec[i].alpha )\n"
"		    img->setAlphaBuffer( TRUE );\n"
"		dict.insert( name, img );\n"
"		break;\n"
"	    }\n"
"	}\n"
"	if ( !img ) {\n"
"	    static QImage dummy;\n"
"	    return dummy;\n"
"	}\n"
"    }\n"
"    return *img;\n"
"}\n\n";
    }

    out << "#endif" << endl;

    return 0;
}