ImageWrapper * ImageFactory::newOrReuseImage() { int total = 1 + (Config::enablePreReading() ? 1 : 0) + CacheNumber; ImageWrapper *image; if(list.size() < total){ image = new ImageWrapper(); }else{ image = list.at(total - 1); list.removeAt(total - 1); waitForImageReady(image); // Without this, it will be crashed if more than one thread run image.load(). image->recycle(); image->setReady(false); image->setHashCode(ImageWrapper::HASH_INVALID); } return image; }
v8::Handle<v8::Object> ImageWrapper::WrapImage(v8::Isolate* isolate, const Dali::Image& image, ImageType imageType ) { v8::EscapableHandleScope handleScope( isolate ); v8::Local<v8::ObjectTemplate> objectTemplate; objectTemplate = GetImageTemplate( isolate, imageType); // create an instance of the template v8::Local<v8::Object> localObject = objectTemplate->NewInstance(); // create the Image wrapper ImageWrapper* pointer = new ImageWrapper( image, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); // assign the JavaScript object to the wrapper. // This also stores the Image object, in an internal field inside the JavaScript object. pointer->SetJavascriptObject( isolate, localObject ); return handleScope.Escape( localObject ); }
bool ImageSegmenter::_generateRaw( const ImageWrapper& image, const Handler& handler ) const { const SegmentParametersList& paramList = _generateSegmentParameters( image ); // resulting Raw segments for( SegmentParametersList::const_iterator it = paramList.begin(); it != paramList.end(); ++it ) { Segment segment; segment.parameters = *it; segment.imageData.reserve( segment.parameters.width * segment.parameters.height * image.getBytesPerPixel( )); if( paramList.size() == 1 ) { // If we are not segmenting the image, just append the image data segment.imageData.append( (const char*)image.data, int(image.getBufferSize( ))); } else // Copy the image subregion { // assume imageBuffer isn't padded const size_t imagePitch = image.width * image.getBytesPerPixel(); const size_t offset = segment.parameters.y * imagePitch + segment.parameters.x * image.getBytesPerPixel(); const char* lineData = (const char*)image.data + offset; for( unsigned int i = 0; i < segment.parameters.height; ++i ) { segment.imageData.append( lineData, segment.parameters.width * image.getBytesPerPixel( )); lineData += imagePitch; } } if( !handler( segment )) return false; } return true; }
QByteArray ImageJpegCompressor::computeJpeg(const ImageWrapper& sourceImage, const QRect& imageRegion) { // tjCompress API is incorrect and takes a non-const input buffer, even // though it does not modify it. It can "safely" be cast to non-const // pointer to comply to the incorrect API. unsigned char* tjSrcBuffer = (unsigned char*)sourceImage.data; tjSrcBuffer += imageRegion.y() * sourceImage.width * sourceImage.getBytesPerPixel(); tjSrcBuffer += imageRegion.x() * sourceImage.getBytesPerPixel(); const int tjWidth = imageRegion.width(); // assume imageBuffer isn't padded const int tjPitch = sourceImage.width * sourceImage.getBytesPerPixel(); const int tjHeight = imageRegion.height(); const int tjPixelFormat = getTurboJpegFormat(sourceImage.pixelFormat); unsigned char* tjJpegBuf = 0; unsigned long tjJpegSize = 0; const int tjJpegSubsamp = TJSAMP_444; const int tjJpegQual = sourceImage.compressionQuality; const int tjFlags = 0; // or: TJFLAG_BOTTOMUP int err = tjCompress2(_tjHandle, tjSrcBuffer, tjWidth, tjPitch, tjHeight, tjPixelFormat, &tjJpegBuf, &tjJpegSize, tjJpegSubsamp, tjJpegQual, tjFlags); if (err != 0) { std::cerr << "libjpeg-turbo image conversion failure" << std::endl; return QByteArray(); } // move the JPEG buffer to a byte array const QByteArray jpegData((char*)tjJpegBuf, tjJpegSize); // free the libjpeg-turbo allocated memory tjFree(tjJpegBuf); return jpegData; }