void ImageSource::loadDescription(FileSource& fileSource) { if (!url) { loaded = true; } if (req || loaded) { return; } const Resource imageResource { Resource::Image, *url, {}, Resource::Necessity::Required }; req = fileSource.request(imageResource, [this](Response res) { if (res.error) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); } else if (res.notModified) { return; } else if (res.noContent) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty image url"))); } else { try { UnassociatedImage image = util::unpremultiply(decodeImage(*res.data)); baseImpl = makeMutable<Impl>(impl(), std::move(image)); } catch (...) { observer->onSourceError(*this, std::current_exception()); } loaded = true; observer->onSourceLoaded(*this); } }); }
static DFBResult IDirectFBImageProvider_ANDROID_GetSurfaceDescription( IDirectFBImageProvider *thiz, DFBSurfaceDescription *desc ) { DFBResult ret; DFBSurfacePixelFormat primary_format = dfb_primary_layer_pixelformat(); DIRECT_INTERFACE_GET_DATA (IDirectFBImageProvider_ANDROID) if (!desc) return DFB_INVARG; ret = decodeImage( data ); if (ret) return ret; desc->flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT; if (data->alpha) desc->pixelformat = DFB_PIXELFORMAT_HAS_ALPHA(primary_format) ? primary_format : DSPF_ARGB; //desc->pixelformat = DSPF_ABGR; else desc->pixelformat = primary_format; desc->width = data->width; desc->height = data->height; D_DEBUG_AT( imageProviderANDROID, "GetSurfaceDescription: width=%d height=%d pitch=%d has_alpha=%d pixelformat=%s/%s\n", data->width, data->height, data->pitch, data->alpha, dfb_pixelformat_name(data->format), dfb_pixelformat_name(desc->pixelformat) ); return DFB_OK; }
static DFBResult IDirectFBImageProvider_ANDROID_GetImageDescription( IDirectFBImageProvider *thiz, DFBImageDescription *desc ) { DFBResult ret; DIRECT_INTERFACE_GET_DATA(IDirectFBImageProvider_ANDROID) if (!desc) return DFB_INVARG; ret = decodeImage( data ); if (ret) return ret; desc->caps = DICAPS_NONE; if (data->alpha) desc->caps |= DICAPS_ALPHACHANNEL; D_DEBUG_AT( imageProviderANDROID, "GetImageDescription: width=%d height=%d pitch=%d has_alpha=%d pixelformat=%s\n", data->width, data->height, data->pitch, data->alpha, dfb_pixelformat_name(data->format) ); return DFB_OK; }
std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& encodedImage, const std::string& json) { const PremultipliedImage raster = decodeImage(encodedImage); JSDocument doc; doc.Parse<0>(json.c_str()); if (doc.HasParseError()) { std::stringstream message; message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset(); throw std::runtime_error(message.str()); } else if (!doc.IsObject()) { throw std::runtime_error("Sprite JSON root must be an object"); } else { std::vector<std::unique_ptr<style::Image>> images; for (const auto& property : doc.GetObject()) { const std::string name = { property.name.GetString(), property.name.GetStringLength() }; const JSValue& value = property.value; if (value.IsObject()) { const uint16_t x = getUInt16(value, "x", 0); const uint16_t y = getUInt16(value, "y", 0); const uint16_t width = getUInt16(value, "width", 0); const uint16_t height = getUInt16(value, "height", 0); const double pixelRatio = getDouble(value, "pixelRatio", 1); const bool sdf = getBoolean(value, "sdf", false); auto image = createStyleImage(name, raster, x, y, width, height, pixelRatio, sdf); if (image) { images.push_back(std::move(image)); } } } return images; } }
void checkImage(const std::string& base, const PremultipliedImage& actual, double imageThreshold, double pixelThreshold) { if (getenv("UPDATE")) { util::write_file(base + "/expected.png", encodePNG(actual)); return; } PremultipliedImage expected = decodeImage(util::read_file(base + "/expected.png")); PremultipliedImage diff { expected.width, expected.height }; ASSERT_EQ(expected.width, actual.width); ASSERT_EQ(expected.height, actual.height); double pixels = mapbox::pixelmatch(actual.data.get(), expected.data.get(), expected.width, expected.height, diff.data.get(), pixelThreshold); EXPECT_LE(pixels / (expected.width * expected.height), imageThreshold); util::write_file(base + "/actual.png", encodePNG(actual)); util::write_file(base + "/diff.png", encodePNG(diff)); }
int main(int argc, char *argv[]) { OPENMAX_JPEG_DECODER *pDecoder; char *sourceImage; size_t imageSize; int s; if (argc < 2) { printf("Usage: %s <filename>\n", argv[0]); return -1; } FILE *fp = fopen(argv[1], "rb"); if (!fp) { printf("File %s not found.\n", argv[1]); } fseek(fp, 0L, SEEK_END); imageSize = ftell(fp); fseek(fp, 0L, SEEK_SET); sourceImage = malloc(imageSize); assert(sourceImage != NULL); s = fread(sourceImage, 1, imageSize, fp); assert(s == imageSize); fclose(fp); bcm_host_init(); s = setupOpenMaxJpegDecoder(&pDecoder); assert(s == 0); s = decodeImage(pDecoder, sourceImage, imageSize); assert(s == 0); cleanup(pDecoder); free(sourceImage); return 0; }
static bool decodeDataObject(ArgumentDecoder* decoder, RefPtr<DataObjectGtk>& dataObject) { RefPtr<DataObjectGtk> data = DataObjectGtk::create(); bool hasText; if (!decoder->decode(hasText)) return false; if (hasText) { String text; if (!decoder->decode(text)) return false; data->setText(text); } bool hasMarkup; if (!decoder->decode(hasMarkup)) return false; if (hasMarkup) { String markup; if (!decoder->decode(markup)) return false; data->setMarkup(markup); } bool hasURL; if (!decoder->decode(hasURL)) return false; if (hasURL) { String url; if (!decoder->decode(url)) return false; data->setURL(KURL(KURL(), url), String()); } bool hasURIList; if (!decoder->decode(hasURIList)) return false; if (hasURIList) { String uriList; if (!decoder->decode(uriList)) return false; data->setURIList(uriList); } bool hasImage; if (!decoder->decode(hasImage)) return false; if (hasImage) { GRefPtr<GdkPixbuf> image; if (!decodeImage(decoder, image)) return false; data->setImage(image.get()); } dataObject = data; return true; }
QQrDecoder::QQrDecoder(QWidget *parent): QMainWindow(parent), decoder(this) { ui.setupUi(this); connect(ui.centralwidget, SIGNAL(imageCaptured(QImage)), this, SLOT(decodeImage(QImage))); connect(&decoder, SIGNAL(tagFound(QString)), this, SLOT(reportTagFound(QString))); //by default all decoders available are enabled //decoder.setDecoder( QZXing::DecoderFormat_DATA_MATRIX); //decoder.setDecoder( QZXing::DecoderFormat_Aztec ); }
bool ArgumentCoder<PasteboardImage>::decode(ArgumentDecoder& decoder, WebCore::PasteboardImage& pasteboardImage) { if (!decodeImage(decoder, pasteboardImage.image)) return false; if (!decoder.decode(pasteboardImage.url.url)) return false; if (!decoder.decode(pasteboardImage.url.title)) return false; if (!decoder.decode(pasteboardImage.resourceMIMEType)) return false; if (!decodeSharedBuffer(decoder, pasteboardImage.resourceData)) return false; return true; }
void parseRasterTile(std::unique_ptr<RasterBucket> bucket, const std::shared_ptr<const std::string> data, std::function<void(RasterTileParseResult)> callback) { PremultipliedImage image; try { image = decodeImage(*data); } catch (...) { callback(RasterTileParseResult("error parsing raster image")); return; } bucket->setImage(std::move(image)); callback(RasterTileParseResult(std::move(bucket))); }
QtBarcodeReader::QtBarcodeReader(QObject *parent,QWidget *viewer) : QObject(parent) { qRegisterMetaType<cv::Mat>("cv::Mat"); converter.setProcessAll(false); cvcapture= new CVCapture(); converter.connect(cvcapture, SIGNAL(matReady(cv::Mat)), SLOT(processFrame(cv::Mat))); BarcodeDecoder.connect(&converter, SIGNAL(imageReady(QImage)), SLOT(decodeImage(QImage))); connect(&BarcodeDecoder,SIGNAL(BarcodeDecodeStatus(BARCODESTATUS,QString)),this, SLOT(NewBarcodeDecodeStatus(BARCODESTATUS,QString))); if (viewer!=0) { viewer->connect(&converter, SIGNAL(imageReady(QImage)), SLOT(setImage(QImage))); } }
SpriteParseResult parseSprite(const std::string& image, const std::string& json) { Sprites sprites; PremultipliedImage raster; try { raster = decodeImage(image); } catch (...) { return std::current_exception(); } JSDocument doc; doc.Parse<0>(json.c_str()); if (doc.HasParseError()) { std::stringstream message; message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset(); return std::make_exception_ptr(std::runtime_error(message.str())); } else if (!doc.IsObject()) { return std::make_exception_ptr(std::runtime_error("Sprite JSON root must be an object")); } else { for (JSValue::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) { const std::string name = { itr->name.GetString(), itr->name.GetStringLength() }; const JSValue& value = itr->value; if (value.IsObject()) { const uint16_t x = getUInt16(value, "x", 0); const uint16_t y = getUInt16(value, "y", 0); const uint16_t width = getUInt16(value, "width", 0); const uint16_t height = getUInt16(value, "height", 0); const double pixelRatio = getDouble(value, "pixelRatio", 1); const bool sdf = getBoolean(value, "sdf", false); auto sprite = createSpriteImage(raster, x, y, width, height, pixelRatio, sdf); if (sprite) { sprites.emplace(name, sprite); } } } } return sprites; }
bool ArgumentCoder<Cursor>::decode(ArgumentDecoder& decoder, Cursor& cursor) { Cursor::Type type; if (!decoder.decodeEnum(type)) return false; if (type > Cursor::Custom) return false; if (type != Cursor::Custom) { const Cursor& cursorReference = Cursor::fromType(type); // Calling platformCursor here will eagerly create the platform cursor for the cursor singletons inside WebCore. // This will avoid having to re-create the platform cursors over and over. (void)cursorReference.platformCursor(); cursor = cursorReference; return true; } bool isValidImagePresent; if (!decoder.decode(isValidImagePresent)) return false; if (!isValidImagePresent) { cursor = Cursor(Image::nullImage(), IntPoint()); return true; } RefPtr<Image> image; if (!decodeImage(decoder, image)) return false; IntPoint hotSpot; if (!decoder.decode(hotSpot)) return false; if (!image->rect().contains(hotSpot)) return false; cursor = Cursor(image.get(), hotSpot); return true; }
CachedPage::UnitPtr Jpeg2000Pager::populateImageData(const DimensionDescriptor& startRow, const DimensionDescriptor& startColumn, unsigned int concurrentRows, unsigned int concurrentColumns) const { VERIFYRV(startRow.isOnDiskNumberValid() == true, CachedPage::UnitPtr()); VERIFYRV(startColumn.isOnDiskNumberValid() == true, CachedPage::UnitPtr()); VERIFYRV(concurrentRows > 0, CachedPage::UnitPtr()); VERIFYRV(concurrentColumns > 0, CachedPage::UnitPtr()); // Get the rows, colums, and bands to load unsigned int onDiskStartRow = startRow.getOnDiskNumber(); unsigned int onDiskStopRow = onDiskStartRow + concurrentRows; unsigned int onDiskStartColumn = startColumn.getOnDiskNumber(); unsigned int onDiskStopColumn = onDiskStartColumn + concurrentColumns; const RasterElement* pRaster = getRasterElement(); VERIFYRV(pRaster != NULL, CachedPage::UnitPtr()); const RasterDataDescriptor* pDescriptor = dynamic_cast<const RasterDataDescriptor*>(pRaster->getDataDescriptor()); VERIFYRV(pDescriptor != NULL, CachedPage::UnitPtr()); const RasterFileDescriptor* pFileDescriptor = dynamic_cast<const RasterFileDescriptor*>(pDescriptor->getFileDescriptor()); VERIFYRV(pFileDescriptor != NULL, CachedPage::UnitPtr()); const std::vector<DimensionDescriptor>& allBands = pFileDescriptor->getBands(); if (allBands.empty() == true) { return CachedPage::UnitPtr(); } // Create the output data unsigned int numPixels = concurrentRows * concurrentColumns * allBands.size(); unsigned int numBytes = numPixels * getBytesPerBand(); if (numPixels > static_cast<unsigned int>(std::numeric_limits<int>::max())) // ArrayResource only allocates up // to INT_MAX number of values { return CachedPage::UnitPtr(); } ArrayResource<Out> pDestination(numPixels, true); char* pDest = reinterpret_cast<char*>(pDestination.get()); if (pDest == NULL) { return CachedPage::UnitPtr(); } memset(pDest, 0, numPixels); // Decode the image from the file, first trying the codestream format then the file format opj_image_t* pImage = decodeImage(onDiskStartRow, onDiskStartColumn, onDiskStopRow, onDiskStopColumn, Jpeg2000Utilities::J2K_CFMT); if (pImage == NULL) { pImage = decodeImage(onDiskStartRow, onDiskStartColumn, onDiskStopRow, onDiskStopColumn, Jpeg2000Utilities::JP2_CFMT); } if (pImage == NULL) { return CachedPage::UnitPtr(); } // Populate the output image data int bandFactor = 1; std::string filename = pRaster->getFilename(); if (filename.empty() == false) { QStringList parts = QString::fromStdString(filename).split('.'); foreach (QString part, parts) { bool error; EncodingType dataType = StringUtilities::fromXmlString<EncodingType>(part.toStdString(), &error); if (dataType.isValid() == true && error == false) { int currentBandFactor = Jpeg2000Utilities::get_num_bands(dataType); if (currentBandFactor > 0) { bandFactor = currentBandFactor; break; } } }
int decodeResizeImage(char const *filename, int maxPixels, Bitmap* bitmap) { LOGI("decodeResizeImage. START"); int returnCode; int maxWidth; int maxHeight; // Decode Image returnCode = decodeImage(filename, bitmap); if (returnCode != MEMORY_OK) { LOGE("Failed to decode image"); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return returnCode; } LOGI("decodeResizeImage. IMAGE DECODED"); doTransforms(bitmap, 1, 0, 0); // Resize red channel getScaledSize((*bitmap).redWidth, (*bitmap).redHeight, maxPixels, &maxWidth, &maxHeight); //We only need to do this once as r, g, b should be the same sizes returnCode = resizeChannel(&(*bitmap).red, (*bitmap).redWidth, (*bitmap).redHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { freeUnsignedCharArray(&(*bitmap).red); return returnCode; } // Set red channel dimensions if ((*bitmap).redWidth >= maxWidth && (*bitmap).redHeight >= maxHeight) { (*bitmap).redWidth = maxWidth; (*bitmap).redHeight = maxHeight; } LOGI("decodeResizeImage. RED COMPLETE"); /** * GREEN CHANNEL */ doTransforms(bitmap, 0, 1, 0); // Resize green channel returnCode = resizeChannel(&(*bitmap).green, (*bitmap).greenWidth, (*bitmap).greenHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); return returnCode; } // Set green channel dimensions if ((*bitmap).greenWidth >= maxWidth && (*bitmap).greenHeight >= maxHeight) { (*bitmap).greenWidth = maxWidth; (*bitmap).greenHeight = maxHeight; } LOGI("decodeResizeImage. GREEN COMPLETE"); /** * BLUE CHANNEL */ doTransforms(bitmap, 0, 0, 1); // Resize blue channel returnCode = resizeChannel(&(*bitmap).blue, (*bitmap).blueWidth, (*bitmap).blueHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return returnCode; } // Set blue channel dimensions if ((*bitmap).blueWidth >= maxWidth && (*bitmap).blueHeight >= maxHeight) { (*bitmap).blueWidth = maxWidth; (*bitmap).blueHeight = maxHeight; } LOGI("decodeResizeImage. BLUE COMPLETE"); // Set the final bitmap dimensions if ((*bitmap).redWidth == (*bitmap).greenWidth && (*bitmap).redWidth == (*bitmap).blueWidth && (*bitmap).redHeight == (*bitmap).greenHeight && (*bitmap).redHeight == (*bitmap).blueHeight) { (*bitmap).width = (*bitmap).redWidth; (*bitmap).height = (*bitmap).redHeight; } else { freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return INCONSISTENT_BITMAP_ERROR; } LOGI("decodeResizeImage. FINISHED"); return MEMORY_OK; }
static DFBResult IDirectFBImageProvider_ANDROID_RenderTo( IDirectFBImageProvider *thiz, IDirectFBSurface *destination, const DFBRectangle *dest_rect ) { DFBResult ret; bool direct = false; DFBRegion clip; DFBRectangle rect; DFBSurfacePixelFormat format; IDirectFBSurface_data *dst_data; CoreSurface *dst_surface; CoreSurfaceBufferLock lock; DIRenderCallbackResult cb_result = DIRCR_OK; DIRECT_INTERFACE_GET_DATA(IDirectFBImageProvider_ANDROID) dst_data = (IDirectFBSurface_data*) destination->priv; if (!dst_data) return DFB_DEAD; dst_surface = dst_data->surface; if (!dst_surface) return DFB_DESTROYED; ret = decodeImage( data ); if (ret) return ret; ret = destination->GetPixelFormat( destination, &format ); if (ret) return ret; dfb_region_from_rectangle( &clip, &dst_data->area.current ); if (dest_rect) { if (dest_rect->w < 1 || dest_rect->h < 1) return DFB_INVARG; rect = *dest_rect; rect.x += dst_data->area.wanted.x; rect.y += dst_data->area.wanted.y; if (!dfb_rectangle_region_intersects( &rect, &clip )) return DFB_OK; } else { rect = dst_data->area.wanted; } ret = dfb_surface_lock_buffer( dst_surface, CSBR_BACK, CSAID_CPU, CSAF_WRITE, &lock ); if (ret) return ret; dfb_scale_linear_32( (u32 *)data->image, data->width, data->height, lock.addr, lock.pitch, &rect, dst_surface, &clip ); if (data->base.render_callback) { DFBRectangle r = { 0, 0, data->width, data->height }; data->base.render_callback( &r, data->base.render_callback_context ); } dfb_surface_unlock_buffer( dst_surface, &lock ); return DFB_OK; }
static bool decodeDataObject(ArgumentDecoder& decoder, RefPtr<DataObjectGtk>& dataObject) { RefPtr<DataObjectGtk> data = DataObjectGtk::create(); bool hasText; if (!decoder.decode(hasText)) return false; if (hasText) { String text; if (!decoder.decode(text)) return false; data->setText(text); } bool hasMarkup; if (!decoder.decode(hasMarkup)) return false; if (hasMarkup) { String markup; if (!decoder.decode(markup)) return false; data->setMarkup(markup); } bool hasURL; if (!decoder.decode(hasURL)) return false; if (hasURL) { String url; if (!decoder.decode(url)) return false; data->setURL(URL(URL(), url), String()); } bool hasURIList; if (!decoder.decode(hasURIList)) return false; if (hasURIList) { String uriList; if (!decoder.decode(uriList)) return false; data->setURIList(uriList); } bool hasImage; if (!decoder.decode(hasImage)) return false; if (hasImage) { GRefPtr<GdkPixbuf> image; if (!decodeImage(decoder, image)) return false; data->setImage(image.get()); } bool hasUnknownTypeData; if (!decoder.decode(hasUnknownTypeData)) return false; if (hasUnknownTypeData) { HashMap<String, String> unknownTypes; if (!decoder.decode(unknownTypes)) return false; auto end = unknownTypes.end(); for (auto it = unknownTypes.begin(); it != end; ++it) data->setUnknownTypeData(it->key, it->value); } dataObject = data; return true; }