libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

file-jxl.cc (5689B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #include <string.h>
      7 
      8 #include <string>
      9 
     10 #include "plugins/gimp/common.h"
     11 #include "plugins/gimp/file-jxl-load.h"
     12 #include "plugins/gimp/file-jxl-save.h"
     13 
     14 namespace jxl {
     15 namespace {
     16 
     17 constexpr char kLoadProc[] = "file-jxl-load";
     18 constexpr char kSaveProc[] = "file-jxl-save";
     19 
     20 void Query() {
     21   {
     22     static char run_mode_name[] = "run-mode";
     23     static char run_mode_description[] = "Run mode";
     24     static char filename_name[] = "filename";
     25     static char filename_description[] = "The name of the file to load";
     26     static char raw_filename_name[] = "raw-filename";
     27     static char raw_filename_description[] =
     28         "The name of the file, as entered by the user";
     29     static const GimpParamDef load_args[] = {
     30         {GIMP_PDB_INT32, run_mode_name, run_mode_description},
     31         {GIMP_PDB_STRING, filename_name, filename_description},
     32         {GIMP_PDB_STRING, raw_filename_name, raw_filename_description},
     33     };
     34     static char image_name[] = "image";
     35     static char image_description[] = "Loaded image";
     36     static const GimpParamDef load_return_vals[] = {
     37         {GIMP_PDB_IMAGE, image_name, image_description},
     38     };
     39 
     40     gimp_install_procedure(
     41         /*name=*/kLoadProc, /*blurb=*/"Loads JPEG XL image files",
     42         /*help=*/"Loads JPEG XL image files", /*author=*/"JPEG XL Project",
     43         /*copyright=*/"JPEG XL Project", /*date=*/"2019",
     44         /*menu_label=*/"JPEG XL image", /*image_types=*/nullptr,
     45         /*type=*/GIMP_PLUGIN, /*n_params=*/G_N_ELEMENTS(load_args),
     46         /*n_return_vals=*/G_N_ELEMENTS(load_return_vals), /*params=*/load_args,
     47         /*return_vals=*/load_return_vals);
     48     gimp_register_file_handler_mime(kLoadProc, "image/jxl");
     49     gimp_register_magic_load_handler(
     50         kLoadProc, "jxl", "",
     51         "0,string,\xFF\x0A,"
     52         "0,string,\\000\\000\\000\x0CJXL\\040\\015\\012\x87\\012");
     53   }
     54 
     55   {
     56     static char run_mode_name[] = "run-mode";
     57     static char run_mode_description[] = "Run mode";
     58     static char image_name[] = "image";
     59     static char image_description[] = "Input image";
     60     static char drawable_name[] = "drawable";
     61     static char drawable_description[] = "Drawable to save";
     62     static char filename_name[] = "filename";
     63     static char filename_description[] = "The name of the file to save";
     64     static char raw_filename_name[] = "raw-filename";
     65     static char raw_filename_description[] = "The name of the file to save";
     66     static const GimpParamDef save_args[] = {
     67         {GIMP_PDB_INT32, run_mode_name, run_mode_description},
     68         {GIMP_PDB_IMAGE, image_name, image_description},
     69         {GIMP_PDB_DRAWABLE, drawable_name, drawable_description},
     70         {GIMP_PDB_STRING, filename_name, filename_description},
     71         {GIMP_PDB_STRING, raw_filename_name, raw_filename_description},
     72     };
     73 
     74     gimp_install_procedure(
     75         /*name=*/kSaveProc, /*blurb=*/"Saves JPEG XL image files",
     76         /*help=*/"Saves JPEG XL image files", /*author=*/"JPEG XL Project",
     77         /*copyright=*/"JPEG XL Project", /*date=*/"2019",
     78         /*menu_label=*/"JPEG XL image", /*image_types=*/"RGB*, GRAY*",
     79         /*type=*/GIMP_PLUGIN, /*n_params=*/G_N_ELEMENTS(save_args),
     80         /*n_return_vals=*/0, /*params=*/save_args,
     81         /*return_vals=*/nullptr);
     82     gimp_register_file_handler_mime(kSaveProc, "image/jxl");
     83     gimp_register_save_handler(kSaveProc, "jxl", "");
     84   }
     85 }
     86 
     87 void Run(const gchar* const name, const gint nparams,
     88          const GimpParam* const params, gint* const nreturn_vals,
     89          GimpParam** const return_vals) {
     90   gegl_init(nullptr, nullptr);
     91 
     92   static GimpParam values[2];
     93 
     94   *nreturn_vals = 1;
     95   *return_vals = values;
     96 
     97   values[0].type = GIMP_PDB_STATUS;
     98   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
     99 
    100   if (strcmp(name, kLoadProc) == 0) {
    101     if (nparams != 3) {
    102       values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
    103       return;
    104     }
    105 
    106     const gchar* const filename = params[1].data.d_string;
    107     gint32 image_id;
    108     if (!LoadJpegXlImage(filename, &image_id)) {
    109       values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
    110       return;
    111     }
    112 
    113     *nreturn_vals = 2;
    114     values[0].data.d_status = GIMP_PDB_SUCCESS;
    115     values[1].type = GIMP_PDB_IMAGE;
    116     values[1].data.d_image = image_id;
    117   } else if (strcmp(name, kSaveProc) == 0) {
    118     if (nparams != 5) {
    119       values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
    120       return;
    121     }
    122 
    123     gint32 image_id = params[1].data.d_image;
    124     gint32 drawable_id = params[2].data.d_drawable;
    125     const gchar* const filename = params[3].data.d_string;
    126     const gint32 orig_image_id = image_id;
    127     const GimpExportReturn export_result = gimp_export_image(
    128         &image_id, &drawable_id, "JPEG XL",
    129         static_cast<GimpExportCapabilities>(GIMP_EXPORT_CAN_HANDLE_RGB |
    130                                             GIMP_EXPORT_CAN_HANDLE_GRAY |
    131                                             GIMP_EXPORT_CAN_HANDLE_ALPHA));
    132     switch (export_result) {
    133       case GIMP_EXPORT_CANCEL:
    134         values[0].data.d_status = GIMP_PDB_CANCEL;
    135         return;
    136       case GIMP_EXPORT_IGNORE:
    137         break;
    138       case GIMP_EXPORT_EXPORT:
    139         break;
    140     }
    141     if (!SaveJpegXlImage(image_id, drawable_id, orig_image_id, filename)) {
    142       return;
    143     }
    144     if (image_id != orig_image_id) {
    145       gimp_image_delete(image_id);
    146     }
    147     values[0].data.d_status = GIMP_PDB_SUCCESS;
    148   }
    149 }
    150 
    151 }  // namespace
    152 }  // namespace jxl
    153 
    154 static const GimpPlugInInfo PLUG_IN_INFO = {nullptr, nullptr, &jxl::Query,
    155                                             &jxl::Run};
    156 
    157 MAIN()