Annotation of parser3/src/classes/image.C, revision 1.163
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.160 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.66 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://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.163 ! moko 29: volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.162 2017/05/16 14:42:07 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: }
192: } exif_tag_value2name;
193:
1.118 misha 194: class EXIF_gps_tag_value2name: public Hash<int, const char*> {
195: public:
196: EXIF_gps_tag_value2name() {
197: EXIF_TAG(0x0, GPSVersionID);
198: EXIF_TAG(0x1, GPSLatitudeRef);
199: EXIF_TAG(0x2, GPSLatitude);
200: EXIF_TAG(0x3, GPSLongitudeRef);
201: EXIF_TAG(0x4, GPSLongitude);
202: EXIF_TAG(0x5, GPSAltitudeRef);
203: EXIF_TAG(0x6, GPSAltitude);
204: EXIF_TAG(0x7, GPSTimeStamp);
205: EXIF_TAG(0x8, GPSSatellites);
206: EXIF_TAG(0x9, GPSStatus);
207: EXIF_TAG(0xA, GPSMeasureMode);
208: EXIF_TAG(0xB, GPSDOP);
209: EXIF_TAG(0xC, GPSSpeedRef);
210: EXIF_TAG(0xD, GPSSpeed);
211: EXIF_TAG(0xE, GPSTrackRef);
212: EXIF_TAG(0xF, GPSTrack);
213: EXIF_TAG(0x10, GPSImgDirectionRef);
214: EXIF_TAG(0x11, GPSImgDirection);
215: EXIF_TAG(0x12, GPSMapDatum);
216: EXIF_TAG(0x13, GPSDestLatitudeRef);
217: EXIF_TAG(0x14, GPSDestLatitude);
218: EXIF_TAG(0x15, GPSDestLongitudeRef);
219: EXIF_TAG(0x16, GPSDestLongitude);
220: EXIF_TAG(0x17, GPSDestBearingRef);
221: EXIF_TAG(0x18, GPSDestBearing);
222: EXIF_TAG(0x19, GPSDestDistanceRef);
223: EXIF_TAG(0x1A, GPSDestDistance);
224: EXIF_TAG(0x1B, GPSProcessingMethod);
225: EXIF_TAG(0x1C, GPSAreaInformation);
226: EXIF_TAG(0x1D, GPSDateStamp);
227: EXIF_TAG(0x1E, GPSDifferential);
228: }
229: } exif_gps_tag_value2name;
230:
231: #undef EXIF_TAG
1.91 paf 232:
1.43 parser 233: #ifndef DOXYGEN
1.1 paf 234: class Measure_reader {
235: public:
1.91 paf 236: virtual size_t read(const char* &buf, size_t limit)=0;
1.79 paf 237: virtual void seek(long value, int whence)=0;
238: virtual long tell()=0;
1.77 paf 239: };
1.1 paf 240:
1.77 paf 241: class Measure_file_reader: public Measure_reader {
1.152 moko 242: const String& file_name;
1.91 paf 243: int f;
244:
1.77 paf 245: public:
1.152 moko 246: Measure_file_reader(int af, const String& afile_name):
247: file_name(afile_name), f(af) {
1.1 paf 248: }
249:
1.91 paf 250: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 251: if(limit==0)
1.1 paf 252: return 0;
1.77 paf 253:
1.91 paf 254: char* lbuf=new(PointerFreeGC) char[limit];
1.143 moko 255: ssize_t read_size=::read(f, lbuf, limit); abuf=lbuf;
256: if(read_size<0)
257: throw Exception(0, &file_name, "measure read failed: %s (%d)", strerror(errno), errno);
1.1 paf 258: return read_size;
259: }
260:
1.91 paf 261: override void seek(long value, int whence) {
1.79 paf 262: if(lseek(f, value, whence)<0)
1.152 moko 263: throw Exception(IMAGE_FORMAT, &file_name, "seek(value=%ld, whence=%d) failed: %s (%d)", value, whence, strerror(errno), errno);
1.77 paf 264: }
265:
1.91 paf 266: override long tell() { return lseek(f, 0, SEEK_CUR); }
1.79 paf 267:
1.77 paf 268: };
269:
270: class Measure_buf_reader: public Measure_reader {
1.91 paf 271:
272: const char* buf; size_t size;
273: const String& file_name;
274:
275: size_t offset;
276:
1.77 paf 277: public:
1.91 paf 278: Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):
1.77 paf 279: buf(abuf), size(asize), file_name(afile_name), offset(0) {
280: }
281:
1.91 paf 282: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 283: size_t to_read=min(limit, size-offset);
1.91 paf 284: abuf=buf+offset;
1.77 paf 285: offset+=to_read;
286: return to_read;
287: }
288:
1.91 paf 289: override void seek(long value, int whence) {
1.79 paf 290: size_t new_offset;
291: switch(whence) {
292: case SEEK_CUR: new_offset=offset+value; break;
293: case SEEK_SET: new_offset=(size_t)value; break;
1.91 paf 294: default:
1.155 moko 295: throw Exception(0, 0, "whence #%d not supported", 0, whence);
1.91 paf 296: break; // never
1.79 paf 297: }
298:
1.77 paf 299: if((ssize_t)new_offset<0 || new_offset>size)
1.155 moko 300: throw Exception(IMAGE_FORMAT, &file_name, "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0",
1.79 paf 301: value, whence, new_offset, size);
1.77 paf 302: offset=new_offset;
303: }
304:
1.91 paf 305: override long tell() { return offset; }
1.79 paf 306:
1.1 paf 307: };
1.77 paf 308:
1.43 parser 309: #endif
1.1 paf 310:
1.72 paf 311: /// PNG file header
312: struct PNG_Header {
313: char dummy[12];
314: char signature[4]; //< must be "IHDR"
1.80 paf 315: uchar high_width[2]; //< image width high bytes [we ignore for now]
316: uchar width[2]; //< image width low bytes
317: uchar high_height[2]; //< image height high bytes [we ignore for now]
318: uchar height[4]; //< image height
1.72 paf 319: };
320:
1.21 paf 321: /// GIF file header
1.1 paf 322: struct GIF_Header {
1.72 paf 323: char signature[3]; // 'GIF'
1.1 paf 324: char version[3];
1.80 paf 325: uchar width[2];
326: uchar height[2];
1.1 paf 327: char dif;
328: char fonColor;
329: char nulls;
330: };
331:
1.31 parser 332: /// JPEG record head
333: struct JPG_Segment_head {
1.80 paf 334: uchar marker;
335: uchar code;
336: uchar length[2];
1.1 paf 337: };
1.21 paf 338: /// JPEG frame header
1.31 parser 339: struct JPG_Size_segment_body {
1.27 parser 340: char data; //< data precision of bits/sample
1.80 paf 341: uchar height[2]; //< image height
342: uchar width[2]; //< image width
1.27 parser 343: char numComponents; //< number of color components
1.1 paf 344: };
345:
1.80 paf 346: /// JPEG Exif TIFF Header
347: struct JPG_Exif_TIFF_header {
348: uchar byte_align_identifier[2];
349: char dummy[2]; // always 000A [or 0A00]
350: uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
351: };
352:
353: // JPEG Exif IFD start
1.91 paf 354: struct JPG_Exif_IFD_begin {
1.80 paf 355: uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
356: };
357:
358: // TTTT ffff NNNNNNNN DDDDDDDD
359: struct JPG_Exif_IFD_entry {
360: uchar tag[2]; // Tag number, this shows a kind of data
361: uchar format[2]; // data format
362: uchar components_count[4]; // number of components
363: uchar value_or_offset_to_it[4]; // data value or offset to data value
364: };
365:
366: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
367:
1.118 misha 368: #define JPG_IFD_TAG_EXIF_GPS_OFFSET 0x8825
369:
1.89 paf 370: #define JPEG_EXIF_DATE_CHARS 20
371:
1.162 moko 372:
373: #ifndef DOXYGEN
374: struct Measure_info {
375: ushort width;
376: ushort height;
377: Value** exif;
378: Value** xmp;
1.163 ! moko 379: Charset* xmp_charset;
1.162 moko 380: };
381: #endif
382:
1.1 paf 383: //
384:
1.80 paf 385: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
386: return (ushort)((b1<<8) + b0);
1.33 parser 387: }
388:
1.80 paf 389: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
390: return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
1.33 parser 391: }
392:
1.80 paf 393: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
394: return is_big?x_endian_to_ushort(b[1], b[0]):
395: x_endian_to_ushort(b[0], b[1]);
1.1 paf 396: }
397:
1.80 paf 398: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
399: return is_big?x_endian_to_uint(b[3], b[2], b[1], b[0]):
400: x_endian_to_uint(b[0], b[1], b[2], b[3]);
401: }
402:
1.152 moko 403: static void measure_gif(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
1.1 paf 404:
1.91 paf 405: const char* buf;
1.102 paf 406: const size_t head_size=sizeof(GIF_Header);
407: if(reader.read(buf, head_size)<head_size)
1.155 moko 408: throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - too small");
1.31 parser 409: GIF_Header *head=(GIF_Header *)buf;
1.1 paf 410:
1.72 paf 411: if(strncmp(head->signature, "GIF", 3)!=0)
1.155 moko 412: throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - wrong signature");
1.1 paf 413:
1.80 paf 414: width=endian_to_ushort(false, head->width);
415: height=endian_to_ushort(false, head->height);
416: }
417:
1.155 moko 418: static Value* parse_IFD_entry_formatted_one_value(bool is_big, ushort format, size_t component_size, const uchar *value) {
1.80 paf 419: switch(format) {
420: case 1: // unsigned byte
1.91 paf 421: return new VInt((uchar)value[0]);
1.80 paf 422: case 3: // unsigned short
1.91 paf 423: return new VInt(endian_to_ushort(is_big, value));
1.80 paf 424: case 4: // unsigned long
425: // 'double' because parser's Int is signed
1.91 paf 426: return new VDouble(endian_to_uint(is_big, value));
1.80 paf 427: case 5: // unsigned rational
428: {
429: uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
430: uint denominator=endian_to_uint(is_big, value);
431: if(!denominator)
432: return 0;
1.91 paf 433: return new VDouble(((double)numerator)/denominator);
1.80 paf 434: }
435: case 6: // signed byte
1.91 paf 436: return new VInt((signed char)value[0]);
1.80 paf 437: case 8: // signed short
1.91 paf 438: return new VInt((signed short)endian_to_ushort(is_big, value));
1.80 paf 439: case 9: // signed long
1.91 paf 440: return new VInt((signed int)endian_to_uint(is_big, value));
1.80 paf 441: case 10: // signed rational
442: {
443: signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
444: uint denominator=endian_to_uint(is_big, value);
445: if(!denominator)
446: return 0;
1.91 paf 447: return new VDouble(numerator/denominator);
1.80 paf 448: }
449: /*
450: case 11: // single float
1.120 misha 451: @todo
1.80 paf 452: case 12: // double float
1.120 misha 453: @todo
1.80 paf 454: */
455: };
456:
457: return 0;
458: }
459:
1.89 paf 460: // date.C
1.147 moko 461: tm cstr_to_time_t(char *, const char **);
1.89 paf 462:
1.155 moko 463: 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 464: if(format==2) { // ascii string, exception: the only type with varying size
1.91 paf 465: const char* cstr=(const char* )value;
466: size_t length=components_count;
1.89 paf 467: // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.155 moko 468: if(length==JPEG_EXIF_DATE_CHARS && isdigit((unsigned char)cstr[0]) && cstr[length-1]==0) {
469: char cstr_writable[JPEG_EXIF_DATE_CHARS];
1.89 paf 470: strcpy(cstr_writable, cstr);
471:
1.106 paf 472: try {
1.147 moko 473: tm tmIn=cstr_to_time_t(cstr_writable, 0);
1.146 moko 474: return new VDate(tmIn);
1.106 paf 475: }
476: catch(...) { /*ignore bad date times*/ }
1.89 paf 477: }
478:
1.123 misha 479: return new VString(*new String(cstr, String::L_TAINTED));
1.80 paf 480: }
481:
482: if(components_count==1)
1.91 paf 483: return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80 paf 484:
1.91 paf 485: VHash* result=new VHash;
486: HashStringValue& hash=result->hash();
1.80 paf 487: for(uint i=0; i<components_count; i++, value+=component_size) {
1.91 paf 488: hash.put(
1.93 paf 489: String::Body::Format(i),
1.91 paf 490: parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80 paf 491: }
492:
1.91 paf 493: return result;
1.80 paf 494: }
495:
1.155 moko 496: static Value* parse_IFD_entry_value(bool is_big, Measure_reader& reader, long tiff_base, JPG_Exif_IFD_entry& entry) {
1.80 paf 497: size_t format2component_size[]={
498: 0, // undefined
499: 1, // unsigned byte
500: 1, // ascii string
501: 2, // unsigned short
502: 4, // unsigned long
503: 8, // unsigned rational
504: 1, // signed byte
505: 0, // undefined
506: 2, // signed short
507: 4, // signed long
508: 8, // signed rational
509: /*
510: 4, // single float
511: 8, // double float
512: */
513: };
514:
515: ushort format=endian_to_ushort(is_big, entry.format);
516: if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
517: return 0; // format out of range, ignoring
518:
519: size_t component_size=format2component_size[format];
520: if(component_size==0)
521: return 0; // undefined format
522:
523: // You can get the total data byte length by multiplies
524: // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
525: uint components_count=endian_to_uint(is_big, entry.components_count);
1.121 misha 526: uint value_size=component_size*components_count;
1.80 paf 527: // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91 paf 528: Value* result;
1.80 paf 529:
530: if(value_size<=4)
1.155 moko 531: result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it);
1.80 paf 532: else {
533: long remembered=reader.tell();
534: {
535: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91 paf 536: const char* value;
1.156 moko 537: if(reader.read(value, value_size)<value_size)
1.80 paf 538: return 0;
1.155 moko 539: result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value);
1.80 paf 540: }
541: reader.seek(remembered, SEEK_SET);
542: }
543:
544: return result;
545: }
546:
1.155 moko 547: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, long tiff_base, bool gps=false);
1.91 paf 548:
1.155 moko 549: static void parse_IFD_entry(HashStringValue& hash, bool is_big, Measure_reader& reader, long tiff_base, JPG_Exif_IFD_entry& entry, bool gps=false) {
1.80 paf 550: ushort tag=endian_to_ushort(is_big, entry.tag);
1.118 misha 551:
552: if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
1.80 paf 553: long remembered=reader.tell();
554: {
555: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.118 misha 556: parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps);
1.80 paf 557: }
558: reader.seek(remembered, SEEK_SET);
559: return;
560: }
1.118 misha 561:
1.91 paf 562: if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.118 misha 563: if(const char* name=(gps)?exif_gps_tag_value2name.get(tag):exif_tag_value2name.get(tag))
1.148 moko 564: hash.put(name, value);
1.80 paf 565: else
1.93 paf 566: hash.put(String::Body::Format(tag), value);
1.80 paf 567: }
1.1 paf 568: }
569:
1.155 moko 570: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, long tiff_base, bool gps) {
1.91 paf 571: const char* buf;
572: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80 paf 573: return;
1.91 paf 574: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80 paf 575:
576: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
577: for(int i=0; i<directory_entry_count; i++) {
578: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
579: return;
580:
1.118 misha 581: parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf, gps);
1.80 paf 582: }
583: // then goes: LLLLLLLL Offset to next IFD [not going there]
584: }
585:
1.162 moko 586: static Value* parse_exif(Measure_reader& reader) {
1.91 paf 587: const char* buf;
1.80 paf 588:
589: uint tiff_base=reader.tell();
590: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
591: return 0;
592:
593: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
594: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
595:
596: uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
597: reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
598:
1.91 paf 599: VHash* vhash=new VHash;
1.80 paf 600:
601: // IFD
1.91 paf 602: parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80 paf 603:
1.91 paf 604: return vhash;
1.80 paf 605: }
606:
1.163 ! moko 607: static Value* parse_xmp(Measure_reader& reader, ushort xmp_length, Measure_info &info) {
1.162 moko 608: const char* buf;
609: if(reader.read(buf, xmp_length)<xmp_length)
610: return 0;
1.163 ! moko 611:
! 612: String::C xmp = Charset::transcode(String::C(pa_strdup(buf, xmp_length), xmp_length), *info.xmp_charset, pa_thread_request().charsets.source());
! 613: return new VString(*new String(xmp, String::L_TAINTED));
1.162 moko 614: }
615:
616: static void measure_jpeg(const String& origin_string, Measure_reader& reader, Measure_info &info) {
1.2 paf 617: // JFIF format markers
1.80 paf 618: const uchar MARKER=0xFF;
619: const uchar CODE_SIZE_A=0xC0;
620: const uchar CODE_SIZE_B=0xC1;
621: const uchar CODE_SIZE_C=0xC2;
622: const uchar CODE_SIZE_D=0xC3;
1.162 moko 623: const uchar CODE_APP1=0xE1;
1.2 paf 624:
1.91 paf 625: const char* buf;
1.18 paf 626: const size_t prefix_size=2;
1.31 parser 627: if(reader.read(buf, prefix_size)<prefix_size)
1.155 moko 628: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - too small");
1.80 paf 629: uchar *signature=(uchar *)buf;
1.1 paf 630:
1.31 parser 631: if(!(signature[0]==0xFF && signature[1]==0xD8))
1.155 moko 632: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - wrong signature");
1.31 parser 633:
634: while(true) {
1.80 paf 635: uint segment_base=reader.tell()+2/*marker,code*/;
1.31 parser 636: if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79 paf 637: break;
1.31 parser 638: JPG_Segment_head *head=(JPG_Segment_head *)buf;
639:
1.162 moko 640: // Verify that it's a valid segment.
1.31 parser 641: if(head->marker!=MARKER)
1.155 moko 642: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - marker not found");
1.79 paf 643:
1.162 moko 644: ushort segment_length=endian_to_ushort(true, head->length);
645:
1.79 paf 646: switch(head->code) {
1.162 moko 647: // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files
648: case CODE_APP1:
649: {
650: const size_t EXIF_SIG_LEN=6; // Exif\0\0
651: const size_t XMP_SIG_LEN=29; // http://ns.adobe.com/xap/1.0/\0
652:
653: if(segment_length<EXIF_SIG_LEN+2 || reader.read(buf, EXIF_SIG_LEN)<EXIF_SIG_LEN)
654: break;
655: if(memcmp(buf, "Exif\0\0", EXIF_SIG_LEN)==0){
656: if(info.exif && !*info.exif) // backward compatibility: using first segment
657: *info.exif=parse_exif(reader);
658: break;
659: }
660:
661: if(memcmp(buf, "http:/", EXIF_SIG_LEN))
662: break;
663: if(segment_length<XMP_SIG_LEN+2 || reader.read(buf, XMP_SIG_LEN-EXIF_SIG_LEN)<XMP_SIG_LEN-EXIF_SIG_LEN)
664: break;
665: if(memcmp(buf, "/ns.adobe.com/xap/1.0/\0", XMP_SIG_LEN-EXIF_SIG_LEN)==0){
666: if(info.xmp && !*info.xmp) // backward compatibility: using first segment
1.163 ! moko 667: *info.xmp=parse_xmp(reader, segment_length - XMP_SIG_LEN - 2 /* segment_length */, info);
1.162 moko 668: }
669:
670: }
1.1 paf 671: break;
1.31 parser 672:
1.79 paf 673: case CODE_SIZE_A:
674: case CODE_SIZE_B:
675: case CODE_SIZE_C:
676: case CODE_SIZE_D:
677: {
678: // Segments that contain size info
679: if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
1.155 moko 680: throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - can not fully read Size segment");
1.79 paf 681: JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
682:
1.162 moko 683: info.width=endian_to_ushort(true, body->width);
684: info.height=endian_to_ushort(true, body->height);
685: }
1.80 paf 686: return;
1.79 paf 687: };
688:
1.162 moko 689: reader.seek(segment_base + segment_length, SEEK_SET);
1.31 parser 690: }
691:
1.155 moko 692: throw Exception(IMAGE_FORMAT, &origin_string, "broken JPEG file - size frame not found");
1.1 paf 693: }
694:
1.152 moko 695: static void measure_png(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
1.72 paf 696:
1.91 paf 697: const char* buf;
1.102 paf 698: const size_t head_size=sizeof(PNG_Header);
699: if(reader.read(buf, head_size)<head_size)
1.152 moko 700: throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - too small");
1.72 paf 701: PNG_Header *head=(PNG_Header *)buf;
702:
703: if(strncmp(head->signature, "IHDR", 4)!=0)
1.152 moko 704: throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - wrong signature");
1.72 paf 705:
1.80 paf 706: width=endian_to_ushort(true, head->width);
707: height=endian_to_ushort(true, head->height);
1.72 paf 708: }
709:
1.1 paf 710: // measure center
711:
1.162 moko 712: static void measure(const String& file_name, Measure_reader& reader, Measure_info &info) {
1.128 misha 713: const char* file_name_cstr=file_name.taint_cstr(String::L_FILE_SPEC);
1.91 paf 714: if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1 paf 715: cext++;
716: if(strcasecmp(cext, "GIF")==0)
1.162 moko 717: measure_gif(file_name, reader, info.width, info.height);
1.1 paf 718: else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.162 moko 719: measure_jpeg(file_name, reader, info);
1.72 paf 720: else if(strcasecmp(cext, "PNG")==0)
1.162 moko 721: measure_png(file_name, reader, info.width, info.height);
1.1 paf 722: else
1.152 moko 723: throw Exception(IMAGE_FORMAT, &file_name, "unhandled image file name extension '%s'", cext);
1.1 paf 724: } else
1.152 moko 725: throw Exception(IMAGE_FORMAT, &file_name, "can not determine image type - no file name extension");
1.1 paf 726: }
727:
1.77 paf 728: // methods
1.1 paf 729:
1.152 moko 730: static void file_measure_action(struct stat& /*finfo*/, int f, const String& file_spec, void *context) {
731: Measure_file_reader reader(f, file_spec);
1.162 moko 732: measure(file_spec, reader, *static_cast<Measure_info *>(context));
1.1 paf 733: }
734:
1.91 paf 735: static void _measure(Request& r, MethodParams& params) {
736: Value& data=params.as_no_junction(0, "data must not be code");
1.1 paf 737:
1.91 paf 738: Value* exif=0;
1.162 moko 739: Value* xmp=0;
1.163 ! moko 740: Measure_info info={ 0, 0, 0, 0, &pa_UTF8_charset };
! 741:
! 742: if(params.count()>1)
! 743: if(HashStringValue* options=params.as_hash(1, "methods options")) {
! 744: int valid_options=0;
! 745: for(HashStringValue::Iterator i(*options); i; i.next() ){
! 746: String::Body key=i.key();
! 747: Value* value=i.value();
! 748: if(key == "exif") {
! 749: if(r.process(*value).as_bool())
! 750: info.exif=&exif;
! 751: valid_options++;
! 752: }
! 753: if(key == "xmp") {
! 754: if(r.process(*value).as_bool())
! 755: info.xmp=&xmp;
! 756: valid_options++;
! 757: }
! 758: if(key == "xmp-charset") {
! 759: info.xmp_charset=&pa_charsets.get(value->as_string());
! 760: valid_options++;
! 761: }
! 762: }
! 763: if(valid_options!=options->count())
! 764: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
! 765: }
1.162 moko 766:
1.91 paf 767: const String* file_name;
1.162 moko 768:
769: if(file_name=data.get_string()) {
1.152 moko 770: file_read_action_under_lock(r.absolute(*file_name), "measure", file_measure_action, &info);
1.1 paf 771: } else {
1.129 misha 772: VFile* vfile=data.as_vfile(String::L_AS_IS);
1.91 paf 773: file_name=&vfile->fields().get(name_name)->as_string();
1.162 moko 774: Measure_buf_reader reader(vfile->value_ptr(), vfile->value_size(), *file_name);
775: measure(*file_name, reader, info);
1.1 paf 776: }
777:
1.162 moko 778: GET_SELF(r, VImage).set(file_name, info.width, info.height, 0, exif, xmp);
1.1 paf 779: }
780:
1.161 moko 781: static void append_attrib_pair(String &tag, String::Body key, Value* value){
782: // skip border attribute with empty value
783: if(key=="border" && !value->is_defined())
1.4 paf 784: return;
785:
1.133 misha 786: // src="a.gif" width="123" ismap[=-1]
1.161 moko 787: tag << " " << key;
1.91 paf 788: if(value->is_string() || value->as_int()>=0)
1.161 moko 789: tag << "=\"" << value->as_string() << "\"";
1.3 paf 790: }
1.161 moko 791:
1.91 paf 792: static void _html(Request& r, MethodParams& params) {
793: String tag;
1.3 paf 794: tag << "<img";
1.4 paf 795:
1.91 paf 796: const HashStringValue& fields=GET_SELF(r, VImage).fields();
797: HashStringValue* attribs=0;
1.4 paf 798:
1.91 paf 799: if(params.count()) {
1.69 paf 800: // for backward compatibility: someday was ^html{}
1.154 moko 801: Value& vattribs=r.process(params[0]);
1.145 moko 802: if(!vattribs.is_string()) { // allow empty
1.100 paf 803: if((attribs=vattribs.get_hash())) {
1.161 moko 804: for(HashStringValue::Iterator i(*attribs); i; i.next() )
805: append_attrib_pair(tag, i.key(), i.value());
1.39 parser 806: } else
1.155 moko 807: throw Exception(PARSER_RUNTIME, 0, "attributes must be hash");
1.145 moko 808: }
1.39 parser 809: }
1.4 paf 810:
1.161 moko 811: for(HashStringValue::Iterator i(fields); i; i.next() ){
812: String::Body key=i.key();
813: // skip user-specified attributes
814: if(attribs && attribs->get(key))
815: continue;
816: // allow only html attributes (to exclude exif, line-*)
817: if(key=="src" || key=="width" || key=="height" || key=="border")
818: append_attrib_pair(tag, key, i.value());
1.91 paf 819: }
1.161 moko 820:
1.6 paf 821: tag << " />";
1.157 moko 822: r.write(tag);
1.3 paf 823: }
1.8 paf 824:
1.68 paf 825: /// @test wrap FILE to auto-object
1.155 moko 826: static gdImage* load(Request& r, const String& file_name){
1.128 misha 827: const char* file_name_cstr=r.absolute(file_name).taint_cstr(String::L_FILE_SPEC);
1.159 moko 828: if(FILE *f=pa_fopen(file_name_cstr, "rb")) {
1.91 paf 829: gdImage* image=new gdImage;
830: bool ok=image->CreateFromGif(f);
1.16 paf 831: fclose(f);
1.23 paf 832: if(!ok)
1.155 moko 833: throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");
1.91 paf 834: return image;
1.16 paf 835: } else {
1.155 moko 836: throw Exception("file.missing", 0, "can not open '%s'", file_name_cstr);
1.16 paf 837: }
838: }
839:
840:
1.91 paf 841: static void _load(Request& r, MethodParams& params) {
1.116 misha 842: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.6 paf 843:
1.91 paf 844: gdImage* image=load(r, file_name);
845: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 846: }
847:
1.91 paf 848: static void _create(Request& r, MethodParams& params) {
849: int width=params.as_int(0, "width must be int", r);
850: int height=params.as_int(1, "height must be int", r);
1.8 paf 851: int bgcolor_value=0xffFFff;
1.91 paf 852: if(params.count()>2)
853: bgcolor_value=params.as_int(2, "color must be int", r);
854: gdImage* image=new gdImage;
855: image->Create(width, height);
856: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
857: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 858: }
859:
1.91 paf 860: static void _gif(Request& r, MethodParams& params) {
1.108 paf 861: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 862:
1.138 misha 863: const String *file_name=params.count()>0?¶ms.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10 paf 864:
1.108 paf 865: gdBuf buf=image.Gif();
1.6 paf 866:
1.91 paf 867: VFile& vfile=*new VFile;
1.137 misha 868:
1.142 moko 869: vfile.set_binary(false/*not tainted*/, (const char *)buf.ptr, buf.size, file_name, new VString(*new String("image/gif")));
1.137 misha 870:
1.157 moko 871: r.write(vfile);
1.6 paf 872: }
873:
1.91 paf 874: static void _line(Request& r, MethodParams& params) {
1.108 paf 875: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 876:
1.108 paf 877: image.Line(
1.91 paf 878: params.as_int(0, "x0 must be int", r),
879: params.as_int(1, "y0 must be int", r),
880: params.as_int(2, "x1 must be int", r),
881: params.as_int(3, "y1 must be int", r),
1.108 paf 882: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 883: }
884:
1.91 paf 885: static void _fill(Request& r, MethodParams& params) {
1.108 paf 886: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 887:
1.108 paf 888: image.Fill(
1.91 paf 889: params.as_int(0, "x must be int", r),
890: params.as_int(1, "y must be int", r),
1.108 paf 891: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 892: }
893:
1.91 paf 894: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 895: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 896:
1.108 paf 897: image.Rectangle(
1.91 paf 898: params.as_int(0, "x0 must be int", r),
899: params.as_int(1, "y0 must be int", r),
900: params.as_int(2, "x1 must be int", r),
901: params.as_int(3, "y1 must be int", r),
1.108 paf 902: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 903: }
904:
1.91 paf 905: static void _bar(Request& r, MethodParams& params) {
1.108 paf 906: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 907:
1.108 paf 908: image.FilledRectangle(
1.91 paf 909: params.as_int(0, "x0 must be int", r),
910: params.as_int(1, "y0 must be int", r),
911: params.as_int(2, "x1 must be int", r),
912: params.as_int(3, "y1 must be int", r),
1.108 paf 913: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 914: }
915:
1.44 parser 916: #ifndef DOXYGEN
1.155 moko 917: static void add_point(Table::element_type row, gdImage::Point **p) {
1.110 paf 918: if(row->count()!=2)
1.155 moko 919: throw Exception(0, 0, "coordinates table must contain two columns: x and y values");
1.91 paf 920: (**p).x=row->get(0)->as_int();
921: (**p).y=row->get(1)->as_int();
1.44 parser 922: (*p)++;
923: }
924: #endif
1.135 misha 925: #ifndef DOXYGEN
1.155 moko 926: static void add_point(int x, int y, gdImage::Point **p) {
1.135 misha 927: (**p).x=x;
928: (**p).y=y;
929: (*p)++;
930: }
931: #endif
1.91 paf 932: static void _replace(Request& r, MethodParams& params) {
1.135 misha 933: int src_color=params.as_int(0, "src color must be int", r);
934: int dest_color=params.as_int(1, "dest color must be int", r);
935:
1.108 paf 936: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 937:
1.135 misha 938: gdImage::Point* all_p=0;
939: size_t count=0;
940: if(params.count() == 3){
1.140 misha 941: Table* table=params.as_table(2, "coordinates");
1.135 misha 942: count=table->count();
943: all_p=new(PointerFreeGC) gdImage::Point[count];
944: gdImage::Point* add_p=all_p;
945: table->for_each(add_point, &add_p);
946: } else {
947: int max_x=image.SX()-1;
948: int max_y=image.SY()-1;
949: if(max_x > 0 && max_y > 0){
950: count=4;
951: all_p=new(PointerFreeGC) gdImage::Point[count];
952: gdImage::Point* add_p=all_p;
953: add_point(0, 0, &add_p);
954: add_point(max_x, 0, &add_p);
955: add_point(max_x, max_y, &add_p);
956: add_point(0, max_y, &add_p);
957: }
958: }
1.13 paf 959:
1.135 misha 960: if(count)
961: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 962: }
963:
1.91 paf 964: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 965: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 966:
1.140 misha 967: Table* table=params.as_table(1, "coordinates");
1.44 parser 968:
1.92 paf 969: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 970: gdImage::Point *add_p=all_p;
971: table->for_each(add_point, &add_p);
1.108 paf 972: image.Polygon(all_p, table->count(),
973: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 974: false/*not closed*/);
975: }
976:
1.91 paf 977: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 978: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 979:
1.140 misha 980: Table* table=(Table*)params.as_table(1, "coordinates");
1.44 parser 981:
1.92 paf 982: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 983: gdImage::Point *add_p=all_p;
984: table->for_each(add_point, &add_p);
1.108 paf 985: image.Polygon(all_p, table->count(),
986: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 987: }
988:
1.91 paf 989: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 990: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 991:
1.140 misha 992: Table* table=(Table*)params.as_table(1, "coordinates");
1.13 paf 993:
1.92 paf 994: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 995: gdImage::Point *add_p=all_p;
996: table->for_each(add_point, &add_p);
1.108 paf 997: image.FilledPolygon(all_p, table->count(),
998: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 999: }
1000:
1.16 paf 1001: // font
1002:
1.85 paf 1003: #define Y(y)(y+index*height)
1.21 paf 1004:
1.91 paf 1005: // Font class
1006:
1.155 moko 1007: Font::Font(Charset& asource_charset, const String& aalphabet, gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.158 moko 1008: letterspacing(aletterspacing),
1.132 misha 1009: height(aheight),
1010: monospace(amonospace),
1011: spacebarspace(aspacebarspace),
1.100 paf 1012: ifont(aifont),
1.158 moko 1013: alphabet(aalphabet),
1014: fsource_charset(asource_charset){
1.132 misha 1015:
1016: if(fsource_charset.isUTF8()){
1017: size_t index=0;
1018: for(UTF8_string_iterator i(alphabet); i.has_next(); )
1019: fletter2index.put_dont_replace(i.next(), index++);
1020: }
1.91 paf 1021: }
1022:
1023: /* ******************************** char ********************************** */
1024:
1025: size_t Font::index_of(char ch) {
1026: if(ch==' ') return STRING_NOT_FOUND;
1027: return alphabet.pos(ch);
1028: }
1029:
1.132 misha 1030: size_t Font::index_of(XMLCh ch) {
1031: if(ch==' ') return STRING_NOT_FOUND;
1032: return fletter2index.get(ch);
1033: }
1034:
1.91 paf 1035: int Font::index_width(size_t index) {
1036: if(index==STRING_NOT_FOUND)
1037: return spacebarspace;
1038: int tr=ifont->GetTransparent();
1039: for(int x=ifont->SX()-1; x>=0; x--) {
1040: for(int y=0; y<height; y++)
1041: if(ifont->GetPixel(x, Y(y))!=tr)
1042: return x+1;
1.16 paf 1043: }
1.91 paf 1044: return 0;
1045: }
1046:
1.108 paf 1047: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1048: if(index!=STRING_NOT_FOUND)
1.108 paf 1049: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1050: }
1051:
1052: /* ******************************** string ********************************** */
1053:
1054: int Font::step_width(int index) {
1.125 misha 1055: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1056: }
1057:
1058: // counts trailing letter_spacing, consider this OK. useful for contiuations
1059: int Font::string_width(const String& s){
1060: const char* cstr=s.cstr();
1061: int result=0;
1.132 misha 1062:
1063: if(fsource_charset.isUTF8()){
1064: for(UTF8_string_iterator i(s); i.has_next(); )
1065: result+=step_width(index_of(i.next()));
1066: } else {
1067: for(const char* current=cstr; *current; current++)
1068: result+=step_width(index_of(*current));
1069: }
1070:
1.91 paf 1071: return result;
1072: }
1073:
1.108 paf 1074: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1075: const char* cstr=s.cstr();
1.132 misha 1076:
1077: if(fsource_charset.isUTF8()){
1078: for(UTF8_string_iterator i(s); i.has_next(); ){
1079: size_t index=index_of(i.next());
1080: index_display(image, x, y, index);
1081: x+=step_width(index);
1082: }
1083: } else {
1084: for(const char* current=cstr; *current; current++) {
1085: size_t index=index_of(*current);
1086: index_display(image, x, y, index);
1087: x+=step_width(index);
1088: }
1.87 paf 1089: }
1.91 paf 1090: }
1.87 paf 1091:
1.91 paf 1092: //
1.35 parser 1093:
1.16 paf 1094:
1.91 paf 1095: static void _font(Request& r, MethodParams& params) {
1096: const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131 misha 1097: size_t alphabet_length=alphabet.length(r.charsets.source());
1098: if(!alphabet_length)
1.155 moko 1099: throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");
1.84 paf 1100:
1.125 misha 1101: gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1102:
1.126 misha 1103: int spacebar_width=image->SX();
1.125 misha 1104: int monospace_width=0; // proportional
1105: int letter_spacing=1;
1106: if(params.count()>2){
1.144 moko 1107: if(HashStringValue* options=params[2].get_hash()){
1.125 misha 1108: // third option is hash
1109: if(params.count()>3)
1.144 moko 1110: throw Exception(PARSER_RUNTIME, 0, "too many params were specified");
1.125 misha 1111: int valid_options=0;
1112: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1113: valid_options++;
1.153 moko 1114: spacebar_width=r.process(*vspacebar_width).as_int();
1.125 misha 1115: }
1116: if(Value* vmonospace_width=options->get(monospace_width_name)){
1117: valid_options++;
1.153 moko 1118: monospace_width=r.process(*vmonospace_width).as_int();
1.125 misha 1119: if(!monospace_width)
1120: monospace_width=image->SX();
1121: }
1122: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1123: valid_options++;
1.153 moko 1124: letter_spacing=r.process(*vletter_spacing).as_int();
1.125 misha 1125: }
1126: if(valid_options!=options->count())
1.134 misha 1127: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1128: } else {
1129: // backward
1.144 moko 1130: spacebar_width=params.as_int(2, "param must be int or hash", r);
1.125 misha 1131: if(params.count()>3) {
1132: monospace_width=params.as_int(3, "monospace_width must be int", r);
1133: if(!monospace_width)
1134: monospace_width=image->SX();
1135: }
1136: }
1137: }
1138:
1.131 misha 1139: if(int remainder=image->SY() % alphabet_length)
1.155 moko 1140: 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 1141:
1.155 moko 1142: 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 1143: }
1144:
1.91 paf 1145: static void _text(Request& r, MethodParams& params) {
1146: int x=params.as_int(0, "x must be int", r);
1147: int y=params.as_int(1, "y must be int", r);
1148: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1149:
1.91 paf 1150: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1151: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1152: }
1153:
1.91 paf 1154: static void _length(Request& r, MethodParams& params) {
1155: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1156:
1.91 paf 1157: VImage& vimage=GET_SELF(r, VImage);
1.157 moko 1158: r.write(*new VInt(vimage.font().string_width(s)));
1.47 parser 1159: }
1160:
1.91 paf 1161: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1162: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1163:
1.108 paf 1164: image.Arc(
1.91 paf 1165: params.as_int(0, "center_x must be int", r),
1166: params.as_int(1, "center_y must be int", r),
1167: params.as_int(2, "width must be int", r),
1168: params.as_int(3, "height must be int", r),
1169: params.as_int(4, "start degrees must be int", r),
1170: params.as_int(5, "end degrees must be int", r),
1.108 paf 1171: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1172: }
1173:
1.91 paf 1174: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1175: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1176:
1.108 paf 1177: image.Sector(
1.91 paf 1178: params.as_int(0, "center_x must be int", r),
1179: params.as_int(1, "center_y must be int", r),
1180: params.as_int(2, "width must be int", r),
1181: params.as_int(3, "height must be int", r),
1182: params.as_int(4, "start degrees must be int", r),
1183: params.as_int(5, "end degrees must be int", r),
1.108 paf 1184: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1185: }
1186:
1.91 paf 1187: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1188: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1189:
1.91 paf 1190: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1191: image.Arc(
1.91 paf 1192: params.as_int(0, "center_x must be int", r),
1193: params.as_int(1, "center_y must be int", r),
1.50 parser 1194: size, //w
1195: size, //h
1.48 parser 1196: 0, //s
1197: 360, //e
1.108 paf 1198: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1199: }
1200:
1.108 paf 1201: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1202: Value& value=params.as_no_junction(index, msg);
1.53 parser 1203:
1.130 misha 1204: if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108 paf 1205: return static_cast<VImage *>(vimage)->image();
1.75 paf 1206: } else
1.155 moko 1207: throw Exception(PARSER_RUNTIME, 0, msg);
1.53 parser 1208: }
1209:
1.91 paf 1210: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1211: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1212:
1.108 paf 1213: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1214:
1.91 paf 1215: int sx=params.as_int(1, "src_x must be int", r);
1216: int sy=params.as_int(2, "src_y must be int", r);
1217: int sw=params.as_int(3, "src_w must be int", r);
1218: int sh=params.as_int(4, "src_h must be int", r);
1219: int dx=params.as_int(5, "dest_x must be int", r);
1220: int dy=params.as_int(6, "dest_y must be int", r);
1221: if(params.count()>1+2+2+2) {
1222: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1223: int dh=(int)(params.count()>1+2+2+2+1?
1224: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1225: int tolerance=params.count()>1+2+2+2+2?
1226: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1227:
1.108 paf 1228: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1229: } else
1.108 paf 1230: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1231: }
1232:
1233: static void _pixel(Request& r, MethodParams& params) {
1234: gdImage& image=GET_SELF(r, VImage).image();
1235:
1.109 paf 1236: int x=params.as_int(0, "x must be int", r);
1237: int y=params.as_int(1, "y must be int", r);
1238:
1239: if(params.count()>2) {
1240: image.SetPixel(x, y,
1241: image.Color(params.as_int(2, "color must be int", r)));
1242: } else
1.157 moko 1243: r.write(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1244: }
1245:
1246:
1.22 paf 1247: // constructor
1248:
1.91 paf 1249: MImage::MImage(): Methoded("image") {
1.1 paf 1250: // ^image:measure[DATA]
1.163 ! moko 1251: // ^image:measure[DATA; $.exif(false) $.xmp(false) $.xmp-charset[UTF-8] ]
! 1252: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 2);
1.3 paf 1253:
1.25 paf 1254: // ^image.html[]
1255: // ^image.html[hash]
1.22 paf 1256: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1257:
1.25 paf 1258: // ^image.load[background.gif]
1.22 paf 1259: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1260:
1.25 paf 1261: // ^image.create[width;height] bgcolor=white
1262: // ^image.create[width;height;bgcolor]
1.22 paf 1263: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1264:
1.25 paf 1265: // ^image.gif[]
1.95 paf 1266: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1267:
1.25 paf 1268: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1269: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1270:
1.25 paf 1271: // ^image.fill(x;y;color)
1.22 paf 1272: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1273:
1.25 paf 1274: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1275: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1276:
1.25 paf 1277: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1278: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1279:
1.44 parser 1280: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1281: // ^image.replace(color-source;color-dest)
1282: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1283:
1284: // ^image.polyline(color)[table x:y]
1285: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1286:
1.44 parser 1287: // ^image.polygon(color)[table x:y]
1288: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1289:
1.44 parser 1290: // ^image.polybar(color)[table x:y]
1291: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1292:
1.125 misha 1293: // ^image.font[alPHAbet;font-file-name.gif]
1294: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1295: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1296: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1297: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1298:
1.125 misha 1299: // ^image.text(x;y)[text]
1.22 paf 1300: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1301:
1.125 misha 1302: // ^image.length[text]
1.47 parser 1303: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1304:
1.48 parser 1305: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1306: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1307:
1308: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1309: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1310:
1311: // ^image.circle(center x;center y;r;color)
1312: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1313:
1.56 parser 1314: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1315: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1316:
1317: // ^image.pixel(x;y)[(color)]
1.109 paf 1318: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1319: }
E-mail: