Diff for /parser3/src/classes/image.C between versions 1.185 and 1.201

version 1.185, 2024/09/11 21:07:36 version 1.201, 2026/10/25 16:58:12
Line 1 Line 1
 /** @file  /** @file
         Parser: @b image parser class.          Parser: @b image parser class.
   
         Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
         Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>          Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
 */  */
   
Line 189  public: Line 189  public:
                 EXIF_TAG(0xa434,        LensModel);                  EXIF_TAG(0xa434,        LensModel);
                 EXIF_TAG(0xa435,        LensSerialNumber);                  EXIF_TAG(0xa435,        LensSerialNumber);
         }          }
 } exif_tag_value2name;  
           static EXIF_tag_value2name &instance(){
                   static EXIF_tag_value2name *singleton=NULL;
                   if(!singleton)
                           singleton=new EXIF_tag_value2name;
                   return *singleton;
           }
   };
   
 class EXIF_gps_tag_value2name: public Hash<int, const char*> {  class EXIF_gps_tag_value2name: public Hash<int, const char*> {
 public:  public:
Line 226  public: Line 233  public:
                 EXIF_TAG(0x1D,  GPSDateStamp);                  EXIF_TAG(0x1D,  GPSDateStamp);
                 EXIF_TAG(0x1E,  GPSDifferential);                  EXIF_TAG(0x1E,  GPSDifferential);
         }          }
 } exif_gps_tag_value2name;  
           static EXIF_gps_tag_value2name &instance(){
                   static EXIF_gps_tag_value2name *singleton=NULL;
                   if(!singleton)
                           singleton=new EXIF_gps_tag_value2name;
                   return *singleton;
           }
   
   };
   
   
 ///*********************************************** support functions  ///*********************************************** support functions
   
   /// Exif lives in one JPEG segment, whose length is 16 bits: nothing in Exif is longer
   #define JPG_EXIF_MAX_SIZE 0xFFFF
   
   /// limits of the work a measured file can require
   class Measure_budget {
   public:
           enum Kind {
                   EXIF_VALUES, ///< bytes of the values lying outside of the Exif IFD entries
                   EXIF_IFDS,   ///< jumps from an IFD to the Exif and GPS IFDs
                   MP4_DEPTH,   ///< nesting of MP4 chunks: given back by release()
                   KIND_COUNT
           };
   
           Measure_budget(const String& aorigin): origin(aorigin) {
                   left[EXIF_VALUES]=JPG_EXIF_MAX_SIZE;
                   left[EXIF_IFDS]=4; // IFD0 has two of them (Exif and GPS), the rest is spare
                   left[MP4_DEPTH]=8; // real files nest chunks 3 levels deep: moov/trak/mdia
           }
   
           void use(Kind kind, uint amount=1) {
                   static const char* const messages[KIND_COUNT]={
                           "broken Exif - too long values",
                           "broken Exif - too many IFD references",
                           "broken MP4 file - chunks nested too deep"
                   };
   
                   if(amount>left[kind])
                           throw Exception(IMAGE_FORMAT, &origin, messages[kind]);
                   left[kind]-=amount;
           }
   
           /// gives back what use() took
           void release(Kind kind, uint amount=1) {
                   left[kind]+=amount;
           }
   
   private:
           const String& origin;
           uint left[KIND_COUNT];
   };
   
 class Measure_reader {  class Measure_reader {
 public:  public:
           const String& file_name;
           Measure_budget budget; // of the file being measured
   
           Measure_reader(const String& afile_name): file_name(afile_name), budget(afile_name) {}
   
         virtual size_t read(const char* &buf, size_t limit)=0;          virtual size_t read(const char* &buf, size_t limit)=0;
         virtual void seek(uint64_t value)=0;          virtual void seek(uint64_t value)=0;
         virtual uint64_t tell()=0;          virtual uint64_t tell()=0;
Line 240  public: Line 301  public:
 };  };
   
 class Measure_file_reader: public Measure_reader {  class Measure_file_reader: public Measure_reader {
         const String& file_name;  
         int f;          int f;
           uint64_t size;
   
 public:  public:
         Measure_file_reader(int af, const String& afile_name):          Measure_file_reader(int af, const String& afile_name, uint64_t asize):
                 file_name(afile_name), f(af) {                  Measure_reader(afile_name), f(af), size(asize) {
         }          }
   
         override size_t read(const char* &abuf, size_t limit) {          override size_t read(const char* &abuf, size_t limit) {
Line 261  public: Line 322  public:
   
         override void seek(uint64_t value) {          override void seek(uint64_t value) {
                 if(pa_lseek(f, value, SEEK_SET)<0)                  if(pa_lseek(f, value, SEEK_SET)<0)
                         throw Exception(IMAGE_FORMAT, &file_name, "seek to %.15g failed: %s (%d)", (double)value, strerror(errno), errno);                          throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: %s (%d)", pa_uitoa(value), strerror(errno), errno);
         }          }
   
         override uint64_t tell() { return pa_lseek(f, 0, SEEK_CUR); }          override uint64_t tell() { return pa_lseek(f, 0, SEEK_CUR); }
   
         override uint64_t length() { return pa_lseek(f, 0, SEEK_END); }          override uint64_t length() { return size; }
   
 };  };
   
 class Measure_buf_reader: public Measure_reader {  class Measure_buf_reader: public Measure_reader {
   
         const char* buf; size_t size;          const char* buf; size_t size;
         const String& file_name;   
   
         size_t offset;          size_t offset;
   
 public:  public:
         Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):           Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name): 
                 buf(abuf), size(asize), file_name(afile_name), offset(0) {                  Measure_reader(afile_name), buf(abuf), size(asize), offset(0) {
         }          }
                   
         override size_t read(const char* &abuf, size_t limit) {          override size_t read(const char* &abuf, size_t limit) {
Line 291  public: Line 349  public:
   
         override void seek(uint64_t value) {          override void seek(uint64_t value) {
                 if(value>(uint64_t)size)                  if(value>(uint64_t)size)
                         throw Exception(IMAGE_FORMAT, &file_name, "seek to %.15g failed: out of buffer (%.15g)", value, size);                          throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: out of buffer (%s)", pa_uitoa(value), pa_uitoa(size));
                 offset=(size_t)value;                  offset=(size_t)value;
         }          }
   
Line 396  static Value* parse_IFD_entry_formatted_ Line 454  static Value* parse_IFD_entry_formatted_
         case 10: // signed rational          case 10: // signed rational
                 {                  {
                         signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;                          signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
                         uint denominator=endian_to_uint(is_big, value);                          signed int denominator=(signed int)endian_to_uint(is_big, value);
                         if(!denominator)                          if(!denominator)
                                 return 0;                                  return 0;
                         return new VDouble(numerator/denominator);                          return new VDouble(((double)numerator)/denominator);
                 }                  }
                 /*                  /*
         case 11: // single float          case 11: // single float
Line 428  static Value* parse_IFD_entry_formatted_ Line 486  static Value* parse_IFD_entry_formatted_
                         catch(...) { /*ignore bad date times*/ }                          catch(...) { /*ignore bad date times*/ }
                 }                  }
   
                 return new VString(*new String(cstr, String::L_TAINTED));                  const char* end=(const char*)memchr(cstr, 0, length);
                   return new VString(pa_strdup(cstr, end ? end-cstr : length));
         }          }
   
         if(components_count==1)          if(components_count==1)
Line 438  static Value* parse_IFD_entry_formatted_ Line 497  static Value* parse_IFD_entry_formatted_
         HashStringValue& hash=result->hash();          HashStringValue& hash=result->hash();
         for(uint i=0; i<components_count; i++, value+=component_size) {          for(uint i=0; i<components_count; i++, value+=component_size) {
                 hash.put(                  hash.put(
                         pa_uitoa(i),                          String::Body::uitoa(i),
                         parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));                          parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
         }          }
   
Line 475  static Value* parse_IFD_entry_value(bool Line 534  static Value* parse_IFD_entry_value(bool
         // You can get the total data byte length by multiplies           // You can get the total data byte length by multiplies 
         // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area          // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
         uint components_count=endian_to_uint(is_big, entry.components_count);          uint components_count=endian_to_uint(is_big, entry.components_count);
           if(components_count>JPG_EXIF_MAX_SIZE)
                   throw Exception(IMAGE_FORMAT, &reader.file_name, "broken Exif - too many components");
         uint value_size=component_size*components_count;          uint value_size=component_size*components_count;
         // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address          // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
         Value* result;          Value* result;
Line 482  static Value* parse_IFD_entry_value(bool Line 543  static Value* parse_IFD_entry_value(bool
         if(value_size<=4)          if(value_size<=4)
                 result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it);                  result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it);
         else {          else {
                   reader.budget.use(Measure_budget::EXIF_VALUES, value_size);
   
                 uint64_t remembered=reader.tell();                  uint64_t remembered=reader.tell();
                 {                  {
                         reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));                          reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));
                         const char* value;                          const char* value;
                         if(reader.read(value, value_size)<value_size)                          if(reader.read(value, value_size)<value_size)
                                 return 0;                                  result=0;
                         result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value);                          else
                                   result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value);
                 }                  }
                 reader.seek(remembered);                  reader.seek(remembered);
         }          }
Line 502  static void parse_IFD_entry(HashStringVa Line 566  static void parse_IFD_entry(HashStringVa
         ushort tag=endian_to_ushort(is_big, entry.tag);          ushort tag=endian_to_ushort(is_big, entry.tag);
   
         if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){          if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
                   reader.budget.use(Measure_budget::EXIF_IFDS); // an IFD can point to itself
   
                 uint64_t remembered=reader.tell();                  uint64_t remembered=reader.tell();
                 {                  {
                         reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));                          reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));
Line 512  static void parse_IFD_entry(HashStringVa Line 578  static void parse_IFD_entry(HashStringVa
         }          }
   
         if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {          if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
                 if(const char* name=(gps)?exif_gps_tag_value2name.get(tag):exif_tag_value2name.get(tag))                  if(const char* name=(gps ? EXIF_gps_tag_value2name::instance().get(tag) : EXIF_tag_value2name::instance().get(tag)))
                         hash.put(name, value);                          hash.put(name, value);
                 else                  else
                         hash.put(pa_uitoa(tag), value);                          hash.put(String::Body::uitoa((int)tag), value);
         }          }
 }  }
   
Line 629  static void measure_jpeg(const String& o Line 695  static void measure_jpeg(const String& o
                         {                          {
                                 // Segments that contain size info                                  // Segments that contain size info
                                 if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))                                  if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
                                         throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - can not fully read Size segment");                                          throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - cannot fully read Size segment");
                                 JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;                                  JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                                                                   
                                 info.width=endian_to_ushort(true, body->width);                                  info.width=endian_to_ushort(true, body->width);
Line 892  static bool measure_mp4(const String& or Line 958  static bool measure_mp4(const String& or
                                 throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - truncated chunk extended size header");                                  throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - truncated chunk extended size header");
                         MP4_ExtSize *ext_size=(MP4_ExtSize *)buf;                          MP4_ExtSize *ext_size=(MP4_ExtSize *)buf;
                         size=((uint64_t)endian_to_uint(true, ext_size->high) << 32) + endian_to_uint(true, ext_size->low);                          size=((uint64_t)endian_to_uint(true, ext_size->high) << 32) + endian_to_uint(true, ext_size->low);
                 }                  } else if(size==0) // the chunk extends to the end (of file or container)
                           size=(anext ? anext : reader.length())-next;
                 next+=size;                  next+=size;
   
                   // the chunk can't end before its header does (size is too small or wraps around): we would never get to the end
                   if(next<reader.tell())
                           throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad chunk size");
   
                 if(first){                  if(first){
                         if(strncmp(head->signature, "ftyp", 4)!=0)                          if(strncmp(head->signature, "ftyp", 4)!=0)
                                 throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature");                                  throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature");
                         first=false;                          first=false;
                         anext=reader.length(); // to avoid reading beyond EOF                          anext=reader.length(); // to avoid reading beyond EOF
                 } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) {                  } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) {
                           reader.budget.use(Measure_budget::MP4_DEPTH);
                         if(measure_mp4(origin_string, reader, width, height, next, lastTkhd))                          if(measure_mp4(origin_string, reader, width, height, next, lastTkhd))
                                 return true;                                  return true;
                           reader.budget.release(Measure_budget::MP4_DEPTH);
                 } else if(strncmp(head->signature, "tkhd", 4)==0) {                  } else if(strncmp(head->signature, "tkhd", 4)==0) {
                         if(size>8){                          if(size>8){
                                 reader.seek(next-8);                                  reader.seek(next-8);
Line 960  static void measure(const String& file_n Line 1033  static void measure(const String& file_n
                 else                  else
                         throw Exception(IMAGE_FORMAT, &file_name, "unhandled file name extension '%s'", cext);                          throw Exception(IMAGE_FORMAT, &file_name, "unhandled file name extension '%s'", cext);
         } else          } else
                 throw Exception(IMAGE_FORMAT, &file_name, "can not determine file type - no file name extension");                  throw Exception(IMAGE_FORMAT, &file_name, "cannot determine file type - no file name extension");
 }  }
   
 // methods  // methods
   
 static void file_measure_action(struct stat& /*finfo*/, int f, const String& file_spec, void *context) {  static void file_measure_action(struct stat& finfo, int f, const String& file_spec, void *context) {
         Measure_file_reader reader(f, file_spec);          Measure_file_reader reader(f, file_spec, finfo.st_size);
         measure(file_spec, reader, *static_cast<Measure_info *>(context));          measure(file_spec, reader, *static_cast<Measure_info *>(context));
 }  }
   
Line 979  static void _measure(Request& r, MethodP Line 1052  static void _measure(Request& r, MethodP
   
         if(params.count()>1)          if(params.count()>1)
                 if(HashStringValue* options=params.as_hash(1, "methods options")) {                  if(HashStringValue* options=params.as_hash(1, "methods options")) {
                         int valid_options=0;  
                         for(HashStringValue::Iterator i(*options); i; i.next() ){                          for(HashStringValue::Iterator i(*options); i; i.next() ){
                                 String::Body key=i.key();                                  String::Body key=i.key();
                                 Value* value=i.value();                                  Value* value=i.value();
                                 if(key == "exif") {                                  if(key == "exif") {
                                         if(r.process(*value).as_bool())                                          if(r.process(*value).as_bool())
                                                 info.exif=&exif;                                                  info.exif=&exif;
                                         valid_options++;                                  } else if(key == "xmp") {
                                 }  
                                 if(key == "xmp") {  
                                         if(r.process(*value).as_bool())                                          if(r.process(*value).as_bool())
                                                 info.xmp=&xmp;                                                  info.xmp=&xmp;
                                         valid_options++;                                  } else if(key == "xmp-charset") {
                                 }  
                                 if(key == "xmp-charset") {  
                                         info.xmp_charset=&pa_charsets.get(value->as_string());                                          info.xmp_charset=&pa_charsets.get(value->as_string());
                                         valid_options++;                                  } else if(key == "video") {
                                 }  
                                 if(key == "video") {  
                                         info.video=r.process(*value).as_bool();                                          info.video=r.process(*value).as_bool();
                                         valid_options++;                                  } else
                                 }                                          throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                         }                          }
                         if(valid_options!=options->count())  
                                 throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);  
                 }                  }
   
         const String* file_name;          const String* file_name;
Line 1075  static gdImage* load(Request& r, const S Line 1139  static gdImage* load(Request& r, const S
                         throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");                          throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");
                 return image;                  return image;
         } else {          } else {
                 throw Exception("file.missing", 0, "can not open '%s'", file_name_cstr);                  throw Exception("file.missing", 0, "cannot open '%s'", file_name_cstr);
         }          }
 }  }
   
   
 static void _load(Request& r, MethodParams& params) {  static void _load(Request& r, MethodParams& params) {
         const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);          const String& file_name=params.as_file_name(0);
   
         gdImage* image=load(r, file_name);          gdImage* image=load(r, file_name);
         GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);          GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
Line 1332  void Font::string_display(gdImage& image Line 1396  void Font::string_display(gdImage& image
   
   
 static void _font(Request& r, MethodParams& params) {  static void _font(Request& r, MethodParams& params) {
         const String& alphabet=params.as_string(0, "alphabet must not be code");          const String& alphabet=params.as_string(0, "alphabet must be string");
         size_t alphabet_length=alphabet.length(r.charsets.source());          size_t alphabet_length=alphabet.length(r.charsets.source());
         if(!alphabet_length)          if(!alphabet_length)
                 throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");                  throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");
   
         gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));          gdImage* image=load(r, params.as_file_name(1));
   
         int spacebar_width=image->SX();          int spacebar_width=image->SX();
         int monospace_width=0; // proportional          int monospace_width=0; // proportional
Line 1384  static void _font(Request& r, MethodPara Line 1448  static void _font(Request& r, MethodPara
 static void _text(Request& r, MethodParams& params) {  static void _text(Request& r, MethodParams& params) {
         int x=params.as_int(0, "x must be int", r);          int x=params.as_int(0, "x must be int", r);
         int y=params.as_int(1, "y must be int", r);          int y=params.as_int(1, "y must be int", r);
         const String& s=params.as_string(2, "text must not be code");          const String& s=params.as_string(2, "text must be string");
   
         VImage& vimage=GET_SELF(r, VImage);          VImage& vimage=GET_SELF(r, VImage);
         vimage.font().string_display(vimage.image(), x, y, s);          vimage.font().string_display(vimage.image(), x, y, s);
 }  }
   
 static void _length(Request& r, MethodParams& params) {  static void _length(Request& r, MethodParams& params) {
         const String& s=params.as_string(0, "text must not be code");          const String& s=params.as_string(0, "text must be string");
   
         VImage& vimage=GET_SELF(r, VImage);          VImage& vimage=GET_SELF(r, VImage);
         r.write(*new VInt(vimage.font().string_width(s)));          r.write(*new VInt(vimage.font().string_width(s)));
Line 1440  static void _circle(Request& r, MethodPa Line 1504  static void _circle(Request& r, MethodPa
 gdImage& as_image(MethodParams& params, int index, const char* msg) {  gdImage& as_image(MethodParams& params, int index, const char* msg) {
         Value& value=params.as_no_junction(index, msg);          Value& value=params.as_no_junction(index, msg);
   
         if(Value* vimage=value.as(VIMAGE_TYPE)) {          if(VImage* vimage=dynamic_cast<VImage *>(&value)) {
                 return static_cast<VImage *>(vimage)->image();                  return vimage->image();
         } else          } else
                 throw Exception(PARSER_RUNTIME, 0, msg);                  throw Exception(PARSER_RUNTIME, 0, msg);
 }  }

Removed from v.1.185  
changed lines
  Added in v.1.201


E-mail: