Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03k
1/*!2 Copyright 2013 Lovell Fuller and others.3 SPDX-License-Identifier: Apache-2.04*/5 6#include <cmath>7#include <numeric>8#include <string>9#include <utility>10#include <vector>11 12#include <napi.h>13#include <vips/vips8>14 15#include "./common.h"16#include "./metadata.h"17 18static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p);19 20class MetadataWorker : public Napi::AsyncWorker {21 public:22 MetadataWorker(Napi::Function callback, MetadataBaton *baton, Napi::Function debuglog) :23 Napi::AsyncWorker(callback), baton(baton), debuglog(Napi::Persistent(debuglog)) {}24 ~MetadataWorker() {}25 26 void Execute() {27 // Decrement queued task counter28 sharp::counterQueue--;29 30 vips::VImage image;31 sharp::ImageType imageType = sharp::ImageType::UNKNOWN;32 try {33 std::tie(image, imageType) = OpenInput(baton->input);34 } catch (std::runtime_error const &err) {35 (baton->err).append(err.what());36 }37 if (imageType != sharp::ImageType::UNKNOWN) {38 // Image type39 baton->format = sharp::ImageTypeId(imageType);40 // VipsImage attributes41 baton->width = image.width();42 baton->height = image.height();43 baton->space = vips_enum_nick(VIPS_TYPE_INTERPRETATION, image.interpretation());44 baton->channels = image.bands();45 baton->depth = vips_enum_nick(VIPS_TYPE_BAND_FORMAT, image.format());46 if (sharp::HasDensity(image)) {47 baton->density = sharp::GetDensity(image);48 }49 if (image.get_typeof("jpeg-chroma-subsample") == VIPS_TYPE_REF_STRING) {50 baton->chromaSubsampling = image.get_string("jpeg-chroma-subsample");51 }52 if (image.get_typeof("interlaced") == G_TYPE_INT) {53 baton->isProgressive = image.get_int("interlaced") == 1;54 }55 if (image.get_typeof(VIPS_META_PALETTE) == G_TYPE_INT) {56 baton->isPalette = image.get_int(VIPS_META_PALETTE);57 }58 if (image.get_typeof(VIPS_META_BITS_PER_SAMPLE) == G_TYPE_INT) {59 baton->bitsPerSample = image.get_int(VIPS_META_BITS_PER_SAMPLE);60 }61 if (image.get_typeof(VIPS_META_N_PAGES) == G_TYPE_INT) {62 baton->pages = image.get_int(VIPS_META_N_PAGES);63 }64 if (image.get_typeof(VIPS_META_PAGE_HEIGHT) == G_TYPE_INT) {65 baton->pageHeight = image.get_int(VIPS_META_PAGE_HEIGHT);66 }67 if (image.get_typeof("loop") == G_TYPE_INT) {68 baton->loop = image.get_int("loop");69 }70 if (image.get_typeof("delay") == VIPS_TYPE_ARRAY_INT) {71 baton->delay = image.get_array_int("delay");72 }73 if (image.get_typeof("heif-primary") == G_TYPE_INT) {74 baton->pagePrimary = image.get_int("heif-primary");75 }76 if (image.get_typeof("heif-compression") == VIPS_TYPE_REF_STRING) {77 baton->compression = image.get_string("heif-compression");78 }79 if (image.get_typeof(VIPS_META_RESOLUTION_UNIT) == VIPS_TYPE_REF_STRING) {80 baton->resolutionUnit = image.get_string(VIPS_META_RESOLUTION_UNIT);81 }82 if (image.get_typeof("magick-format") == VIPS_TYPE_REF_STRING) {83 baton->formatMagick = image.get_string("magick-format");84 }85 if (image.get_typeof("openslide.level-count") == VIPS_TYPE_REF_STRING) {86 int const levels = std::stoi(image.get_string("openslide.level-count"));87 for (int l = 0; l < levels; l++) {88 std::string prefix = "openslide.level[" + std::to_string(l) + "].";89 int const width = std::stoi(image.get_string((prefix + "width").data()));90 int const height = std::stoi(image.get_string((prefix + "height").data()));91 baton->levels.push_back(std::pair<int, int>(width, height));92 }93 }94 if (image.get_typeof(VIPS_META_N_SUBIFDS) == G_TYPE_INT) {95 baton->subifds = image.get_int(VIPS_META_N_SUBIFDS);96 }97 baton->hasProfile = sharp::HasProfile(image);98 if (image.get_typeof("background") == VIPS_TYPE_ARRAY_DOUBLE) {99 baton->background = image.get_array_double("background");100 }101 // Derived attributes102 baton->hasAlpha = image.has_alpha();103 baton->orientation = sharp::ExifOrientation(image);104 // EXIF105 if (image.get_typeof(VIPS_META_EXIF_NAME) == VIPS_TYPE_BLOB) {106 size_t exifLength;107 void const *exif = image.get_blob(VIPS_META_EXIF_NAME, &exifLength);108 baton->exif = static_cast<char*>(g_malloc(exifLength));109 memcpy(baton->exif, exif, exifLength);110 baton->exifLength = exifLength;111 }112 // ICC profile113 if (image.get_typeof(VIPS_META_ICC_NAME) == VIPS_TYPE_BLOB) {114 size_t iccLength;115 void const *icc = image.get_blob(VIPS_META_ICC_NAME, &iccLength);116 baton->icc = static_cast<char*>(g_malloc(iccLength));117 memcpy(baton->icc, icc, iccLength);118 baton->iccLength = iccLength;119 }120 // IPTC121 if (image.get_typeof(VIPS_META_IPTC_NAME) == VIPS_TYPE_BLOB) {122 size_t iptcLength;123 void const *iptc = image.get_blob(VIPS_META_IPTC_NAME, &iptcLength);124 baton->iptc = static_cast<char *>(g_malloc(iptcLength));125 memcpy(baton->iptc, iptc, iptcLength);126 baton->iptcLength = iptcLength;127 }128 // XMP129 if (image.get_typeof(VIPS_META_XMP_NAME) == VIPS_TYPE_BLOB) {130 size_t xmpLength;131 void const *xmp = image.get_blob(VIPS_META_XMP_NAME, &xmpLength);132 baton->xmp = static_cast<char *>(g_malloc(xmpLength));133 memcpy(baton->xmp, xmp, xmpLength);134 baton->xmpLength = xmpLength;135 }136 // TIFFTAG_PHOTOSHOP137 if (image.get_typeof(VIPS_META_PHOTOSHOP_NAME) == VIPS_TYPE_BLOB) {138 size_t tifftagPhotoshopLength;139 void const *tifftagPhotoshop = image.get_blob(VIPS_META_PHOTOSHOP_NAME, &tifftagPhotoshopLength);140 baton->tifftagPhotoshop = static_cast<char *>(g_malloc(tifftagPhotoshopLength));141 memcpy(baton->tifftagPhotoshop, tifftagPhotoshop, tifftagPhotoshopLength);142 baton->tifftagPhotoshopLength = tifftagPhotoshopLength;143 }144 // Gain Map145 if (image.get_typeof("gainmap-data") == VIPS_TYPE_BLOB) {146 size_t gainMapLength;147 void const *gainMap = image.get_blob("gainmap-data", &gainMapLength);148 baton->gainMap = static_cast<char *>(g_malloc(gainMapLength));149 memcpy(baton->gainMap, gainMap, gainMapLength);150 baton->gainMapLength = gainMapLength;151 }152 // PNG comments153 vips_image_map(image.get_image(), readPNGComment, &baton->comments);154 // Media type155 std::string mediaType;156 switch (imageType) {157 case sharp::ImageType::JPEG:158 case sharp::ImageType::PNG:159 case sharp::ImageType::WEBP:160 case sharp::ImageType::JP2:161 case sharp::ImageType::TIFF:162 case sharp::ImageType::GIF:163 case sharp::ImageType::FITS:164 case sharp::ImageType::JXL:165 baton->mediaType = "image/" + baton->format;166 break;167 case sharp::ImageType::SVG:168 baton->mediaType = "image/svg+xml";169 break;170 case sharp::ImageType::HEIF:171 if (baton->compression == "av1") {172 baton->mediaType = "image/avif";173 } else if (baton->compression == "hevc") {174 baton->mediaType = "image/heic";175 }176 break;177 case sharp::ImageType::PDF:178 baton->mediaType = "application/pdf";179 break;180 case sharp::ImageType::OPENSLIDE:181 baton->mediaType = "image/tiff";182 break;183 case sharp::ImageType::PPM:184 baton->mediaType = "image/x-portable-pixmap";185 break;186 case sharp::ImageType::EXR:187 baton->mediaType = "image/x-exr";188 break;189 case sharp::ImageType::RAD:190 baton->mediaType = "image/vnd.radiance";191 break;192 case sharp::ImageType::UHDR:193 baton->mediaType = "image/jpeg";194 break;195 default:196 break;197 }198 }199 200 // Clean up201 vips_error_clear();202 vips_thread_shutdown();203 }204 205 void OnOK() {206 Napi::Env env = Env();207 Napi::HandleScope scope(env);208 209 // Handle warnings210 std::string warning = sharp::VipsWarningPop();211 while (!warning.empty()) {212 debuglog.SHARP_CALLBACK_FN_NAME(Receiver().Value(), { Napi::String::New(env, warning) });213 warning = sharp::VipsWarningPop();214 }215 216 if (baton->err.empty()) {217 Napi::Object info = Napi::Object::New(env);218 info.Set("format", baton->format);219 if (!baton->mediaType.empty()) {220 info.Set("mediaType", baton->mediaType);221 }222 if (baton->input->bufferLength > 0) {223 info.Set("size", baton->input->bufferLength);224 }225 info.Set("width", baton->width);226 info.Set("height", baton->height);227 info.Set("space", baton->space);228 info.Set("channels", baton->channels);229 info.Set("depth", baton->depth);230 if (baton->density > 0) {231 info.Set("density", baton->density);232 }233 if (!baton->chromaSubsampling.empty()) {234 info.Set("chromaSubsampling", baton->chromaSubsampling);235 }236 info.Set("isProgressive", baton->isProgressive);237 info.Set("isPalette", baton->isPalette);238 if (baton->bitsPerSample > 0) {239 info.Set("bitsPerSample", baton->bitsPerSample);240 }241 if (baton->pages > 0) {242 info.Set("pages", baton->pages);243 }244 if (baton->pageHeight > 0) {245 info.Set("pageHeight", baton->pageHeight);246 }247 if (baton->loop >= 0) {248 info.Set("loop", baton->loop);249 }250 if (!baton->delay.empty()) {251 int i = 0;252 Napi::Array delay = Napi::Array::New(env, static_cast<size_t>(baton->delay.size()));253 for (int const d : baton->delay) {254 delay.Set(i++, d);255 }256 info.Set("delay", delay);257 }258 if (baton->pagePrimary > -1) {259 info.Set("pagePrimary", baton->pagePrimary);260 }261 if (!baton->compression.empty()) {262 info.Set("compression", baton->compression);263 }264 if (!baton->resolutionUnit.empty()) {265 info.Set("resolutionUnit", baton->resolutionUnit == "in" ? "inch" : baton->resolutionUnit);266 }267 if (!baton->formatMagick.empty()) {268 info.Set("formatMagick", baton->formatMagick);269 }270 if (!baton->levels.empty()) {271 int i = 0;272 Napi::Array levels = Napi::Array::New(env, static_cast<size_t>(baton->levels.size()));273 for (const auto& [width, height] : baton->levels) {274 Napi::Object level = Napi::Object::New(env);275 level.Set("width", width);276 level.Set("height", height);277 levels.Set(i++, level);278 }279 info.Set("levels", levels);280 }281 if (baton->subifds > 0) {282 info.Set("subifds", baton->subifds);283 }284 if (!baton->background.empty()) {285 Napi::Object background = Napi::Object::New(env);286 if (baton->background.size() == 3) {287 background.Set("r", baton->background[0]);288 background.Set("g", baton->background[1]);289 background.Set("b", baton->background[2]);290 } else {291 background.Set("gray", round(baton->background[0] * 100 / 255));292 }293 info.Set("background", background);294 }295 info.Set("hasProfile", baton->hasProfile);296 info.Set("hasAlpha", baton->hasAlpha);297 if (baton->orientation > 0) {298 info.Set("orientation", baton->orientation);299 }300 Napi::Object autoOrient = Napi::Object::New(env);301 info.Set("autoOrient", autoOrient);302 if (baton->orientation >= 5) {303 autoOrient.Set("width", baton->height);304 autoOrient.Set("height", baton->width);305 } else {306 autoOrient.Set("width", baton->width);307 autoOrient.Set("height", baton->height);308 }309 if (baton->exifLength > 0) {310 info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback));311 }312 if (baton->iccLength > 0) {313 info.Set("icc", Napi::Buffer<char>::NewOrCopy(env, baton->icc, baton->iccLength, sharp::FreeCallback));314 }315 if (baton->iptcLength > 0) {316 info.Set("iptc", Napi::Buffer<char>::NewOrCopy(env, baton->iptc, baton->iptcLength, sharp::FreeCallback));317 }318 if (baton->xmpLength > 0) {319 if (g_utf8_validate(static_cast<char const *>(baton->xmp), baton->xmpLength, nullptr)) {320 info.Set("xmpAsString",321 Napi::String::New(env, static_cast<char const *>(baton->xmp), baton->xmpLength));322 }323 info.Set("xmp", Napi::Buffer<char>::NewOrCopy(env, baton->xmp, baton->xmpLength, sharp::FreeCallback));324 }325 if (baton->tifftagPhotoshopLength > 0) {326 info.Set("tifftagPhotoshop",327 Napi::Buffer<char>::NewOrCopy(env, baton->tifftagPhotoshop,328 baton->tifftagPhotoshopLength, sharp::FreeCallback));329 }330 if (baton->gainMapLength > 0) {331 Napi::Object gainMap = Napi::Object::New(env);332 info.Set("gainMap", gainMap);333 gainMap.Set("image",334 Napi::Buffer<char>::NewOrCopy(env, baton->gainMap, baton->gainMapLength, sharp::FreeCallback));335 }336 if (baton->comments.size() > 0) {337 int i = 0;338 Napi::Array comments = Napi::Array::New(env, baton->comments.size());339 for (const auto& [keyword, text] : baton->comments) {340 Napi::Object comment = Napi::Object::New(env);341 comment.Set("keyword", keyword);342 comment.Set("text", text);343 comments.Set(i++, comment);344 }345 info.Set("comments", comments);346 }347 Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(), { env.Null(), info });348 } else {349 Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(),350 { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });351 }352 353 delete baton->input;354 delete baton;355 }356 357 private:358 MetadataBaton* baton;359 Napi::FunctionReference debuglog;360};361 362/*363 metadata(options, callback)364*/365Napi::Value metadata(const Napi::CallbackInfo& info) {366 // V8 objects are converted to non-V8 types held in the baton struct367 MetadataBaton *baton = new MetadataBaton;368 Napi::Object options = info[size_t(0)].As<Napi::Object>();369 370 // Input371 baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>());372 373 // Function to notify of libvips warnings374 Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>();375 376 // Join queue for worker thread377 Napi::Function callback = info[size_t(1)].As<Napi::Function>();378 MetadataWorker *worker = new MetadataWorker(callback, baton, debuglog);379 worker->Receiver().Set("options", options);380 worker->Queue();381 382 // Increment queued task counter383 sharp::counterQueue++;384 385 return info.Env().Undefined();386}387 388const char *PNG_COMMENT_START = "png-comment-";389const int PNG_COMMENT_START_LEN = strlen(PNG_COMMENT_START);390 391static void* readPNGComment(VipsImage *image, const char *field, GValue *value, void *p) {392 MetadataComments *comments = static_cast<MetadataComments *>(p);393 394 if (vips_isprefix(PNG_COMMENT_START, field)) {395 const char *keyword = strchr(field + PNG_COMMENT_START_LEN, '-');396 const char *str;397 if (keyword != NULL && !vips_image_get_string(image, field, &str)) {398 keyword++; // Skip the hyphen399 comments->push_back(std::make_pair(keyword, str));400 }401 }402 403 return NULL;404}405 