Annotation of parser3/src/classes/image.C, revision 1.201
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.199 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.184 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.73 paf 6: */
1.1 paf 7:
1.31 parser 8: /*
9: jpegsize: gets the width and height (in pixels) of a jpeg file
10: Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995
11: modified slightly by alex@ed.ac.uk
12: and further still by rjray@uswest.com
13: optimization and general re-write from tmetro@vl.com
14: from perl by paf@design.ru
1.1 paf 15: */
16:
17: #include "pa_config_includes.h"
1.139 moko 18: #include "gif.h"
1.1 paf 19:
1.91 paf 20: #include "pa_vmethod_frame.h"
1.1 paf 21: #include "pa_common.h"
22: #include "pa_request.h"
23: #include "pa_vfile.h"
24: #include "pa_vimage.h"
1.89 paf 25: #include "pa_vdate.h"
1.91 paf 26: #include "pa_table.h"
1.163 moko 27: #include "pa_charsets.h"
1.1 paf 28:
1.201 ! moko 29: volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.200 2026/09/27 17:37:51 moko Exp $";
1.139 moko 30:
1.125 misha 31: // defines
32:
1.126 misha 33: static const String spacebar_width_name("space");
34: static const String monospace_width_name("width");
35: static const String letter_spacing_name("spacing");
1.125 misha 36:
1.22 paf 37: // class
38:
1.91 paf 39: class MImage: public Methoded {
1.22 paf 40: public: // VStateless_class
1.130 misha 41: Value* create_new_value(Pool&) { return new VImage(); }
1.22 paf 42:
43: public:
1.91 paf 44: MImage();
1.22 paf 45: };
1.1 paf 46:
1.91 paf 47: // globals
48:
1.151 moko 49: DECLARE_CLASS_VAR(image, new MImage);
1.91 paf 50:
1.1 paf 51: // helpers
52:
1.118 misha 53: #define EXIF_TAG(tag, name) put(tag, #name);
54:
1.91 paf 55: /// value of exif tag -> it's value
56: class EXIF_tag_value2name: public Hash<int, const char*> {
57: public:
58: EXIF_tag_value2name() {
59: // image JPEG Exif
60: // Tags used by IFD0 (main image)
61: EXIF_TAG(0x010e, ImageDescription);
62: EXIF_TAG(0x010f, Make);
63: EXIF_TAG(0x0110, Model);
64: EXIF_TAG(0x0112, Orientation);
65: EXIF_TAG(0x011a, XResolution);
66: EXIF_TAG(0x011b, YResolution);
67: EXIF_TAG(0x0128, ResolutionUnit);
68: EXIF_TAG(0x0131, Software);
69: EXIF_TAG(0x0132, DateTime);
70: EXIF_TAG(0x013e, WhitePoint);
71: EXIF_TAG(0x013f, PrimaryChromaticities);
72: EXIF_TAG(0x0211, YCbCrCoefficients);
73: EXIF_TAG(0x0213, YCbCrPositioning);
74: EXIF_TAG(0x0214, ReferenceBlackWhite);
75: EXIF_TAG(0x8298, Copyright);
76: EXIF_TAG(0x8769, ExifOffset);
77: // Tags used by Exif SubIFD
78: EXIF_TAG(0x829a, ExposureTime);
79: EXIF_TAG(0x829d, FNumber);
80: EXIF_TAG(0x8822, ExposureProgram);
81: EXIF_TAG(0x8827, ISOSpeedRatings);
82: EXIF_TAG(0x9000, ExifVersion);
83: EXIF_TAG(0x9003, DateTimeOriginal);
84: EXIF_TAG(0x9004, DateTimeDigitized);
85: EXIF_TAG(0x9101, ComponentsConfiguration);
86: EXIF_TAG(0x9102, CompressedBitsPerPixel);
87: EXIF_TAG(0x9201, ShutterSpeedValue);
88: EXIF_TAG(0x9202, ApertureValue);
89: EXIF_TAG(0x9203, BrightnessValue);
90: EXIF_TAG(0x9204, ExposureBiasValue);
91: EXIF_TAG(0x9205, MaxApertureValue);
92: EXIF_TAG(0x9206, SubjectDistance);
93: EXIF_TAG(0x9207, MeteringMode);
94: EXIF_TAG(0x9208, LightSource);
95: EXIF_TAG(0x9209, Flash);
96: EXIF_TAG(0x920a, FocalLength);
97: EXIF_TAG(0x927c, MakerNote);
98: EXIF_TAG(0x9286, UserComment);
99: EXIF_TAG(0x9290, SubsecTime);
100: EXIF_TAG(0x9291, SubsecTimeOriginal);
101: EXIF_TAG(0x9292, SubsecTimeDigitized);
102: EXIF_TAG(0xa000, FlashPixVersion);
103: EXIF_TAG(0xa001, ColorSpace);
104: EXIF_TAG(0xa002, ExifImageWidth);
105: EXIF_TAG(0xa003, ExifImageHeight);
106: EXIF_TAG(0xa004, RelatedSoundFile);
107: EXIF_TAG(0xa005, ExifInteroperabilityOffset);
108: EXIF_TAG(0xa20e, FocalPlaneXResolution);
109: EXIF_TAG(0xa20f, FocalPlaneYResolution);
110: EXIF_TAG(0xa210, FocalPlaneResolutionUnit);
111: EXIF_TAG(0xa215, ExposureIndex);
112: EXIF_TAG(0xa217, SensingMethod);
113: EXIF_TAG(0xa300, FileSource);
114: EXIF_TAG(0xa301, SceneType);
115: EXIF_TAG(0xa302, CFAPattern);
116: // Misc Tags
117: EXIF_TAG(0x00fe, NewSubfileType);
118: EXIF_TAG(0x00ff, SubfileType);
119: EXIF_TAG(0x012d, TransferFunction);
120: EXIF_TAG(0x013b, Artist);
121: EXIF_TAG(0x013d, Predictor);
122: EXIF_TAG(0x0142, TileWidth);
123: EXIF_TAG(0x0143, TileLength);
124: EXIF_TAG(0x0144, TileOffsets);
125: EXIF_TAG(0x0145, TileByteCounts);
126: EXIF_TAG(0x014a, SubIFDs);
127: EXIF_TAG(0x015b, JPEGTables);
128: EXIF_TAG(0x828d, CFARepeatPatternDim);
129: EXIF_TAG(0x828e, CFAPattern);
130: EXIF_TAG(0x828f, BatteryLevel);
131: EXIF_TAG(0x83bb, IPTC/NAA);
132: EXIF_TAG(0x8773, InterColorProfile);
133: EXIF_TAG(0x8824, SpectralSensitivity);
1.118 misha 134: //EXIF_TAG(0x8825, GPSInfo);
1.91 paf 135: EXIF_TAG(0x8828, OECF);
136: EXIF_TAG(0x8829, Interlace);
137: EXIF_TAG(0x882a, TimeZoneOffset);
138: EXIF_TAG(0x882b, SelfTimerMode);
139: EXIF_TAG(0x920b, FlashEnergy);
140: EXIF_TAG(0x920c, SpatialFrequencyResponse);
141: EXIF_TAG(0x920d, Noise);
142: EXIF_TAG(0x9211, ImageNumber);
143: EXIF_TAG(0x9212, SecurityClassification);
144: EXIF_TAG(0x9213, ImageHistory);
145: EXIF_TAG(0x9214, SubjectLocation);
146: EXIF_TAG(0x9215, ExposureIndex);
147: EXIF_TAG(0x9216, TIFF/EPStandardID);
148: EXIF_TAG(0xa20b, FlashEnergy);
149: EXIF_TAG(0xa20c, SpatialFrequencyResponse);
150: EXIF_TAG(0xa214, SubjectLocation);
1.118 misha 151:
152: // additional things added by misha@
153: EXIF_TAG(0x0100, ImageWidth);
154: EXIF_TAG(0x0101, ImageLength);
155: EXIF_TAG(0x0102, BitsPerSample);
156: EXIF_TAG(0x0103, Compression);
157: EXIF_TAG(0x0106, PhotometricInterpretation);
158: EXIF_TAG(0x010a, FillOrder);
159: EXIF_TAG(0x010d, DocumentName);
160: EXIF_TAG(0x0111, StripOffsets);
161: EXIF_TAG(0x0115, SamplesPerPixel);
162: EXIF_TAG(0x0116, RowsPerStrip);
163: EXIF_TAG(0x0117, StripByteCounts);
164: EXIF_TAG(0x011c, PlanarConfiguration);
165: EXIF_TAG(0x0156, TransferRange);
166: EXIF_TAG(0x0200, JPEGProc);
167: EXIF_TAG(0x0201, JPEGInterchangeFormat);
168: EXIF_TAG(0x0202, JPEGInterchangeFormatLength);
169: EXIF_TAG(0x0212, YCbCrSubSampling);
170: EXIF_TAG(0xa401, CustomRendered);
171: EXIF_TAG(0xa402, ExposureMode);
172: EXIF_TAG(0xa403, WhiteBalance);
173: EXIF_TAG(0xa404, DigitalZoomRatio);
174: EXIF_TAG(0xa405, FocalLengthIn35mmFilm);
175: EXIF_TAG(0xa406, SceneCaptureType);
176: EXIF_TAG(0xa407, GainControl);
177: EXIF_TAG(0xa408, Contrast);
178: EXIF_TAG(0xa409, Saturation);
179: EXIF_TAG(0xa40a, Sharpness);
180: EXIF_TAG(0xa40b, DeviceSettingDescription);
181: EXIF_TAG(0xa40c, SubjectDistanceRange);
182: EXIF_TAG(0xa420, ImageUniqueID);
1.149 moko 183:
184: // other tags
185: EXIF_TAG(0xa430, CameraOwnerName);
186: EXIF_TAG(0xa431, BodySerialNumber);
187: EXIF_TAG(0xa432, LensSpecification);
188: EXIF_TAG(0xa433, LensManufactor);
189: EXIF_TAG(0xa434, LensModel);
190: EXIF_TAG(0xa435, LensSerialNumber);
1.91 paf 191: }
1.190 moko 192:
193: static EXIF_tag_value2name &instance(){
194: static EXIF_tag_value2name *singleton=NULL;
1.192 moko 195: if(!singleton)
1.190 moko 196: singleton=new EXIF_tag_value2name;
197: return *singleton;
198: }
199: };
1.91 paf 200:
1.118 misha 201: class EXIF_gps_tag_value2name: public Hash<int, const char*> {
202: public:
203: EXIF_gps_tag_value2name() {
204: EXIF_TAG(0x0, GPSVersionID);
205: EXIF_TAG(0x1, GPSLatitudeRef);
206: EXIF_TAG(0x2, GPSLatitude);
207: EXIF_TAG(0x3, GPSLongitudeRef);
208: EXIF_TAG(0x4, GPSLongitude);
209: EXIF_TAG(0x5, GPSAltitudeRef);
210: EXIF_TAG(0x6, GPSAltitude);
211: EXIF_TAG(0x7, GPSTimeStamp);
212: EXIF_TAG(0x8, GPSSatellites);
213: EXIF_TAG(0x9, GPSStatus);
214: EXIF_TAG(0xA, GPSMeasureMode);
215: EXIF_TAG(0xB, GPSDOP);
216: EXIF_TAG(0xC, GPSSpeedRef);
217: EXIF_TAG(0xD, GPSSpeed);
218: EXIF_TAG(0xE, GPSTrackRef);
219: EXIF_TAG(0xF, GPSTrack);
220: EXIF_TAG(0x10, GPSImgDirectionRef);
221: EXIF_TAG(0x11, GPSImgDirection);
222: EXIF_TAG(0x12, GPSMapDatum);
223: EXIF_TAG(0x13, GPSDestLatitudeRef);
224: EXIF_TAG(0x14, GPSDestLatitude);
225: EXIF_TAG(0x15, GPSDestLongitudeRef);
226: EXIF_TAG(0x16, GPSDestLongitude);
227: EXIF_TAG(0x17, GPSDestBearingRef);
228: EXIF_TAG(0x18, GPSDestBearing);
229: EXIF_TAG(0x19, GPSDestDistanceRef);
230: EXIF_TAG(0x1A, GPSDestDistance);
231: EXIF_TAG(0x1B, GPSProcessingMethod);
232: EXIF_TAG(0x1C, GPSAreaInformation);
233: EXIF_TAG(0x1D, GPSDateStamp);
234: EXIF_TAG(0x1E, GPSDifferential);
235: }
1.190 moko 236:
237: static EXIF_gps_tag_value2name &instance(){
238: static EXIF_gps_tag_value2name *singleton=NULL;
1.192 moko 239: if(!singleton)
1.190 moko 240: singleton=new EXIF_gps_tag_value2name;
241: return *singleton;
242: }
243:
244: };
1.118 misha 245:
1.170 moko 246:
1.169 moko 247: ///*********************************************** support functions
1.91 paf 248:
1.201 ! moko 249: /// Exif lives in one JPEG segment, whose length is 16 bits: nothing in Exif is longer
! 250: #define JPG_EXIF_MAX_SIZE 0xFFFF
! 251:
! 252: /// limits of the work a measured file can require
! 253: class Measure_budget {
! 254: public:
! 255: enum Kind {
! 256: EXIF_VALUES, ///< bytes of the values lying outside of the Exif IFD entries
! 257: EXIF_IFDS, ///< jumps from an IFD to the Exif and GPS IFDs
! 258: MP4_DEPTH, ///< nesting of MP4 chunks: given back by release()
! 259: KIND_COUNT
! 260: };
! 261:
! 262: Measure_budget(const String& aorigin): origin(aorigin) {
! 263: left[EXIF_VALUES]=JPG_EXIF_MAX_SIZE;
! 264: left[EXIF_IFDS]=4; // IFD0 has two of them (Exif and GPS), the rest is spare
! 265: left[MP4_DEPTH]=8; // real files nest chunks 3 levels deep: moov/trak/mdia
! 266: }
! 267:
! 268: void use(Kind kind, uint amount=1) {
! 269: static const char* const messages[KIND_COUNT]={
! 270: "broken Exif - too long values",
! 271: "broken Exif - too many IFD references",
! 272: "broken MP4 file - chunks nested too deep"
! 273: };
! 274:
! 275: if(amount>left[kind])
! 276: throw Exception(IMAGE_FORMAT, &origin, messages[kind]);
! 277: left[kind]-=amount;
! 278: }
! 279:
! 280: /// gives back what use() took
! 281: void release(Kind kind, uint amount=1) {
! 282: left[kind]+=amount;
! 283: }
! 284:
! 285: private:
! 286: const String& origin;
! 287: uint left[KIND_COUNT];
! 288: };
! 289:
1.1 paf 290: class Measure_reader {
291: public:
1.201 ! moko 292: const String& file_name;
! 293: Measure_budget budget; // of the file being measured
! 294:
! 295: Measure_reader(const String& afile_name): file_name(afile_name), budget(afile_name) {}
! 296:
1.91 paf 297: virtual size_t read(const char* &buf, size_t limit)=0;
1.174 moko 298: virtual void seek(uint64_t value)=0;
299: virtual uint64_t tell()=0;
300: virtual uint64_t length()=0;
1.77 paf 301: };
1.1 paf 302:
1.77 paf 303: class Measure_file_reader: public Measure_reader {
1.91 paf 304: int f;
1.201 ! moko 305: uint64_t size;
1.91 paf 306:
1.77 paf 307: public:
1.201 ! moko 308: Measure_file_reader(int af, const String& afile_name, uint64_t asize):
! 309: Measure_reader(afile_name), f(af), size(asize) {
1.1 paf 310: }
311:
1.91 paf 312: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 313: if(limit==0)
1.1 paf 314: return 0;
1.77 paf 315:
1.91 paf 316: char* lbuf=new(PointerFreeGC) char[limit];
1.143 moko 317: ssize_t read_size=::read(f, lbuf, limit); abuf=lbuf;
318: if(read_size<0)
319: throw Exception(0, &file_name, "measure read failed: %s (%d)", strerror(errno), errno);
1.1 paf 320: return read_size;
321: }
322:
1.174 moko 323: override void seek(uint64_t value) {
324: if(pa_lseek(f, value, SEEK_SET)<0)
1.200 moko 325: throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: %s (%d)", pa_uitoa(value), strerror(errno), errno);
1.77 paf 326: }
327:
1.174 moko 328: override uint64_t tell() { return pa_lseek(f, 0, SEEK_CUR); }
329:
1.201 ! moko 330: override uint64_t length() { return size; }
1.79 paf 331:
1.77 paf 332: };
333:
334: class Measure_buf_reader: public Measure_reader {
1.91 paf 335: const char* buf; size_t size;
336: size_t offset;
337:
1.77 paf 338: public:
1.91 paf 339: Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):
1.201 ! moko 340: Measure_reader(afile_name), buf(abuf), size(asize), offset(0) {
1.77 paf 341: }
342:
1.91 paf 343: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 344: size_t to_read=min(limit, size-offset);
1.91 paf 345: abuf=buf+offset;
1.77 paf 346: offset+=to_read;
347: return to_read;
348: }
349:
1.174 moko 350: override void seek(uint64_t value) {
351: if(value>(uint64_t)size)
1.200 moko 352: throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: out of buffer (%s)", pa_uitoa(value), pa_uitoa(size));
1.174 moko 353: offset=(size_t)value;
1.77 paf 354: }
355:
1.174 moko 356: override uint64_t tell() { return offset; }
357:
358: override uint64_t length() { return size; }
1.79 paf 359:
1.1 paf 360: };
1.77 paf 361:
1.1 paf 362:
1.169 moko 363: struct Measure_info {
364: ushort width;
365: ushort height;
366: Value** exif;
367: Value** xmp;
368: Charset* xmp_charset;
1.181 moko 369: bool video;
1.72 paf 370: };
371:
1.164 moko 372:
1.169 moko 373: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
374: return (ushort)((b1<<8) + b0);
375: }
376:
377: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
378: return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
379: }
380:
381: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
382: return is_big ? x_endian_to_ushort(b[1], b[0]) : x_endian_to_ushort(b[0], b[1]);
383: }
384:
385: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
386: return is_big ? x_endian_to_uint(b[3], b[2], b[1], b[0]) : x_endian_to_uint(b[0], b[1], b[2], b[3]);
387: }
1.1 paf 388:
1.170 moko 389:
1.169 moko 390: ///*********************************************** JPEG
1.170 moko 391:
1.31 parser 392: struct JPG_Segment_head {
1.80 paf 393: uchar marker;
394: uchar code;
395: uchar length[2];
1.1 paf 396: };
1.21 paf 397: /// JPEG frame header
1.31 parser 398: struct JPG_Size_segment_body {
1.164 moko 399: char data; //< data precision of bits/sample
400: uchar height[2]; //< image height
401: uchar width[2]; //< image width
402: char numComponents; //< number of color components
1.1 paf 403: };
404:
1.80 paf 405: /// JPEG Exif TIFF Header
406: struct JPG_Exif_TIFF_header {
1.165 moko 407: char byte_align_identifier[2];
408: uchar signature[2]; // always 000A [or 0A00]
1.80 paf 409: uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
410: };
411:
412: // JPEG Exif IFD start
1.91 paf 413: struct JPG_Exif_IFD_begin {
1.80 paf 414: uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
415: };
416:
417: // TTTT ffff NNNNNNNN DDDDDDDD
418: struct JPG_Exif_IFD_entry {
419: uchar tag[2]; // Tag number, this shows a kind of data
420: uchar format[2]; // data format
421: uchar components_count[4]; // number of components
422: uchar value_or_offset_to_it[4]; // data value or offset to data value
423: };
424:
425: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
426:
1.118 misha 427: #define JPG_IFD_TAG_EXIF_GPS_OFFSET 0x8825
428:
1.89 paf 429: #define JPEG_EXIF_DATE_CHARS 20
430:
1.155 moko 431: static Value* parse_IFD_entry_formatted_one_value(bool is_big, ushort format, size_t component_size, const uchar *value) {
1.80 paf 432: switch(format) {
433: case 1: // unsigned byte
1.91 paf 434: return new VInt((uchar)value[0]);
1.80 paf 435: case 3: // unsigned short
1.91 paf 436: return new VInt(endian_to_ushort(is_big, value));
1.80 paf 437: case 4: // unsigned long
438: // 'double' because parser's Int is signed
1.91 paf 439: return new VDouble(endian_to_uint(is_big, value));
1.80 paf 440: case 5: // unsigned rational
441: {
442: uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
443: uint denominator=endian_to_uint(is_big, value);
444: if(!denominator)
445: return 0;
1.91 paf 446: return new VDouble(((double)numerator)/denominator);
1.80 paf 447: }
448: case 6: // signed byte
1.91 paf 449: return new VInt((signed char)value[0]);
1.80 paf 450: case 8: // signed short
1.91 paf 451: return new VInt((signed short)endian_to_ushort(is_big, value));
1.80 paf 452: case 9: // signed long
1.91 paf 453: return new VInt((signed int)endian_to_uint(is_big, value));
1.80 paf 454: case 10: // signed rational
455: {
456: signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
1.201 ! moko 457: signed int denominator=(signed int)endian_to_uint(is_big, value);
1.80 paf 458: if(!denominator)
459: return 0;
1.201 ! moko 460: return new VDouble(((double)numerator)/denominator);
1.80 paf 461: }
462: /*
463: case 11: // single float
1.120 misha 464: @todo
1.80 paf 465: case 12: // double float
1.120 misha 466: @todo
1.80 paf 467: */
468: };
469:
470: return 0;
471: }
472:
1.89 paf 473: // date.C
1.147 moko 474: tm cstr_to_time_t(char *, const char **);
1.89 paf 475:
1.155 moko 476: static Value* parse_IFD_entry_formatted_value(bool is_big, ushort format, size_t component_size, uint components_count, const uchar *value) {
1.80 paf 477: if(format==2) { // ascii string, exception: the only type with varying size
1.91 paf 478: const char* cstr=(const char* )value;
479: size_t length=components_count;
1.89 paf 480: // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.155 moko 481: if(length==JPEG_EXIF_DATE_CHARS && isdigit((unsigned char)cstr[0]) && cstr[length-1]==0) {
1.106 paf 482: try {
1.183 moko 483: tm tmIn=cstr_to_time_t(pa_strdup(cstr), 0);
1.146 moko 484: return new VDate(tmIn);
1.106 paf 485: }
486: catch(...) { /*ignore bad date times*/ }
1.89 paf 487: }
488:
1.201 ! moko 489: const char* end=(const char*)memchr(cstr, 0, length);
! 490: return new VString(pa_strdup(cstr, end ? end-cstr : length));
1.80 paf 491: }
492:
493: if(components_count==1)
1.91 paf 494: return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80 paf 495:
1.91 paf 496: VHash* result=new VHash;
497: HashStringValue& hash=result->hash();
1.80 paf 498: for(uint i=0; i<components_count; i++, value+=component_size) {
1.91 paf 499: hash.put(
1.196 moko 500: String::Body::uitoa(i),
1.91 paf 501: parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80 paf 502: }
503:
1.91 paf 504: return result;
1.80 paf 505: }
506:
1.176 moko 507: static Value* parse_IFD_entry_value(bool is_big, Measure_reader& reader, uint64_t tiff_base, JPG_Exif_IFD_entry& entry) {
1.80 paf 508: size_t format2component_size[]={
509: 0, // undefined
510: 1, // unsigned byte
511: 1, // ascii string
512: 2, // unsigned short
513: 4, // unsigned long
514: 8, // unsigned rational
515: 1, // signed byte
516: 0, // undefined
517: 2, // signed short
518: 4, // signed long
519: 8, // signed rational
520: /*
521: 4, // single float
522: 8, // double float
523: */
524: };
525:
526: ushort format=endian_to_ushort(is_big, entry.format);
527: if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
528: return 0; // format out of range, ignoring
529:
530: size_t component_size=format2component_size[format];
531: if(component_size==0)
532: return 0; // undefined format
533:
534: // You can get the total data byte length by multiplies
535: // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
536: uint components_count=endian_to_uint(is_big, entry.components_count);
1.201 ! moko 537: if(components_count>JPG_EXIF_MAX_SIZE)
! 538: throw Exception(IMAGE_FORMAT, &reader.file_name, "broken Exif - too many components");
1.121 misha 539: uint value_size=component_size*components_count;
1.80 paf 540: // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91 paf 541: Value* result;
1.80 paf 542:
543: if(value_size<=4)
1.155 moko 544: result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it);
1.80 paf 545: else {
1.201 ! moko 546: reader.budget.use(Measure_budget::EXIF_VALUES, value_size);
! 547:
1.174 moko 548: uint64_t remembered=reader.tell();
1.80 paf 549: {
1.174 moko 550: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));
1.91 paf 551: const char* value;
1.156 moko 552: if(reader.read(value, value_size)<value_size)
1.201 ! moko 553: result=0;
! 554: else
! 555: result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value);
1.80 paf 556: }
1.174 moko 557: reader.seek(remembered);
1.80 paf 558: }
559:
560: return result;
561: }
562:
1.176 moko 563: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, uint64_t tiff_base, bool gps=false);
1.91 paf 564:
1.176 moko 565: static void parse_IFD_entry(HashStringValue& hash, bool is_big, Measure_reader& reader, uint64_t tiff_base, JPG_Exif_IFD_entry& entry, bool gps=false) {
1.80 paf 566: ushort tag=endian_to_ushort(is_big, entry.tag);
1.118 misha 567:
568: if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
1.201 ! moko 569: reader.budget.use(Measure_budget::EXIF_IFDS); // an IFD can point to itself
! 570:
1.174 moko 571: uint64_t remembered=reader.tell();
1.80 paf 572: {
1.174 moko 573: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it));
1.118 misha 574: parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps);
1.80 paf 575: }
1.174 moko 576: reader.seek(remembered);
1.80 paf 577: return;
578: }
1.118 misha 579:
1.91 paf 580: if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.190 moko 581: if(const char* name=(gps ? EXIF_gps_tag_value2name::instance().get(tag) : EXIF_tag_value2name::instance().get(tag)))
1.148 moko 582: hash.put(name, value);
1.80 paf 583: else
1.196 moko 584: hash.put(String::Body::uitoa((int)tag), value);
1.80 paf 585: }
1.1 paf 586: }
587:
1.176 moko 588: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, uint64_t tiff_base, bool gps) {
1.91 paf 589: const char* buf;
590: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80 paf 591: return;
1.91 paf 592: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80 paf 593:
594: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
595: for(int i=0; i<directory_entry_count; i++) {
596: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
597: return;
598:
1.118 misha 599: parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf, gps);
1.80 paf 600: }
601: // then goes: LLLLLLLL Offset to next IFD [not going there]
602: }
603:
1.162 moko 604: static Value* parse_exif(Measure_reader& reader) {
1.91 paf 605: const char* buf;
1.80 paf 606:
1.175 moko 607: uint64_t tiff_base=reader.tell();
1.80 paf 608: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
609: return 0;
610:
611: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
612: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
613:
614: uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
1.174 moko 615: reader.seek(tiff_base+first_IFD_offset);
1.80 paf 616:
1.91 paf 617: VHash* vhash=new VHash;
1.80 paf 618:
619: // IFD
1.91 paf 620: parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80 paf 621:
1.91 paf 622: return vhash;
1.80 paf 623: }
624:
1.163 moko 625: static Value* parse_xmp(Measure_reader& reader, ushort xmp_length, Measure_info &info) {
1.162 moko 626: const char* buf;
627: if(reader.read(buf, xmp_length)<xmp_length)
628: return 0;
1.163 moko 629:
630: String::C xmp = Charset::transcode(String::C(pa_strdup(buf, xmp_length), xmp_length), *info.xmp_charset, pa_thread_request().charsets.source());
631: return new VString(*new String(xmp, String::L_TAINTED));
1.162 moko 632: }
633:
634: static void measure_jpeg(const String& origin_string, Measure_reader& reader, Measure_info &info) {
1.2 paf 635: // JFIF format markers
1.80 paf 636: const uchar MARKER=0xFF;
637: const uchar CODE_SIZE_A=0xC0;
638: const uchar CODE_SIZE_B=0xC1;
639: const uchar CODE_SIZE_C=0xC2;
640: const uchar CODE_SIZE_D=0xC3;
1.162 moko 641: const uchar CODE_APP1=0xE1;
1.2 paf 642:
1.91 paf 643: const char* buf;
1.18 paf 644: const size_t prefix_size=2;
1.31 parser 645: if(reader.read(buf, prefix_size)<prefix_size)
1.155 moko 646: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - too small");
1.80 paf 647: uchar *signature=(uchar *)buf;
1.1 paf 648:
1.31 parser 649: if(!(signature[0]==0xFF && signature[1]==0xD8))
1.155 moko 650: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - wrong signature");
1.31 parser 651:
652: while(true) {
1.177 moko 653: uint64_t segment_base=reader.tell()+2/*marker,code*/;
1.31 parser 654: if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79 paf 655: break;
1.31 parser 656: JPG_Segment_head *head=(JPG_Segment_head *)buf;
657:
1.162 moko 658: // Verify that it's a valid segment.
1.31 parser 659: if(head->marker!=MARKER)
1.155 moko 660: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - marker not found");
1.79 paf 661:
1.162 moko 662: ushort segment_length=endian_to_ushort(true, head->length);
663:
1.79 paf 664: switch(head->code) {
1.162 moko 665: // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files
666: case CODE_APP1:
667: {
668: const size_t EXIF_SIG_LEN=6; // Exif\0\0
669: const size_t XMP_SIG_LEN=29; // http://ns.adobe.com/xap/1.0/\0
670:
671: if(segment_length<EXIF_SIG_LEN+2 || reader.read(buf, EXIF_SIG_LEN)<EXIF_SIG_LEN)
672: break;
673: if(memcmp(buf, "Exif\0\0", EXIF_SIG_LEN)==0){
674: if(info.exif && !*info.exif) // backward compatibility: using first segment
675: *info.exif=parse_exif(reader);
676: break;
677: }
678:
679: if(memcmp(buf, "http:/", EXIF_SIG_LEN))
680: break;
681: if(segment_length<XMP_SIG_LEN+2 || reader.read(buf, XMP_SIG_LEN-EXIF_SIG_LEN)<XMP_SIG_LEN-EXIF_SIG_LEN)
682: break;
683: if(memcmp(buf, "/ns.adobe.com/xap/1.0/\0", XMP_SIG_LEN-EXIF_SIG_LEN)==0){
684: if(info.xmp && !*info.xmp) // backward compatibility: using first segment
1.163 moko 685: *info.xmp=parse_xmp(reader, segment_length - XMP_SIG_LEN - 2 /* segment_length */, info);
1.162 moko 686: }
687:
688: }
1.1 paf 689: break;
1.31 parser 690:
1.79 paf 691: case CODE_SIZE_A:
692: case CODE_SIZE_B:
693: case CODE_SIZE_C:
694: case CODE_SIZE_D:
695: {
696: // Segments that contain size info
697: if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
1.187 moko 698: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - cannot fully read Size segment");
1.79 paf 699: JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
700:
1.162 moko 701: info.width=endian_to_ushort(true, body->width);
702: info.height=endian_to_ushort(true, body->height);
703: }
1.80 paf 704: return;
1.79 paf 705: };
706:
1.174 moko 707: reader.seek(segment_base + segment_length);
1.31 parser 708: }
709:
1.155 moko 710: throw Exception(IMAGE_FORMAT, &origin_string, "broken JPEG file - size frame not found");
1.1 paf 711: }
712:
1.169 moko 713: ///*********************************************** TIFF
714:
1.165 moko 715: static bool parse_tiff_IFD(bool is_big, Measure_reader& reader, Measure_info &info) {
716: const char* buf;
717: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
718: return false;
719: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
720:
721: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
722: for(int i=0; i<directory_entry_count; i++) {
723: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
724: return false;
725:
726: JPG_Exif_IFD_entry *entry=(JPG_Exif_IFD_entry *)buf;
727: ushort entry_tag=endian_to_ushort(is_big, entry->tag);
728:
729: if(entry_tag == 256 || entry_tag == 257){
730: ushort entry_format=endian_to_ushort(is_big, entry->format);
731: if(entry_format != 3 && entry_format != 4 || endian_to_uint(is_big, entry->components_count) != 1)
732: return false;
733: uint value = (entry_format == 3) ? endian_to_ushort(is_big, entry->value_or_offset_to_it) : endian_to_uint(is_big, entry->value_or_offset_to_it);
1.178 moko 734: (entry_tag == 256) ? info.width=(short)value : info.height=(short)value;
1.165 moko 735: if(info.width && info.height)
736: return true;
737: }
738: }
739:
740: return false;
741: // then goes: LLLLLLLL Offset to next IFD [not going there]
742: }
743:
744: static void measure_tiff(const String& origin_string, Measure_reader& reader, Measure_info &info) {
745: const char* buf;
746:
747: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
748: throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - too small");
749:
750: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
751:
752: if(strncmp(head->byte_align_identifier, "II", 2)!=0 && strncmp(head->byte_align_identifier, "MM", 2)!=0)
753: throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - wrong signature");
754:
755: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
756:
757: if(endian_to_ushort(is_big, head->signature) != 42)
758: throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - wrong signature");
759:
1.174 moko 760: reader.seek(endian_to_uint(is_big, head->first_IFD_offset));
1.165 moko 761: if(!parse_tiff_IFD(is_big, reader, info))
762: throw Exception(IMAGE_FORMAT, &origin_string, "broken TIFF file - size field entry not found");
763: }
764:
1.169 moko 765:
766: ///*********************************************** GIF
767:
768: struct GIF_Header {
769: char signature[3]; // 'GIF'
770: char version[3];
771: uchar width[2];
772: uchar height[2];
773: char dif;
774: char fonColor;
775: char nulls;
776: };
777:
1.164 moko 778: static void measure_gif(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
779:
780: const char* buf;
781: const size_t head_size=sizeof(GIF_Header);
782: if(reader.read(buf, head_size)<head_size)
783: throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - too small");
784: GIF_Header *head=(GIF_Header *)buf;
785:
786: if(strncmp(head->signature, "GIF", 3)!=0)
787: throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - wrong signature");
788:
789: width=endian_to_ushort(false, head->width);
790: height=endian_to_ushort(false, head->height);
791: }
792:
1.169 moko 793:
794: ///*********************************************** PNG
795:
796: struct PNG_Header {
797: char dummy[12];
798: char signature[4]; //< must be "IHDR"
799: uchar high_width[2]; //< image width high bytes [we ignore for now]
800: uchar width[2]; //< image width low bytes
801: uchar high_height[2]; //< image height high bytes [we ignore for now]
802: uchar height[4]; //< image height
803: };
804:
1.152 moko 805: static void measure_png(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
1.72 paf 806:
1.91 paf 807: const char* buf;
1.102 paf 808: const size_t head_size=sizeof(PNG_Header);
809: if(reader.read(buf, head_size)<head_size)
1.152 moko 810: throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - too small");
1.72 paf 811: PNG_Header *head=(PNG_Header *)buf;
812:
813: if(strncmp(head->signature, "IHDR", 4)!=0)
1.152 moko 814: throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - wrong signature");
1.72 paf 815:
1.80 paf 816: width=endian_to_ushort(true, head->width);
817: height=endian_to_ushort(true, head->height);
1.72 paf 818: }
819:
1.169 moko 820:
821: ///*********************************************** BMP
822:
823: struct BMP_Header {
824: char signature[2]; // 'BM'
825: uchar file_size[4];
826: uchar reserved[4];
827: uchar bitmap_offset[4];
828: uchar header_size[4];
829: uchar width[2];
830: uchar high_width[2]; //< image width high bytes [we ignore for now]
831: uchar height[2];
832: uchar high_height[2]; //< image height high bytes [we ignore for now]
833: };
834:
1.164 moko 835: static void measure_bmp(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
836:
837: const char* buf;
838: const size_t head_size=sizeof(BMP_Header);
839: if(reader.read(buf, head_size)<head_size)
840: throw Exception(IMAGE_FORMAT, &origin_string, "not BMP file - too small");
841: BMP_Header *head=(BMP_Header *)buf;
842:
843: if(strncmp(head->signature, "BM", 2)!=0)
844: throw Exception(IMAGE_FORMAT, &origin_string, "not BMP file - wrong signature");
845:
1.174 moko 846: if((uint)reader.length() != endian_to_uint(false, head->file_size))
1.164 moko 847: throw Exception(IMAGE_FORMAT, &origin_string, "not BMP file - length header and file size do not match");
848:
849: width=endian_to_ushort(false, head->width);
850: height=endian_to_ushort(false, head->height);
851: }
852:
1.168 moko 853:
1.169 moko 854: ///*********************************************** WEBP
855:
856: struct WEBP_Header {
857: char signature_riff[4]; // 'RIFF'
858: uchar file_size[4];
859: char signature[4]; // 'WEBP'
860: char format[4]; // 'VP8 ' or 'VP8L' or 'VP8X'
861: };
862:
863: struct WEBP_VP8_Chunk {
864: uchar size[4];
865: char tag[3];
866: uchar signature[3]; // 0x9D 0x01 0x2A
867: uchar width[2]; // 14 bits each
868: uchar height[2]; // 14 bits each
869: };
870:
871: struct WEBP_VP8L_Chunk {
872: uchar size[4];
873: char signature; // 0x2F
874: uchar width_height[4]; // 14 bits each
875: };
876:
877: struct WEBP_X_Chunk {
878: uchar size[4];
879: char reserved[4];
880: uchar width[3];
881: uchar height[3];
882: };
883:
1.168 moko 884: static void measure_webp(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
885: const char* buf;
886:
887: if(reader.read(buf, sizeof(WEBP_Header))<sizeof(WEBP_Header))
888: throw Exception(IMAGE_FORMAT, &origin_string, "not WEBP file - too small");
889:
890: WEBP_Header *head=(WEBP_Header *)buf;
891:
892: if(strncmp(head->signature_riff, "RIFF", 4)!=0 || strncmp(head->signature, "WEBP", 4)!=0)
893: throw Exception(IMAGE_FORMAT, &origin_string, "not WEBP file - wrong signature");
894:
895: if(strncmp(head->format, "VP8 ", 4)==0){
896: if(reader.read(buf, sizeof(WEBP_VP8_Chunk))<sizeof(WEBP_VP8_Chunk))
897: throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8 chunk");
898:
899: WEBP_VP8_Chunk *chunk=(WEBP_VP8_Chunk *)buf;
900: if (chunk->signature[0] != 0x9D || chunk->signature[1] != 0x01 || chunk->signature[2] != 0x2A)
901: throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - wrong VP8 chunk signature");
902:
903: width=endian_to_ushort(false, chunk->width) & 0x3FFF;
904: height=endian_to_ushort(false, chunk->height) & 0x3FFF;
905: } else if(strncmp(head->format, "VP8L", 4)==0){
906: if(reader.read(buf, sizeof(WEBP_VP8L_Chunk))<sizeof(WEBP_VP8L_Chunk))
907: throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8L chunk");
908:
909: WEBP_VP8L_Chunk *chunk=(WEBP_VP8L_Chunk *)buf;
910: if(chunk->signature != 0x2F)
911: throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - wrong VP8L chunk signature");
912:
913: uint wh=endian_to_uint(false, chunk->width_height);
914: width=(wh & 0x3FFF) + 1;
915: height=((wh >> 14) & 0x3FFF) + 1;
916: } else if (strncmp(head->format, "VP8X", 4)==0){
917: if(reader.read(buf, sizeof(WEBP_X_Chunk))<sizeof(WEBP_X_Chunk))
918: throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8X chunk");
919:
920: WEBP_X_Chunk *chunk=(WEBP_X_Chunk *)buf;
921:
922: width=endian_to_ushort(false, chunk->width) + 1; // we ignore third byte to simplify code
923: height=endian_to_ushort(false, chunk->height) + 1; // we ignore third byte to simplify code
924: } else throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - invalid chunk signature");
925: }
926:
1.170 moko 927:
928: ///*********************************************** MP4
929:
930: struct MP4_Header {
931: uchar size[4];
932: char signature[4]; // 'ftyp' in first chunk
933: };
934:
1.173 moko 935: struct MP4_ExtSize {
936: uchar high[4];
937: uchar low[4];
938: };
939:
940: struct MP4_Tkhd {
941: uchar width[4];
942: uchar height[4];
943: };
944:
1.174 moko 945: static bool measure_mp4(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height, uint64_t anext, const char* lastTkhd=NULL) {
1.170 moko 946: for(bool first=anext==0;;){
947: const char* buf;
1.174 moko 948: uint64_t next=reader.tell();
1.173 moko 949:
950: if(reader.read(buf, sizeof(MP4_Header))<sizeof(MP4_Header))
1.172 moko 951: throw Exception(IMAGE_FORMAT, &origin_string, first ? "not MP4 file - too small" : "broken MP4 file - truncated chunk header");
1.170 moko 952:
953: MP4_Header *head=(MP4_Header *)buf;
1.174 moko 954: uint64_t size=endian_to_uint(true, head->size);
1.170 moko 955:
1.173 moko 956: if(size==1){
957: if(reader.read(buf, sizeof(MP4_ExtSize))<sizeof(MP4_ExtSize))
958: throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - truncated chunk extended size header");
959: MP4_ExtSize *ext_size=(MP4_ExtSize *)buf;
1.174 moko 960: size=((uint64_t)endian_to_uint(true, ext_size->high) << 32) + endian_to_uint(true, ext_size->low);
1.201 ! moko 961: } else if(size==0) // the chunk extends to the end (of file or container)
! 962: size=(anext ? anext : reader.length())-next;
1.173 moko 963: next+=size;
1.170 moko 964:
1.201 ! moko 965: // the chunk can't end before its header does (size is too small or wraps around): we would never get to the end
! 966: if(next<reader.tell())
! 967: throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad chunk size");
! 968:
1.170 moko 969: if(first){
970: if(strncmp(head->signature, "ftyp", 4)!=0)
971: throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature");
972: first=false;
1.174 moko 973: anext=reader.length(); // to avoid reading beyond EOF
1.170 moko 974: } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) {
1.201 ! moko 975: reader.budget.use(Measure_budget::MP4_DEPTH);
1.170 moko 976: if(measure_mp4(origin_string, reader, width, height, next, lastTkhd))
977: return true;
1.201 ! moko 978: reader.budget.release(Measure_budget::MP4_DEPTH);
1.170 moko 979: } else if(strncmp(head->signature, "tkhd", 4)==0) {
980: if(size>8){
1.174 moko 981: reader.seek(next-8);
1.173 moko 982: if(reader.read(lastTkhd, sizeof(MP4_Tkhd))<sizeof(MP4_Tkhd))
1.170 moko 983: throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad tkhd chunk");
984: }
985: } else if (strncmp(head->signature, "hdlr", 4)==0) {
986: if(size>12){
987: const char* hdlr;
988: if(reader.read(hdlr, 12)<12)
989: throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad hdlr chunk");
1.173 moko 990: if(lastTkhd && strncmp(hdlr+8, "vide", 4)==0) {
991: MP4_Tkhd *tkhd=(MP4_Tkhd *)lastTkhd;
992: width=endian_to_ushort(true, tkhd->width);
993: height=endian_to_ushort(true, tkhd->height);
1.170 moko 994: return true;
995: }
996: }
997: }
998: if(anext && next>=anext)
999: break;
1.174 moko 1000: reader.seek(next);
1.170 moko 1001: }
1002: return false;
1003: }
1004:
1005: static void measure_mp4(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
1006: if(!measure_mp4(origin_string, reader, width, height, 0))
1007: throw Exception(IMAGE_FORMAT, &origin_string, "unsupported MP4 file - size not found");
1008: }
1009:
1.169 moko 1010: ///*********************************************** measure center
1.1 paf 1011:
1.162 moko 1012: static void measure(const String& file_name, Measure_reader& reader, Measure_info &info) {
1.128 misha 1013: const char* file_name_cstr=file_name.taint_cstr(String::L_FILE_SPEC);
1.91 paf 1014: if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1 paf 1015: cext++;
1016: if(strcasecmp(cext, "GIF")==0)
1.162 moko 1017: measure_gif(file_name, reader, info.width, info.height);
1.165 moko 1018: else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.162 moko 1019: measure_jpeg(file_name, reader, info);
1.72 paf 1020: else if(strcasecmp(cext, "PNG")==0)
1.162 moko 1021: measure_png(file_name, reader, info.width, info.height);
1.164 moko 1022: else if(strcasecmp(cext, "BMP")==0)
1023: measure_bmp(file_name, reader, info.width, info.height);
1.168 moko 1024: else if(strcasecmp(cext, "WEBP")==0)
1025: measure_webp(file_name, reader, info.width, info.height);
1.165 moko 1026: else if(strcasecmp(cext, "TIF")==0 || strcasecmp(cext, "TIFF")==0)
1027: measure_tiff(file_name, reader, info);
1.171 moko 1028: else if(strcasecmp(cext, "MP4")==0 || strcasecmp(cext, "MOV")==0)
1.181 moko 1029: if(info.video)
1030: measure_mp4(file_name, reader, info.width, info.height);
1031: else
1032: throw Exception(IMAGE_FORMAT, &file_name, "handling disabled for file name extension '%s'", cext);
1.1 paf 1033: else
1.181 moko 1034: throw Exception(IMAGE_FORMAT, &file_name, "unhandled file name extension '%s'", cext);
1.1 paf 1035: } else
1.187 moko 1036: throw Exception(IMAGE_FORMAT, &file_name, "cannot determine file type - no file name extension");
1.1 paf 1037: }
1038:
1.77 paf 1039: // methods
1.1 paf 1040:
1.201 ! moko 1041: static void file_measure_action(struct stat& finfo, int f, const String& file_spec, void *context) {
! 1042: Measure_file_reader reader(f, file_spec, finfo.st_size);
1.162 moko 1043: measure(file_spec, reader, *static_cast<Measure_info *>(context));
1.1 paf 1044: }
1045:
1.91 paf 1046: static void _measure(Request& r, MethodParams& params) {
1047: Value& data=params.as_no_junction(0, "data must not be code");
1.1 paf 1048:
1.91 paf 1049: Value* exif=0;
1.162 moko 1050: Value* xmp=0;
1.181 moko 1051: Measure_info info={ 0, 0, 0, 0, &pa_UTF8_charset, false };
1.163 moko 1052:
1053: if(params.count()>1)
1054: if(HashStringValue* options=params.as_hash(1, "methods options")) {
1055: for(HashStringValue::Iterator i(*options); i; i.next() ){
1056: String::Body key=i.key();
1057: Value* value=i.value();
1058: if(key == "exif") {
1059: if(r.process(*value).as_bool())
1060: info.exif=&exif;
1.198 moko 1061: } else if(key == "xmp") {
1.163 moko 1062: if(r.process(*value).as_bool())
1063: info.xmp=&xmp;
1.198 moko 1064: } else if(key == "xmp-charset") {
1.163 moko 1065: info.xmp_charset=&pa_charsets.get(value->as_string());
1.198 moko 1066: } else if(key == "video") {
1.181 moko 1067: info.video=r.process(*value).as_bool();
1.198 moko 1068: } else
1069: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.163 moko 1070: }
1071: }
1.162 moko 1072:
1.91 paf 1073: const String* file_name;
1.162 moko 1074:
1075: if(file_name=data.get_string()) {
1.167 moko 1076: file_read_action_under_lock(r.full_disk_path(*file_name), "measure", file_measure_action, &info);
1.1 paf 1077: } else {
1.182 moko 1078: VFile* vfile=data.as_vfile();
1.91 paf 1079: file_name=&vfile->fields().get(name_name)->as_string();
1.162 moko 1080: Measure_buf_reader reader(vfile->value_ptr(), vfile->value_size(), *file_name);
1081: measure(*file_name, reader, info);
1.1 paf 1082: }
1083:
1.162 moko 1084: GET_SELF(r, VImage).set(file_name, info.width, info.height, 0, exif, xmp);
1.1 paf 1085: }
1086:
1.161 moko 1087: static void append_attrib_pair(String &tag, String::Body key, Value* value){
1088: // skip border attribute with empty value
1089: if(key=="border" && !value->is_defined())
1.4 paf 1090: return;
1091:
1.133 misha 1092: // src="a.gif" width="123" ismap[=-1]
1.161 moko 1093: tag << " " << key;
1.91 paf 1094: if(value->is_string() || value->as_int()>=0)
1.161 moko 1095: tag << "=\"" << value->as_string() << "\"";
1.3 paf 1096: }
1.161 moko 1097:
1.91 paf 1098: static void _html(Request& r, MethodParams& params) {
1099: String tag;
1.3 paf 1100: tag << "<img";
1.4 paf 1101:
1.91 paf 1102: const HashStringValue& fields=GET_SELF(r, VImage).fields();
1103: HashStringValue* attribs=0;
1.4 paf 1104:
1.91 paf 1105: if(params.count()) {
1.69 paf 1106: // for backward compatibility: someday was ^html{}
1.154 moko 1107: Value& vattribs=r.process(params[0]);
1.145 moko 1108: if(!vattribs.is_string()) { // allow empty
1.100 paf 1109: if((attribs=vattribs.get_hash())) {
1.161 moko 1110: for(HashStringValue::Iterator i(*attribs); i; i.next() )
1111: append_attrib_pair(tag, i.key(), i.value());
1.39 parser 1112: } else
1.155 moko 1113: throw Exception(PARSER_RUNTIME, 0, "attributes must be hash");
1.145 moko 1114: }
1.39 parser 1115: }
1.4 paf 1116:
1.161 moko 1117: for(HashStringValue::Iterator i(fields); i; i.next() ){
1118: String::Body key=i.key();
1119: // skip user-specified attributes
1120: if(attribs && attribs->get(key))
1121: continue;
1122: // allow only html attributes (to exclude exif, line-*)
1123: if(key=="src" || key=="width" || key=="height" || key=="border")
1124: append_attrib_pair(tag, key, i.value());
1.91 paf 1125: }
1.161 moko 1126:
1.6 paf 1127: tag << " />";
1.157 moko 1128: r.write(tag);
1.3 paf 1129: }
1.8 paf 1130:
1.68 paf 1131: /// @test wrap FILE to auto-object
1.155 moko 1132: static gdImage* load(Request& r, const String& file_name){
1.167 moko 1133: const char* file_name_cstr=r.full_disk_path(file_name).taint_cstr(String::L_FILE_SPEC);
1.159 moko 1134: if(FILE *f=pa_fopen(file_name_cstr, "rb")) {
1.91 paf 1135: gdImage* image=new gdImage;
1136: bool ok=image->CreateFromGif(f);
1.16 paf 1137: fclose(f);
1.23 paf 1138: if(!ok)
1.155 moko 1139: throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");
1.91 paf 1140: return image;
1.16 paf 1141: } else {
1.187 moko 1142: throw Exception("file.missing", 0, "cannot open '%s'", file_name_cstr);
1.16 paf 1143: }
1144: }
1145:
1146:
1.91 paf 1147: static void _load(Request& r, MethodParams& params) {
1.194 moko 1148: const String& file_name=params.as_file_name(0);
1.6 paf 1149:
1.91 paf 1150: gdImage* image=load(r, file_name);
1151: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 1152: }
1153:
1.91 paf 1154: static void _create(Request& r, MethodParams& params) {
1155: int width=params.as_int(0, "width must be int", r);
1156: int height=params.as_int(1, "height must be int", r);
1.8 paf 1157: int bgcolor_value=0xffFFff;
1.91 paf 1158: if(params.count()>2)
1159: bgcolor_value=params.as_int(2, "color must be int", r);
1160: gdImage* image=new gdImage;
1161: image->Create(width, height);
1162: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
1163: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 1164: }
1165:
1.91 paf 1166: static void _gif(Request& r, MethodParams& params) {
1.108 paf 1167: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 1168:
1.138 misha 1169: const String *file_name=params.count()>0?¶ms.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10 paf 1170:
1.108 paf 1171: gdBuf buf=image.Gif();
1.6 paf 1172:
1.91 paf 1173: VFile& vfile=*new VFile;
1.137 misha 1174:
1.142 moko 1175: vfile.set_binary(false/*not tainted*/, (const char *)buf.ptr, buf.size, file_name, new VString(*new String("image/gif")));
1.137 misha 1176:
1.157 moko 1177: r.write(vfile);
1.6 paf 1178: }
1179:
1.91 paf 1180: static void _line(Request& r, MethodParams& params) {
1.108 paf 1181: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 1182:
1.108 paf 1183: image.Line(
1.91 paf 1184: params.as_int(0, "x0 must be int", r),
1185: params.as_int(1, "y0 must be int", r),
1186: params.as_int(2, "x1 must be int", r),
1187: params.as_int(3, "y1 must be int", r),
1.108 paf 1188: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 1189: }
1190:
1.91 paf 1191: static void _fill(Request& r, MethodParams& params) {
1.108 paf 1192: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 1193:
1.108 paf 1194: image.Fill(
1.91 paf 1195: params.as_int(0, "x must be int", r),
1196: params.as_int(1, "y must be int", r),
1.108 paf 1197: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 1198: }
1199:
1.91 paf 1200: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 1201: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1202:
1.108 paf 1203: image.Rectangle(
1.91 paf 1204: params.as_int(0, "x0 must be int", r),
1205: params.as_int(1, "y0 must be int", r),
1206: params.as_int(2, "x1 must be int", r),
1207: params.as_int(3, "y1 must be int", r),
1.108 paf 1208: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 1209: }
1210:
1.91 paf 1211: static void _bar(Request& r, MethodParams& params) {
1.108 paf 1212: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1213:
1.108 paf 1214: image.FilledRectangle(
1.91 paf 1215: params.as_int(0, "x0 must be int", r),
1216: params.as_int(1, "y0 must be int", r),
1217: params.as_int(2, "x1 must be int", r),
1218: params.as_int(3, "y1 must be int", r),
1.108 paf 1219: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 1220: }
1221:
1.44 parser 1222: #ifndef DOXYGEN
1.155 moko 1223: static void add_point(Table::element_type row, gdImage::Point **p) {
1.110 paf 1224: if(row->count()!=2)
1.155 moko 1225: throw Exception(0, 0, "coordinates table must contain two columns: x and y values");
1.91 paf 1226: (**p).x=row->get(0)->as_int();
1227: (**p).y=row->get(1)->as_int();
1.44 parser 1228: (*p)++;
1229: }
1230: #endif
1.135 misha 1231: #ifndef DOXYGEN
1.155 moko 1232: static void add_point(int x, int y, gdImage::Point **p) {
1.135 misha 1233: (**p).x=x;
1234: (**p).y=y;
1235: (*p)++;
1236: }
1237: #endif
1.91 paf 1238: static void _replace(Request& r, MethodParams& params) {
1.135 misha 1239: int src_color=params.as_int(0, "src color must be int", r);
1240: int dest_color=params.as_int(1, "dest color must be int", r);
1241:
1.108 paf 1242: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1243:
1.135 misha 1244: gdImage::Point* all_p=0;
1245: size_t count=0;
1246: if(params.count() == 3){
1.180 moko 1247: if(Table* table=params.as_table(2, "coordinates")){
1248: count=table->count();
1249: all_p=new(PointerFreeGC) gdImage::Point[count];
1250: gdImage::Point* add_p=all_p;
1251: table->for_each(add_point, &add_p);
1252: }
1.135 misha 1253: } else {
1254: int max_x=image.SX()-1;
1255: int max_y=image.SY()-1;
1256: if(max_x > 0 && max_y > 0){
1257: count=4;
1258: all_p=new(PointerFreeGC) gdImage::Point[count];
1259: gdImage::Point* add_p=all_p;
1260: add_point(0, 0, &add_p);
1261: add_point(max_x, 0, &add_p);
1262: add_point(max_x, max_y, &add_p);
1263: add_point(0, max_y, &add_p);
1264: }
1265: }
1.13 paf 1266:
1.135 misha 1267: if(count)
1268: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 1269: }
1270:
1.91 paf 1271: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 1272: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1273:
1.180 moko 1274: if(Table* table=params.as_table(1, "coordinates")){
1275: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1276: gdImage::Point *add_p=all_p;
1277: table->for_each(add_point, &add_p);
1278: image.Polygon(all_p, table->count(), image.Color(params.as_int(0, "color must be int", r)), false/*not closed*/);
1279: }
1.44 parser 1280: }
1281:
1.91 paf 1282: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 1283: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1284:
1.180 moko 1285: if(Table* table=(Table*)params.as_table(1, "coordinates")){
1286: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1287: gdImage::Point *add_p=all_p;
1288: table->for_each(add_point, &add_p);
1289: image.Polygon(all_p, table->count(), image.Color(params.as_int(0, "color must be int", r)));
1290: }
1.13 paf 1291: }
1292:
1.91 paf 1293: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 1294: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1295:
1.180 moko 1296: if(Table* table=(Table*)params.as_table(1, "coordinates")){
1297: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1298: gdImage::Point *add_p=all_p;
1299: table->for_each(add_point, &add_p);
1300: image.FilledPolygon(all_p, table->count(), image.Color(params.as_int(0, "color must be int", r)));
1301: }
1.12 paf 1302: }
1303:
1.16 paf 1304: // font
1305:
1.85 paf 1306: #define Y(y)(y+index*height)
1.21 paf 1307:
1.91 paf 1308: // Font class
1309:
1.155 moko 1310: Font::Font(Charset& asource_charset, const String& aalphabet, gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.158 moko 1311: letterspacing(aletterspacing),
1.132 misha 1312: height(aheight),
1313: monospace(amonospace),
1314: spacebarspace(aspacebarspace),
1.100 paf 1315: ifont(aifont),
1.158 moko 1316: alphabet(aalphabet),
1317: fsource_charset(asource_charset){
1.132 misha 1318:
1319: if(fsource_charset.isUTF8()){
1320: size_t index=0;
1321: for(UTF8_string_iterator i(alphabet); i.has_next(); )
1322: fletter2index.put_dont_replace(i.next(), index++);
1323: }
1.91 paf 1324: }
1325:
1326: /* ******************************** char ********************************** */
1327:
1328: size_t Font::index_of(char ch) {
1329: if(ch==' ') return STRING_NOT_FOUND;
1330: return alphabet.pos(ch);
1331: }
1332:
1.132 misha 1333: size_t Font::index_of(XMLCh ch) {
1334: if(ch==' ') return STRING_NOT_FOUND;
1335: return fletter2index.get(ch);
1336: }
1337:
1.91 paf 1338: int Font::index_width(size_t index) {
1339: if(index==STRING_NOT_FOUND)
1340: return spacebarspace;
1341: int tr=ifont->GetTransparent();
1342: for(int x=ifont->SX()-1; x>=0; x--) {
1343: for(int y=0; y<height; y++)
1344: if(ifont->GetPixel(x, Y(y))!=tr)
1345: return x+1;
1.16 paf 1346: }
1.91 paf 1347: return 0;
1348: }
1349:
1.108 paf 1350: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1351: if(index!=STRING_NOT_FOUND)
1.108 paf 1352: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1353: }
1354:
1355: /* ******************************** string ********************************** */
1356:
1357: int Font::step_width(int index) {
1.125 misha 1358: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1359: }
1360:
1361: // counts trailing letter_spacing, consider this OK. useful for contiuations
1362: int Font::string_width(const String& s){
1363: const char* cstr=s.cstr();
1364: int result=0;
1.132 misha 1365:
1366: if(fsource_charset.isUTF8()){
1367: for(UTF8_string_iterator i(s); i.has_next(); )
1368: result+=step_width(index_of(i.next()));
1369: } else {
1370: for(const char* current=cstr; *current; current++)
1371: result+=step_width(index_of(*current));
1372: }
1373:
1.91 paf 1374: return result;
1375: }
1376:
1.108 paf 1377: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1378: const char* cstr=s.cstr();
1.132 misha 1379:
1380: if(fsource_charset.isUTF8()){
1381: for(UTF8_string_iterator i(s); i.has_next(); ){
1382: size_t index=index_of(i.next());
1383: index_display(image, x, y, index);
1384: x+=step_width(index);
1385: }
1386: } else {
1387: for(const char* current=cstr; *current; current++) {
1388: size_t index=index_of(*current);
1389: index_display(image, x, y, index);
1390: x+=step_width(index);
1391: }
1.87 paf 1392: }
1.91 paf 1393: }
1.87 paf 1394:
1.91 paf 1395: //
1.35 parser 1396:
1.16 paf 1397:
1.91 paf 1398: static void _font(Request& r, MethodParams& params) {
1.195 moko 1399: const String& alphabet=params.as_string(0, "alphabet must be string");
1.131 misha 1400: size_t alphabet_length=alphabet.length(r.charsets.source());
1401: if(!alphabet_length)
1.155 moko 1402: throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");
1.84 paf 1403:
1.194 moko 1404: gdImage* image=load(r, params.as_file_name(1));
1.125 misha 1405:
1.126 misha 1406: int spacebar_width=image->SX();
1.125 misha 1407: int monospace_width=0; // proportional
1408: int letter_spacing=1;
1409: if(params.count()>2){
1.144 moko 1410: if(HashStringValue* options=params[2].get_hash()){
1.125 misha 1411: // third option is hash
1412: if(params.count()>3)
1.144 moko 1413: throw Exception(PARSER_RUNTIME, 0, "too many params were specified");
1.125 misha 1414: int valid_options=0;
1415: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1416: valid_options++;
1.153 moko 1417: spacebar_width=r.process(*vspacebar_width).as_int();
1.125 misha 1418: }
1419: if(Value* vmonospace_width=options->get(monospace_width_name)){
1420: valid_options++;
1.153 moko 1421: monospace_width=r.process(*vmonospace_width).as_int();
1.125 misha 1422: if(!monospace_width)
1423: monospace_width=image->SX();
1424: }
1425: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1426: valid_options++;
1.153 moko 1427: letter_spacing=r.process(*vletter_spacing).as_int();
1.125 misha 1428: }
1429: if(valid_options!=options->count())
1.134 misha 1430: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1431: } else {
1432: // backward
1.144 moko 1433: spacebar_width=params.as_int(2, "param must be int or hash", r);
1.125 misha 1434: if(params.count()>3) {
1435: monospace_width=params.as_int(3, "monospace_width must be int", r);
1436: if(!monospace_width)
1437: monospace_width=image->SX();
1438: }
1439: }
1440: }
1441:
1.131 misha 1442: if(int remainder=image->SY() % alphabet_length)
1.155 moko 1443: throw Exception(PARSER_RUNTIME, 0, "font-file height(%d) not divisable by alphabet size(%d), remainder=%d", image->SY(), alphabet_length, remainder);
1.37 parser 1444:
1.155 moko 1445: GET_SELF(r, VImage).set_font(new Font(r.charsets.source(), alphabet, image, image->SY() / alphabet_length, monospace_width, spacebar_width, letter_spacing));
1.16 paf 1446: }
1447:
1.91 paf 1448: static void _text(Request& r, MethodParams& params) {
1449: int x=params.as_int(0, "x must be int", r);
1450: int y=params.as_int(1, "y must be int", r);
1.195 moko 1451: const String& s=params.as_string(2, "text must be string");
1.16 paf 1452:
1.91 paf 1453: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1454: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1455: }
1456:
1.91 paf 1457: static void _length(Request& r, MethodParams& params) {
1.195 moko 1458: const String& s=params.as_string(0, "text must be string");
1.47 parser 1459:
1.91 paf 1460: VImage& vimage=GET_SELF(r, VImage);
1.157 moko 1461: r.write(*new VInt(vimage.font().string_width(s)));
1.47 parser 1462: }
1463:
1.91 paf 1464: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1465: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1466:
1.108 paf 1467: image.Arc(
1.91 paf 1468: params.as_int(0, "center_x must be int", r),
1469: params.as_int(1, "center_y must be int", r),
1470: params.as_int(2, "width must be int", r),
1471: params.as_int(3, "height must be int", r),
1472: params.as_int(4, "start degrees must be int", r),
1473: params.as_int(5, "end degrees must be int", r),
1.108 paf 1474: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1475: }
1476:
1.91 paf 1477: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1478: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1479:
1.108 paf 1480: image.Sector(
1.91 paf 1481: params.as_int(0, "center_x must be int", r),
1482: params.as_int(1, "center_y must be int", r),
1483: params.as_int(2, "width must be int", r),
1484: params.as_int(3, "height must be int", r),
1485: params.as_int(4, "start degrees must be int", r),
1486: params.as_int(5, "end degrees must be int", r),
1.108 paf 1487: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1488: }
1489:
1.91 paf 1490: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1491: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1492:
1.91 paf 1493: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1494: image.Arc(
1.91 paf 1495: params.as_int(0, "center_x must be int", r),
1496: params.as_int(1, "center_y must be int", r),
1.50 parser 1497: size, //w
1498: size, //h
1.48 parser 1499: 0, //s
1500: 360, //e
1.108 paf 1501: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1502: }
1503:
1.108 paf 1504: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1505: Value& value=params.as_no_junction(index, msg);
1.53 parser 1506:
1.186 moko 1507: if(VImage* vimage=dynamic_cast<VImage *>(&value)) {
1508: return vimage->image();
1.75 paf 1509: } else
1.155 moko 1510: throw Exception(PARSER_RUNTIME, 0, msg);
1.53 parser 1511: }
1512:
1.91 paf 1513: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1514: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1515:
1.108 paf 1516: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1517:
1.91 paf 1518: int sx=params.as_int(1, "src_x must be int", r);
1519: int sy=params.as_int(2, "src_y must be int", r);
1520: int sw=params.as_int(3, "src_w must be int", r);
1521: int sh=params.as_int(4, "src_h must be int", r);
1522: int dx=params.as_int(5, "dest_x must be int", r);
1523: int dy=params.as_int(6, "dest_y must be int", r);
1524: if(params.count()>1+2+2+2) {
1525: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1526: int dh=(int)(params.count()>1+2+2+2+1?
1527: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1528: int tolerance=params.count()>1+2+2+2+2?
1529: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1530:
1.108 paf 1531: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1532: } else
1.108 paf 1533: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1534: }
1535:
1536: static void _pixel(Request& r, MethodParams& params) {
1537: gdImage& image=GET_SELF(r, VImage).image();
1538:
1.109 paf 1539: int x=params.as_int(0, "x must be int", r);
1540: int y=params.as_int(1, "y must be int", r);
1541:
1542: if(params.count()>2) {
1543: image.SetPixel(x, y,
1544: image.Color(params.as_int(2, "color must be int", r)));
1545: } else
1.157 moko 1546: r.write(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1547: }
1548:
1549:
1.22 paf 1550: // constructor
1551:
1.91 paf 1552: MImage::MImage(): Methoded("image") {
1.1 paf 1553: // ^image:measure[DATA]
1.163 moko 1554: // ^image:measure[DATA; $.exif(false) $.xmp(false) $.xmp-charset[UTF-8] ]
1555: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 2);
1.3 paf 1556:
1.25 paf 1557: // ^image.html[]
1558: // ^image.html[hash]
1.22 paf 1559: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1560:
1.25 paf 1561: // ^image.load[background.gif]
1.22 paf 1562: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1563:
1.25 paf 1564: // ^image.create[width;height] bgcolor=white
1565: // ^image.create[width;height;bgcolor]
1.22 paf 1566: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1567:
1.25 paf 1568: // ^image.gif[]
1.95 paf 1569: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1570:
1.25 paf 1571: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1572: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1573:
1.25 paf 1574: // ^image.fill(x;y;color)
1.22 paf 1575: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1576:
1.25 paf 1577: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1578: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1579:
1.25 paf 1580: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1581: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1582:
1.44 parser 1583: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1584: // ^image.replace(color-source;color-dest)
1585: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1586:
1587: // ^image.polyline(color)[table x:y]
1588: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1589:
1.44 parser 1590: // ^image.polygon(color)[table x:y]
1591: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1592:
1.44 parser 1593: // ^image.polybar(color)[table x:y]
1594: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1595:
1.125 misha 1596: // ^image.font[alPHAbet;font-file-name.gif]
1597: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1598: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1599: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1600: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1601:
1.125 misha 1602: // ^image.text(x;y)[text]
1.22 paf 1603: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1604:
1.125 misha 1605: // ^image.length[text]
1.47 parser 1606: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1607:
1.48 parser 1608: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1609: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1610:
1611: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1612: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1613:
1614: // ^image.circle(center x;center y;r;color)
1615: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1616:
1.56 parser 1617: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1618: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1619:
1620: // ^image.pixel(x;y)[(color)]
1.109 paf 1621: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1622: }
E-mail: