Annotation of parser3/src/classes/image.C, revision 1.139
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.139 ! moko 4: Copyright (c) 2001-2012 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.1 paf 27:
1.139 ! moko 28: volatile const char * IDENT_IMAGE_C="$Id: 2011-11-23 12:17:22 $";
! 29:
1.125 misha 30: // defines
31:
1.126 misha 32: static const String spacebar_width_name("space");
33: static const String monospace_width_name("width");
34: static const String letter_spacing_name("spacing");
1.125 misha 35:
1.22 paf 36: // class
37:
1.91 paf 38: class MImage: public Methoded {
1.22 paf 39: public: // VStateless_class
1.130 misha 40: Value* create_new_value(Pool&) { return new VImage(); }
1.22 paf 41:
42: public:
1.91 paf 43: MImage();
1.22 paf 44: };
1.1 paf 45:
1.91 paf 46: // globals
47:
48: DECLARE_CLASS_VAR(image, new MImage, 0);
49:
1.1 paf 50: // helpers
51:
1.118 misha 52: #define EXIF_TAG(tag, name) put(tag, #name);
53:
1.91 paf 54: /// value of exif tag -> it's value
55: class EXIF_tag_value2name: public Hash<int, const char*> {
56: public:
57: EXIF_tag_value2name() {
58: // image JPEG Exif
59: // Tags used by IFD0 (main image)
60: EXIF_TAG(0x010e, ImageDescription);
61: EXIF_TAG(0x010f, Make);
62: EXIF_TAG(0x0110, Model);
63: EXIF_TAG(0x0112, Orientation);
64: EXIF_TAG(0x011a, XResolution);
65: EXIF_TAG(0x011b, YResolution);
66: EXIF_TAG(0x0128, ResolutionUnit);
67: EXIF_TAG(0x0131, Software);
68: EXIF_TAG(0x0132, DateTime);
69: EXIF_TAG(0x013e, WhitePoint);
70: EXIF_TAG(0x013f, PrimaryChromaticities);
71: EXIF_TAG(0x0211, YCbCrCoefficients);
72: EXIF_TAG(0x0213, YCbCrPositioning);
73: EXIF_TAG(0x0214, ReferenceBlackWhite);
74: EXIF_TAG(0x8298, Copyright);
75: EXIF_TAG(0x8769, ExifOffset);
76: // Tags used by Exif SubIFD
77: EXIF_TAG(0x829a, ExposureTime);
78: EXIF_TAG(0x829d, FNumber);
79: EXIF_TAG(0x8822, ExposureProgram);
80: EXIF_TAG(0x8827, ISOSpeedRatings);
81: EXIF_TAG(0x9000, ExifVersion);
82: EXIF_TAG(0x9003, DateTimeOriginal);
83: EXIF_TAG(0x9004, DateTimeDigitized);
84: EXIF_TAG(0x9101, ComponentsConfiguration);
85: EXIF_TAG(0x9102, CompressedBitsPerPixel);
86: EXIF_TAG(0x9201, ShutterSpeedValue);
87: EXIF_TAG(0x9202, ApertureValue);
88: EXIF_TAG(0x9203, BrightnessValue);
89: EXIF_TAG(0x9204, ExposureBiasValue);
90: EXIF_TAG(0x9205, MaxApertureValue);
91: EXIF_TAG(0x9206, SubjectDistance);
92: EXIF_TAG(0x9207, MeteringMode);
93: EXIF_TAG(0x9208, LightSource);
94: EXIF_TAG(0x9209, Flash);
95: EXIF_TAG(0x920a, FocalLength);
96: EXIF_TAG(0x927c, MakerNote);
97: EXIF_TAG(0x9286, UserComment);
98: EXIF_TAG(0x9290, SubsecTime);
99: EXIF_TAG(0x9291, SubsecTimeOriginal);
100: EXIF_TAG(0x9292, SubsecTimeDigitized);
101: EXIF_TAG(0xa000, FlashPixVersion);
102: EXIF_TAG(0xa001, ColorSpace);
103: EXIF_TAG(0xa002, ExifImageWidth);
104: EXIF_TAG(0xa003, ExifImageHeight);
105: EXIF_TAG(0xa004, RelatedSoundFile);
106: EXIF_TAG(0xa005, ExifInteroperabilityOffset);
107: EXIF_TAG(0xa20e, FocalPlaneXResolution);
108: EXIF_TAG(0xa20f, FocalPlaneYResolution);
109: EXIF_TAG(0xa210, FocalPlaneResolutionUnit);
110: EXIF_TAG(0xa215, ExposureIndex);
111: EXIF_TAG(0xa217, SensingMethod);
112: EXIF_TAG(0xa300, FileSource);
113: EXIF_TAG(0xa301, SceneType);
114: EXIF_TAG(0xa302, CFAPattern);
115: // Misc Tags
116: EXIF_TAG(0x00fe, NewSubfileType);
117: EXIF_TAG(0x00ff, SubfileType);
118: EXIF_TAG(0x012d, TransferFunction);
119: EXIF_TAG(0x013b, Artist);
120: EXIF_TAG(0x013d, Predictor);
121: EXIF_TAG(0x0142, TileWidth);
122: EXIF_TAG(0x0143, TileLength);
123: EXIF_TAG(0x0144, TileOffsets);
124: EXIF_TAG(0x0145, TileByteCounts);
125: EXIF_TAG(0x014a, SubIFDs);
126: EXIF_TAG(0x015b, JPEGTables);
127: EXIF_TAG(0x828d, CFARepeatPatternDim);
128: EXIF_TAG(0x828e, CFAPattern);
129: EXIF_TAG(0x828f, BatteryLevel);
130: EXIF_TAG(0x83bb, IPTC/NAA);
131: EXIF_TAG(0x8773, InterColorProfile);
132: EXIF_TAG(0x8824, SpectralSensitivity);
1.118 misha 133: //EXIF_TAG(0x8825, GPSInfo);
1.91 paf 134: EXIF_TAG(0x8828, OECF);
135: EXIF_TAG(0x8829, Interlace);
136: EXIF_TAG(0x882a, TimeZoneOffset);
137: EXIF_TAG(0x882b, SelfTimerMode);
138: EXIF_TAG(0x920b, FlashEnergy);
139: EXIF_TAG(0x920c, SpatialFrequencyResponse);
140: EXIF_TAG(0x920d, Noise);
141: EXIF_TAG(0x9211, ImageNumber);
142: EXIF_TAG(0x9212, SecurityClassification);
143: EXIF_TAG(0x9213, ImageHistory);
144: EXIF_TAG(0x9214, SubjectLocation);
145: EXIF_TAG(0x9215, ExposureIndex);
146: EXIF_TAG(0x9216, TIFF/EPStandardID);
147: EXIF_TAG(0xa20b, FlashEnergy);
148: EXIF_TAG(0xa20c, SpatialFrequencyResponse);
149: EXIF_TAG(0xa214, SubjectLocation);
1.118 misha 150:
151: // additional things added by misha@
152: EXIF_TAG(0x0100, ImageWidth);
153: EXIF_TAG(0x0101, ImageLength);
154: EXIF_TAG(0x0102, BitsPerSample);
155: EXIF_TAG(0x0103, Compression);
156: EXIF_TAG(0x0106, PhotometricInterpretation);
157: EXIF_TAG(0x010a, FillOrder);
158: EXIF_TAG(0x010d, DocumentName);
159: EXIF_TAG(0x0111, StripOffsets);
160: EXIF_TAG(0x0115, SamplesPerPixel);
161: EXIF_TAG(0x0116, RowsPerStrip);
162: EXIF_TAG(0x0117, StripByteCounts);
163: EXIF_TAG(0x011c, PlanarConfiguration);
164: EXIF_TAG(0x0156, TransferRange);
165: EXIF_TAG(0x0200, JPEGProc);
166: EXIF_TAG(0x0201, JPEGInterchangeFormat);
167: EXIF_TAG(0x0202, JPEGInterchangeFormatLength);
168: EXIF_TAG(0x0212, YCbCrSubSampling);
169: EXIF_TAG(0xa401, CustomRendered);
170: EXIF_TAG(0xa402, ExposureMode);
171: EXIF_TAG(0xa403, WhiteBalance);
172: EXIF_TAG(0xa404, DigitalZoomRatio);
173: EXIF_TAG(0xa405, FocalLengthIn35mmFilm);
174: EXIF_TAG(0xa406, SceneCaptureType);
175: EXIF_TAG(0xa407, GainControl);
176: EXIF_TAG(0xa408, Contrast);
177: EXIF_TAG(0xa409, Saturation);
178: EXIF_TAG(0xa40a, Sharpness);
179: EXIF_TAG(0xa40b, DeviceSettingDescription);
180: EXIF_TAG(0xa40c, SubjectDistanceRange);
181: EXIF_TAG(0xa420, ImageUniqueID);
1.91 paf 182: }
183: } exif_tag_value2name;
184:
1.118 misha 185: class EXIF_gps_tag_value2name: public Hash<int, const char*> {
186: public:
187: EXIF_gps_tag_value2name() {
188: EXIF_TAG(0x0, GPSVersionID);
189: EXIF_TAG(0x1, GPSLatitudeRef);
190: EXIF_TAG(0x2, GPSLatitude);
191: EXIF_TAG(0x3, GPSLongitudeRef);
192: EXIF_TAG(0x4, GPSLongitude);
193: EXIF_TAG(0x5, GPSAltitudeRef);
194: EXIF_TAG(0x6, GPSAltitude);
195: EXIF_TAG(0x7, GPSTimeStamp);
196: EXIF_TAG(0x8, GPSSatellites);
197: EXIF_TAG(0x9, GPSStatus);
198: EXIF_TAG(0xA, GPSMeasureMode);
199: EXIF_TAG(0xB, GPSDOP);
200: EXIF_TAG(0xC, GPSSpeedRef);
201: EXIF_TAG(0xD, GPSSpeed);
202: EXIF_TAG(0xE, GPSTrackRef);
203: EXIF_TAG(0xF, GPSTrack);
204: EXIF_TAG(0x10, GPSImgDirectionRef);
205: EXIF_TAG(0x11, GPSImgDirection);
206: EXIF_TAG(0x12, GPSMapDatum);
207: EXIF_TAG(0x13, GPSDestLatitudeRef);
208: EXIF_TAG(0x14, GPSDestLatitude);
209: EXIF_TAG(0x15, GPSDestLongitudeRef);
210: EXIF_TAG(0x16, GPSDestLongitude);
211: EXIF_TAG(0x17, GPSDestBearingRef);
212: EXIF_TAG(0x18, GPSDestBearing);
213: EXIF_TAG(0x19, GPSDestDistanceRef);
214: EXIF_TAG(0x1A, GPSDestDistance);
215: EXIF_TAG(0x1B, GPSProcessingMethod);
216: EXIF_TAG(0x1C, GPSAreaInformation);
217: EXIF_TAG(0x1D, GPSDateStamp);
218: EXIF_TAG(0x1E, GPSDifferential);
219: }
220: } exif_gps_tag_value2name;
221:
222: #undef EXIF_TAG
1.91 paf 223:
1.43 parser 224: #ifndef DOXYGEN
1.1 paf 225: class Measure_reader {
226: public:
1.91 paf 227: virtual size_t read(const char* &buf, size_t limit)=0;
1.79 paf 228: virtual void seek(long value, int whence)=0;
229: virtual long tell()=0;
1.77 paf 230: };
1.1 paf 231:
1.77 paf 232: class Measure_file_reader: public Measure_reader {
1.91 paf 233: const String& file_name; const char* fname;
234: int f;
235:
1.77 paf 236: public:
1.91 paf 237: Measure_file_reader(int af, const String& afile_name, const char* afname):
238: file_name(afile_name), fname(afname), f(af) {
1.1 paf 239: }
240:
1.91 paf 241: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 242: if(limit==0)
1.1 paf 243: return 0;
1.77 paf 244:
1.91 paf 245: char* lbuf=new(PointerFreeGC) char[limit];
1.77 paf 246: size_t read_size=(size_t)::read(f, lbuf, limit); abuf=lbuf;
247: if(ssize_t(read_size)<0 || read_size>limit)
248: throw Exception(0,
249: &file_name,
1.104 paf 250: "measure failed: actually read %u bytes count not in [0..%u] valid range",
1.77 paf 251: read_size, limit);
252:
1.1 paf 253: return read_size;
254: }
255:
1.91 paf 256: override void seek(long value, int whence) {
1.79 paf 257: if(lseek(f, value, whence)<0)
1.118 misha 258: throw Exception(IMAGE_FORMAT,
1.77 paf 259: &file_name,
1.79 paf 260: "seek(value=%ld, whence=%d) failed: %s (%d), actual filename '%s'",
261: value, whence, strerror(errno), errno, fname);
1.77 paf 262: }
263:
1.91 paf 264: override long tell() { return lseek(f, 0, SEEK_CUR); }
1.79 paf 265:
1.77 paf 266: };
267:
268: class Measure_buf_reader: public Measure_reader {
1.91 paf 269:
270: const char* buf; size_t size;
271: const String& file_name;
272:
273: size_t offset;
274:
1.77 paf 275: public:
1.91 paf 276: Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):
1.77 paf 277: buf(abuf), size(asize), file_name(afile_name), offset(0) {
278: }
279:
1.91 paf 280: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 281: size_t to_read=min(limit, size-offset);
1.91 paf 282: abuf=buf+offset;
1.77 paf 283: offset+=to_read;
284: return to_read;
285: }
286:
1.91 paf 287: override void seek(long value, int whence) {
1.79 paf 288: size_t new_offset;
289: switch(whence) {
290: case SEEK_CUR: new_offset=offset+value; break;
291: case SEEK_SET: new_offset=(size_t)value; break;
1.91 paf 292: default:
293: throw Exception(0,
294: 0,
295: "whence #%d not supported", 0, whence);
296: break; // never
1.79 paf 297: }
298:
1.77 paf 299: if((ssize_t)new_offset<0 || new_offset>size)
1.118 misha 300: throw Exception(IMAGE_FORMAT,
1.77 paf 301: &file_name,
1.79 paf 302: "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0",
303: value, whence, new_offset, size);
1.77 paf 304: offset=new_offset;
305: }
306:
1.91 paf 307: override long tell() { return offset; }
1.79 paf 308:
1.1 paf 309: };
1.77 paf 310:
1.43 parser 311: #endif
1.1 paf 312:
1.72 paf 313: /// PNG file header
314: struct PNG_Header {
315: char dummy[12];
316: char signature[4]; //< must be "IHDR"
1.80 paf 317: uchar high_width[2]; //< image width high bytes [we ignore for now]
318: uchar width[2]; //< image width low bytes
319: uchar high_height[2]; //< image height high bytes [we ignore for now]
320: uchar height[4]; //< image height
1.72 paf 321: };
322:
1.21 paf 323: /// GIF file header
1.1 paf 324: struct GIF_Header {
1.72 paf 325: char signature[3]; // 'GIF'
1.1 paf 326: char version[3];
1.80 paf 327: uchar width[2];
328: uchar height[2];
1.1 paf 329: char dif;
330: char fonColor;
331: char nulls;
332: };
333:
1.31 parser 334: /// JPEG record head
335: struct JPG_Segment_head {
1.80 paf 336: uchar marker;
337: uchar code;
338: uchar length[2];
1.1 paf 339: };
1.21 paf 340: /// JPEG frame header
1.31 parser 341: struct JPG_Size_segment_body {
1.27 parser 342: char data; //< data precision of bits/sample
1.80 paf 343: uchar height[2]; //< image height
344: uchar width[2]; //< image width
1.27 parser 345: char numComponents; //< number of color components
1.1 paf 346: };
347:
1.79 paf 348: /// JPEG frame header
1.91 paf 349: struct JPG_Exif_segment_begin {
1.79 paf 350: char signature[6]; // Exif\0\0
351: };
352:
1.80 paf 353: /// JPEG Exif TIFF Header
354: struct JPG_Exif_TIFF_header {
355: uchar byte_align_identifier[2];
356: char dummy[2]; // always 000A [or 0A00]
357: uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
358: };
359:
360: // JPEG Exif IFD start
1.91 paf 361: struct JPG_Exif_IFD_begin {
1.80 paf 362: uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
363: };
364:
365: // TTTT ffff NNNNNNNN DDDDDDDD
366: struct JPG_Exif_IFD_entry {
367: uchar tag[2]; // Tag number, this shows a kind of data
368: uchar format[2]; // data format
369: uchar components_count[4]; // number of components
370: uchar value_or_offset_to_it[4]; // data value or offset to data value
371: };
372:
373: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
374:
1.118 misha 375: #define JPG_IFD_TAG_EXIF_GPS_OFFSET 0x8825
376:
1.89 paf 377: #define JPEG_EXIF_DATE_CHARS 20
378:
1.1 paf 379: //
380:
1.80 paf 381: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
382: return (ushort)((b1<<8) + b0);
1.33 parser 383: }
384:
1.80 paf 385: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
386: return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
1.33 parser 387: }
388:
1.80 paf 389: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
390: return is_big?x_endian_to_ushort(b[1], b[0]):
391: x_endian_to_ushort(b[0], b[1]);
1.1 paf 392: }
393:
1.80 paf 394: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
395: return is_big?x_endian_to_uint(b[3], b[2], b[1], b[0]):
396: x_endian_to_uint(b[0], b[1], b[2], b[3]);
397: }
398:
1.91 paf 399: static void measure_gif(const String& origin_string,
1.78 paf 400: Measure_reader& reader, ushort& width, ushort& height) {
1.1 paf 401:
1.91 paf 402: const char* buf;
1.102 paf 403: const size_t head_size=sizeof(GIF_Header);
404: if(reader.read(buf, head_size)<head_size)
1.118 misha 405: throw Exception(IMAGE_FORMAT,
1.91 paf 406: &origin_string,
1.34 parser 407: "not GIF file - too small");
1.31 parser 408: GIF_Header *head=(GIF_Header *)buf;
1.1 paf 409:
1.72 paf 410: if(strncmp(head->signature, "GIF", 3)!=0)
1.118 misha 411: throw Exception(IMAGE_FORMAT,
1.91 paf 412: &origin_string,
1.44 parser 413: "not GIF file - wrong signature");
1.1 paf 414:
1.80 paf 415: width=endian_to_ushort(false, head->width);
416: height=endian_to_ushort(false, head->height);
417: }
418:
1.91 paf 419: static Value* parse_IFD_entry_formatted_one_value(
1.80 paf 420: bool is_big,
421: ushort format,
422: size_t component_size,
423: const uchar *value) {
424: switch(format) {
425: case 1: // unsigned byte
1.91 paf 426: return new VInt((uchar)value[0]);
1.80 paf 427: case 3: // unsigned short
1.91 paf 428: return new VInt(endian_to_ushort(is_big, value));
1.80 paf 429: case 4: // unsigned long
430: // 'double' because parser's Int is signed
1.91 paf 431: return new VDouble(endian_to_uint(is_big, value));
1.80 paf 432: case 5: // unsigned rational
433: {
434: uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
435: uint denominator=endian_to_uint(is_big, value);
436: if(!denominator)
437: return 0;
1.91 paf 438: return new VDouble(((double)numerator)/denominator);
1.80 paf 439: }
440: case 6: // signed byte
1.91 paf 441: return new VInt((signed char)value[0]);
1.80 paf 442: case 8: // signed short
1.91 paf 443: return new VInt((signed short)endian_to_ushort(is_big, value));
1.80 paf 444: case 9: // signed long
1.91 paf 445: return new VInt((signed int)endian_to_uint(is_big, value));
1.80 paf 446: case 10: // signed rational
447: {
448: signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
449: uint denominator=endian_to_uint(is_big, value);
450: if(!denominator)
451: return 0;
1.91 paf 452: return new VDouble(numerator/denominator);
1.80 paf 453: }
454: /*
455: case 11: // single float
1.120 misha 456: @todo
1.80 paf 457: case 12: // double float
1.120 misha 458: @todo
1.80 paf 459: */
460: };
461:
462: return 0;
463: }
464:
1.89 paf 465: // date.C
1.107 paf 466: tm cstr_to_time_t(char *cstr);
1.89 paf 467:
1.91 paf 468: static Value* parse_IFD_entry_formatted_value(bool is_big, ushort format,
469: size_t component_size, uint components_count,
470: const uchar *value) {
1.80 paf 471: if(format==2) { // ascii string, exception: the only type with varying size
1.91 paf 472: const char* cstr=(const char* )value;
473: size_t length=components_count;
1.89 paf 474: // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.91 paf 475: if(length==JPEG_EXIF_DATE_CHARS
1.111 paf 476: && isdigit((unsigned char)cstr[0])
1.91 paf 477: && cstr[length-1]==0) {
1.89 paf 478: char cstr_writable[JPEG_EXIF_DATE_CHARS];
479: strcpy(cstr_writable, cstr);
480:
1.106 paf 481: try {
482: return new VDate(cstr_to_time_t(cstr_writable));
483: }
484: catch(...) { /*ignore bad date times*/ }
1.89 paf 485: }
486:
1.123 misha 487: return new VString(*new String(cstr, String::L_TAINTED));
1.80 paf 488: }
489:
490: if(components_count==1)
1.91 paf 491: return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80 paf 492:
1.91 paf 493: VHash* result=new VHash;
494: HashStringValue& hash=result->hash();
1.80 paf 495: for(uint i=0; i<components_count; i++, value+=component_size) {
1.91 paf 496: hash.put(
1.93 paf 497: String::Body::Format(i),
1.91 paf 498: parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80 paf 499: }
500:
1.91 paf 501: return result;
1.80 paf 502: }
503:
1.91 paf 504: static Value* parse_IFD_entry_value(
1.80 paf 505: bool is_big, Measure_reader& reader, long tiff_base,
506: JPG_Exif_IFD_entry& entry) {
507: size_t format2component_size[]={
508: 0, // undefined
509: 1, // unsigned byte
510: 1, // ascii string
511: 2, // unsigned short
512: 4, // unsigned long
513: 8, // unsigned rational
514: 1, // signed byte
515: 0, // undefined
516: 2, // signed short
517: 4, // signed long
518: 8, // signed rational
519: /*
520: 4, // single float
521: 8, // double float
522: */
523: };
524:
525: ushort format=endian_to_ushort(is_big, entry.format);
526: if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
527: return 0; // format out of range, ignoring
528:
529: size_t component_size=format2component_size[format];
530: if(component_size==0)
531: return 0; // undefined format
532:
533: // You can get the total data byte length by multiplies
534: // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
535: uint components_count=endian_to_uint(is_big, entry.components_count);
1.121 misha 536: uint value_size=component_size*components_count;
1.80 paf 537: // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91 paf 538: Value* result;
1.80 paf 539:
540: if(value_size<=4)
1.91 paf 541: result=parse_IFD_entry_formatted_value(
1.80 paf 542: is_big, format,
543: component_size, components_count,
544: entry.value_or_offset_to_it);
545: else {
546: long remembered=reader.tell();
547: {
548: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91 paf 549: const char* value;
1.80 paf 550: if(reader.read(value, value_size)<sizeof(value_size))
551: return 0;
1.91 paf 552: result=parse_IFD_entry_formatted_value(
1.80 paf 553: is_big, format,
554: component_size, components_count,
555: (const uchar*)value);
556: }
557: reader.seek(remembered, SEEK_SET);
558: }
559:
560: return result;
561: }
562:
1.91 paf 563: static void parse_IFD(HashStringValue& hash,
1.118 misha 564: bool is_big, Measure_reader& reader, long tiff_base, bool gps=false);
1.91 paf 565:
566: static void parse_IFD_entry(HashStringValue& hash,
567: bool is_big, Measure_reader& reader, long tiff_base,
1.118 misha 568: JPG_Exif_IFD_entry& entry, bool gps=false) {
1.80 paf 569: ushort tag=endian_to_ushort(is_big, entry.tag);
1.118 misha 570:
571: if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
1.80 paf 572: long remembered=reader.tell();
573: {
574: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.118 misha 575: parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps);
1.80 paf 576: }
577: reader.seek(remembered, SEEK_SET);
578: return;
579: }
1.118 misha 580:
1.91 paf 581: if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.118 misha 582: if(const char* name=(gps)?exif_gps_tag_value2name.get(tag):exif_tag_value2name.get(tag))
1.93 paf 583: hash.put(String::Body(name), value);
1.80 paf 584: else
1.93 paf 585: hash.put(String::Body::Format(tag), value);
1.80 paf 586: }
1.1 paf 587: }
588:
1.91 paf 589: static void parse_IFD(
590: HashStringValue& hash,
1.118 misha 591: bool is_big, Measure_reader& reader, long tiff_base, bool gps) {
1.91 paf 592: const char* buf;
593: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80 paf 594: return;
1.91 paf 595: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80 paf 596:
597: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
598: for(int i=0; i<directory_entry_count; i++) {
599: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
600: return;
601:
1.118 misha 602: parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf, gps);
1.80 paf 603: }
604: // then goes: LLLLLLLL Offset to next IFD [not going there]
605: }
606:
1.91 paf 607: static Value* parse_exif(Measure_reader& reader, const String& origin_string) {
608: const char* buf;
609: if(reader.read(buf, sizeof(JPG_Exif_segment_begin))<sizeof(JPG_Exif_segment_begin))
1.118 misha 610: throw Exception(IMAGE_FORMAT,
1.91 paf 611: &origin_string,
1.80 paf 612: "not JPEG file - can not fully read Exif segment start");
613:
1.91 paf 614: JPG_Exif_segment_begin *start=(JPG_Exif_segment_begin *)buf;
1.80 paf 615: if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid?
616: return 0; // ignore invalid block
617:
618: uint tiff_base=reader.tell();
619: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
620: return 0;
621:
622: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
623: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
624:
625: uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
626: reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
627:
1.91 paf 628: VHash* vhash=new VHash;
1.80 paf 629:
630: // IFD
1.91 paf 631: parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80 paf 632:
1.91 paf 633: return vhash;
1.80 paf 634: }
635:
1.91 paf 636: static void measure_jpeg(const String& origin_string,
637: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.2 paf 638: // JFIF format markers
1.80 paf 639: const uchar MARKER=0xFF;
640: const uchar CODE_SIZE_A=0xC0;
641: const uchar CODE_SIZE_B=0xC1;
642: const uchar CODE_SIZE_C=0xC2;
643: const uchar CODE_SIZE_D=0xC3;
644: const uchar CODE_EXIF=0xE1;
1.2 paf 645:
1.91 paf 646: const char* buf;
1.18 paf 647: const size_t prefix_size=2;
1.31 parser 648: if(reader.read(buf, prefix_size)<prefix_size)
1.118 misha 649: throw Exception(IMAGE_FORMAT,
1.91 paf 650: &origin_string,
1.34 parser 651: "not JPEG file - too small");
1.80 paf 652: uchar *signature=(uchar *)buf;
1.1 paf 653:
1.31 parser 654: if(!(signature[0]==0xFF && signature[1]==0xD8))
1.118 misha 655: throw Exception(IMAGE_FORMAT,
1.91 paf 656: &origin_string,
1.44 parser 657: "not JPEG file - wrong signature");
1.31 parser 658:
659: while(true) {
1.80 paf 660: uint segment_base=reader.tell()+2/*marker,code*/;
1.31 parser 661: if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79 paf 662: break;
1.31 parser 663: JPG_Segment_head *head=(JPG_Segment_head *)buf;
664:
665: // Verify that it's a valid segment.
666: if(head->marker!=MARKER)
1.118 misha 667: throw Exception(IMAGE_FORMAT,
1.91 paf 668: &origin_string,
1.79 paf 669: "not JPEG file - marker not found");
670:
671: switch(head->code) {
1.118 misha 672: // http://park2.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
1.79 paf 673: case CODE_EXIF:
1.80 paf 674: if(exif && !*exif) // seen .jpg with some xml under EXIF tag, after real exif block :)
1.91 paf 675: *exif=parse_exif(reader, origin_string);
1.1 paf 676: break;
1.31 parser 677:
1.79 paf 678: case CODE_SIZE_A:
679: case CODE_SIZE_B:
680: case CODE_SIZE_C:
681: case CODE_SIZE_D:
682: {
683: // Segments that contain size info
684: if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
1.118 misha 685: throw Exception(IMAGE_FORMAT,
1.91 paf 686: &origin_string,
1.79 paf 687: "not JPEG file - can not fully read Size segment");
688: JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
689:
1.80 paf 690: width=endian_to_ushort(true, body->width);
691: height=endian_to_ushort(true, body->height);
1.79 paf 692: }
1.80 paf 693: return;
1.79 paf 694: };
695:
1.80 paf 696: reader.seek(segment_base+endian_to_ushort(true, head->length), SEEK_SET);
1.31 parser 697: }
698:
1.118 misha 699: throw Exception(IMAGE_FORMAT,
1.91 paf 700: &origin_string,
1.79 paf 701: "broken JPEG file - size frame not found");
1.1 paf 702: }
703:
1.91 paf 704: static void measure_png(const String& origin_string,
1.78 paf 705: Measure_reader& reader, ushort& width, ushort& height) {
1.72 paf 706:
1.91 paf 707: const char* buf;
1.102 paf 708: const size_t head_size=sizeof(PNG_Header);
709: if(reader.read(buf, head_size)<head_size)
1.118 misha 710: throw Exception(IMAGE_FORMAT,
1.91 paf 711: &origin_string,
1.72 paf 712: "not PNG file - too small");
713: PNG_Header *head=(PNG_Header *)buf;
714:
715: if(strncmp(head->signature, "IHDR", 4)!=0)
1.118 misha 716: throw Exception(IMAGE_FORMAT,
1.91 paf 717: &origin_string,
1.72 paf 718: "not PNG file - wrong signature");
719:
1.80 paf 720: width=endian_to_ushort(true, head->width);
721: height=endian_to_ushort(true, head->height);
1.72 paf 722: }
723:
1.1 paf 724: // measure center
725:
1.91 paf 726: static void measure(const String& file_name,
727: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.128 misha 728: const char* file_name_cstr=file_name.taint_cstr(String::L_FILE_SPEC);
1.91 paf 729: if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1 paf 730: cext++;
731: if(strcasecmp(cext, "GIF")==0)
1.91 paf 732: measure_gif(file_name, reader, width, height);
1.1 paf 733: else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.91 paf 734: measure_jpeg(file_name, reader, width, height, exif);
1.72 paf 735: else if(strcasecmp(cext, "PNG")==0)
1.91 paf 736: measure_png(file_name, reader, width, height);
1.1 paf 737: else
1.118 misha 738: throw Exception(IMAGE_FORMAT,
1.1 paf 739: &file_name,
740: "unhandled image file name extension '%s'", cext);
741: } else
1.118 misha 742: throw Exception(IMAGE_FORMAT,
1.1 paf 743: &file_name,
744: "can not determine image type - no file name extension");
745: }
746:
1.77 paf 747: // methods
1.1 paf 748:
1.40 parser 749: #ifndef DOXYGEN
1.77 paf 750: struct File_measure_action_info {
1.91 paf 751: ushort* width;
752: ushort* height;
753: Value** exif;
754: const String* file_name;
1.1 paf 755: };
1.40 parser 756: #endif
1.91 paf 757: static void file_measure_action(
1.98 paf 758: struct stat& /*finfo*/, int f,
759: const String& /*file_spec*/, const char* fname, bool /*as_text*/,
1.77 paf 760: void *context) {
761: File_measure_action_info& info=*static_cast<File_measure_action_info *>(context);
1.1 paf 762:
1.91 paf 763: Measure_file_reader reader(f, *info.file_name, fname);
764: measure(*info.file_name, reader, *info.width, *info.height, info.exif);
1.1 paf 765: }
766:
1.91 paf 767: static void _measure(Request& r, MethodParams& params) {
768: Value& data=params.as_no_junction(0, "data must not be code");
1.1 paf 769:
1.78 paf 770: ushort width=0;
771: ushort height=0;
1.91 paf 772: Value* exif=0;
773: const String* file_name;
1.100 paf 774: if((file_name=data.get_string())) {
775: File_measure_action_info info={
776: &width, &height,
777: &exif,
778: file_name
779: };
1.91 paf 780: file_read_action_under_lock(r.absolute(*file_name),
1.77 paf 781: "measure", file_measure_action, &info);
1.1 paf 782: } else {
1.129 misha 783: VFile* vfile=data.as_vfile(String::L_AS_IS);
1.91 paf 784: file_name=&vfile->fields().get(name_name)->as_string();
1.77 paf 785: Measure_buf_reader reader(
1.91 paf 786: vfile->value_ptr(),
787: vfile->value_size(),
1.77 paf 788: *file_name
789: );
1.91 paf 790: measure(*file_name, reader, width, height, &exif);
1.1 paf 791: }
792:
1.91 paf 793: GET_SELF(r, VImage).set(file_name, width, height, 0, exif);
1.1 paf 794: }
795:
1.40 parser 796: #ifndef DOXYGEN
1.4 paf 797: struct Attrib_info {
1.91 paf 798: String* tag; ///< html tag being constructed
799: HashStringValue* skip; ///< tag attributes not to append to tag string [to skip]
1.4 paf 800: };
1.40 parser 801: #endif
1.91 paf 802: static void append_attrib_pair(
1.133 misha 803: HashStringValue::key_type key,
804: HashStringValue::value_type value,
805: Attrib_info* info) {
806: // skip user-specified, internal(starting with "line-") attributes and border attribute with empty value
807: if(
808: (info->skip && info->skip->get(key))
809: || key.pos("line-")==0
810: || (key=="border" && !value->is_defined())
811: )
1.4 paf 812: return;
813:
1.133 misha 814: // src="a.gif" width="123" ismap[=-1]
1.91 paf 815: *info->tag << " " << key;
816: if(value->is_string() || value->as_int()>=0)
817: *info->tag << "=\"" << value->as_string() << "\"";
1.3 paf 818: }
1.91 paf 819: static void _html(Request& r, MethodParams& params) {
1.3 paf 820:
1.91 paf 821: String tag;
1.3 paf 822: tag << "<img";
1.4 paf 823:
1.91 paf 824: const HashStringValue& fields=GET_SELF(r, VImage).fields();
825: HashStringValue* attribs=0;
1.4 paf 826:
1.91 paf 827: if(params.count()) {
1.69 paf 828: // for backward compatibility: someday was ^html{}
1.133 misha 829: Value& vattribs=r.process_to_value(params[0], false/*don't intercept string*/);
1.82 paf 830: if(!vattribs.is_string()) // allow empty
1.100 paf 831: if((attribs=vattribs.get_hash())) {
832: Attrib_info info={&tag, 0};
1.114 paf 833: attribs->for_each<Attrib_info*>(append_attrib_pair, &info);
1.39 parser 834: } else
1.115 misha 835: throw Exception(PARSER_RUNTIME,
1.91 paf 836: 0,
1.39 parser 837: "attributes must be hash");
838: }
1.4 paf 839:
1.91 paf 840: {
841: Attrib_info info={&tag, attribs};
1.114 paf 842: fields.for_each<Attrib_info*>(append_attrib_pair, &info);
1.91 paf 843: }
1.6 paf 844: tag << " />";
1.3 paf 845: r.write_pass_lang(tag);
846: }
1.8 paf 847:
1.68 paf 848: /// @test wrap FILE to auto-object
1.91 paf 849: static gdImage* load(Request& r,
1.16 paf 850: const String& file_name){
1.128 misha 851: const char* file_name_cstr=r.absolute(file_name).taint_cstr(String::L_FILE_SPEC);
1.16 paf 852: if(FILE *f=fopen(file_name_cstr, "rb")) {
1.91 paf 853: gdImage* image=new gdImage;
854: bool ok=image->CreateFromGif(f);
1.16 paf 855: fclose(f);
1.23 paf 856: if(!ok)
1.118 misha 857: throw Exception(IMAGE_FORMAT,
1.23 paf 858: &file_name,
859: "is not in GIF format");
1.91 paf 860: return image;
1.16 paf 861: } else {
1.68 paf 862: throw Exception("file.missing",
1.91 paf 863: 0,
1.16 paf 864: "can not open '%s'", file_name_cstr);
865: }
866: }
867:
868:
1.91 paf 869: static void _load(Request& r, MethodParams& params) {
1.116 misha 870: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.6 paf 871:
1.91 paf 872: gdImage* image=load(r, file_name);
873: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 874: }
875:
1.91 paf 876: static void _create(Request& r, MethodParams& params) {
877: int width=params.as_int(0, "width must be int", r);
878: int height=params.as_int(1, "height must be int", r);
1.8 paf 879: int bgcolor_value=0xffFFff;
1.91 paf 880: if(params.count()>2)
881: bgcolor_value=params.as_int(2, "color must be int", r);
882: gdImage* image=new gdImage;
883: image->Create(width, height);
884: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
885: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 886: }
887:
1.91 paf 888: static void _gif(Request& r, MethodParams& params) {
1.108 paf 889: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 890:
1.138 misha 891: const String *file_name=params.count()>0?¶ms.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10 paf 892:
1.108 paf 893: gdBuf buf=image.Gif();
1.6 paf 894:
1.91 paf 895: VFile& vfile=*new VFile;
1.137 misha 896:
1.138 misha 897: vfile.set(false/*not tainted*/,
898: (const char*)buf.ptr, buf.size,
899: file_name,
1.137 misha 900: new VString(*new String("image/gif")));
901:
902: vfile.set_mode(false/*binary*/);
1.6 paf 903:
904: r.write_no_lang(vfile);
905: }
906:
1.91 paf 907: static void _line(Request& r, MethodParams& params) {
1.108 paf 908: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 909:
1.108 paf 910: image.Line(
1.91 paf 911: params.as_int(0, "x0 must be int", r),
912: params.as_int(1, "y0 must be int", r),
913: params.as_int(2, "x1 must be int", r),
914: params.as_int(3, "y1 must be int", r),
1.108 paf 915: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 916: }
917:
1.91 paf 918: static void _fill(Request& r, MethodParams& params) {
1.108 paf 919: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 920:
1.108 paf 921: image.Fill(
1.91 paf 922: params.as_int(0, "x must be int", r),
923: params.as_int(1, "y must be int", r),
1.108 paf 924: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 925: }
926:
1.91 paf 927: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 928: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 929:
1.108 paf 930: image.Rectangle(
1.91 paf 931: params.as_int(0, "x0 must be int", r),
932: params.as_int(1, "y0 must be int", r),
933: params.as_int(2, "x1 must be int", r),
934: params.as_int(3, "y1 must be int", r),
1.108 paf 935: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 936: }
937:
1.91 paf 938: static void _bar(Request& r, MethodParams& params) {
1.108 paf 939: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 940:
1.108 paf 941: image.FilledRectangle(
1.91 paf 942: params.as_int(0, "x0 must be int", r),
943: params.as_int(1, "y0 must be int", r),
944: params.as_int(2, "x1 must be int", r),
945: params.as_int(3, "y1 must be int", r),
1.108 paf 946: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 947: }
948:
1.44 parser 949: #ifndef DOXYGEN
1.91 paf 950: static void add_point(Table::element_type row,
951: gdImage::Point **p) {
1.110 paf 952: if(row->count()!=2)
953: throw Exception(0,
954: 0,
955: "coordinates table must contain two columns: x and y values");
1.91 paf 956: (**p).x=row->get(0)->as_int();
957: (**p).y=row->get(1)->as_int();
1.44 parser 958: (*p)++;
959: }
960: #endif
1.135 misha 961: #ifndef DOXYGEN
962: static void add_point(int x, int y,
963: gdImage::Point **p) {
964: (**p).x=x;
965: (**p).y=y;
966: (*p)++;
967: }
968: #endif
1.91 paf 969: static void _replace(Request& r, MethodParams& params) {
1.135 misha 970: int src_color=params.as_int(0, "src color must be int", r);
971: int dest_color=params.as_int(1, "dest color must be int", r);
972:
1.108 paf 973: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 974:
1.135 misha 975: gdImage::Point* all_p=0;
976: size_t count=0;
977: if(params.count() == 3){
978: Table* table=params.as_no_junction(2, COORDINATES_MUST_NOT_BE_CODE).get_table();
979: if(!table)
980: throw Exception(PARSER_RUNTIME,
981: 0,
982: "coordinates must be table");
983: count=table->count();
984: all_p=new(PointerFreeGC) gdImage::Point[count];
985: gdImage::Point* add_p=all_p;
986: table->for_each(add_point, &add_p);
987: } else {
988: int max_x=image.SX()-1;
989: int max_y=image.SY()-1;
990: if(max_x > 0 && max_y > 0){
991: count=4;
992: all_p=new(PointerFreeGC) gdImage::Point[count];
993: gdImage::Point* add_p=all_p;
994: add_point(0, 0, &add_p);
995: add_point(max_x, 0, &add_p);
996: add_point(max_x, max_y, &add_p);
997: add_point(0, max_y, &add_p);
998: }
999: }
1.13 paf 1000:
1.135 misha 1001: if(count)
1002: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 1003: }
1004:
1.91 paf 1005: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 1006: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1007:
1.116 misha 1008: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1009: if(!table)
1.119 misha 1010: throw Exception(PARSER_RUNTIME,
1.91 paf 1011: 0,
1.44 parser 1012: "coordinates must be table");
1013:
1.92 paf 1014: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1015: gdImage::Point *add_p=all_p;
1016: table->for_each(add_point, &add_p);
1.108 paf 1017: image.Polygon(all_p, table->count(),
1018: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 1019: false/*not closed*/);
1020: }
1021:
1.91 paf 1022: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 1023: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1024:
1.116 misha 1025: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1026: if(!table)
1.119 misha 1027: throw Exception(PARSER_RUNTIME,
1.91 paf 1028: 0,
1.44 parser 1029: "coordinates must be table");
1030:
1.92 paf 1031: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1032: gdImage::Point *add_p=all_p;
1033: table->for_each(add_point, &add_p);
1.108 paf 1034: image.Polygon(all_p, table->count(),
1035: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 1036: }
1037:
1.91 paf 1038: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 1039: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1040:
1.116 misha 1041: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1042: if(!table)
1.115 misha 1043: throw Exception(PARSER_RUNTIME,
1.91 paf 1044: 0,
1.44 parser 1045: "coordinates must be table");
1.13 paf 1046:
1.92 paf 1047: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1048: gdImage::Point *add_p=all_p;
1049: table->for_each(add_point, &add_p);
1.108 paf 1050: image.FilledPolygon(all_p, table->count(),
1051: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 1052: }
1053:
1.16 paf 1054: // font
1055:
1.85 paf 1056: #define Y(y)(y+index*height)
1.21 paf 1057:
1.91 paf 1058: // Font class
1059:
1.132 misha 1060: Font::Font(
1061: Charset& asource_charset,
1.91 paf 1062: const String& aalphabet,
1.125 misha 1063: gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.132 misha 1064: fsource_charset(asource_charset),
1065: height(aheight),
1066: monospace(amonospace),
1067: spacebarspace(aspacebarspace),
1.125 misha 1068: letterspacing(aletterspacing),
1.100 paf 1069: ifont(aifont),
1070: alphabet(aalphabet) {
1.132 misha 1071:
1072: if(fsource_charset.isUTF8()){
1073: size_t index=0;
1074: for(UTF8_string_iterator i(alphabet); i.has_next(); )
1075: fletter2index.put_dont_replace(i.next(), index++);
1076: }
1.91 paf 1077: }
1078:
1079: /* ******************************** char ********************************** */
1080:
1081: size_t Font::index_of(char ch) {
1082: if(ch==' ') return STRING_NOT_FOUND;
1083: return alphabet.pos(ch);
1084: }
1085:
1.132 misha 1086: size_t Font::index_of(XMLCh ch) {
1087: if(ch==' ') return STRING_NOT_FOUND;
1088: return fletter2index.get(ch);
1089: }
1090:
1.91 paf 1091: int Font::index_width(size_t index) {
1092: if(index==STRING_NOT_FOUND)
1093: return spacebarspace;
1094: int tr=ifont->GetTransparent();
1095: for(int x=ifont->SX()-1; x>=0; x--) {
1096: for(int y=0; y<height; y++)
1097: if(ifont->GetPixel(x, Y(y))!=tr)
1098: return x+1;
1.16 paf 1099: }
1.91 paf 1100: return 0;
1101: }
1102:
1.108 paf 1103: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1104: if(index!=STRING_NOT_FOUND)
1.108 paf 1105: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1106: }
1107:
1108: /* ******************************** string ********************************** */
1109:
1110: int Font::step_width(int index) {
1.125 misha 1111: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1112: }
1113:
1114: // counts trailing letter_spacing, consider this OK. useful for contiuations
1115: int Font::string_width(const String& s){
1116: const char* cstr=s.cstr();
1117: int result=0;
1.132 misha 1118:
1119: if(fsource_charset.isUTF8()){
1120: for(UTF8_string_iterator i(s); i.has_next(); )
1121: result+=step_width(index_of(i.next()));
1122: } else {
1123: for(const char* current=cstr; *current; current++)
1124: result+=step_width(index_of(*current));
1125: }
1126:
1.91 paf 1127: return result;
1128: }
1129:
1.108 paf 1130: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1131: const char* cstr=s.cstr();
1.132 misha 1132:
1133: if(fsource_charset.isUTF8()){
1134: for(UTF8_string_iterator i(s); i.has_next(); ){
1135: size_t index=index_of(i.next());
1136: index_display(image, x, y, index);
1137: x+=step_width(index);
1138: }
1139: } else {
1140: for(const char* current=cstr; *current; current++) {
1141: size_t index=index_of(*current);
1142: index_display(image, x, y, index);
1143: x+=step_width(index);
1144: }
1.87 paf 1145: }
1.91 paf 1146: }
1.87 paf 1147:
1.91 paf 1148: //
1.35 parser 1149:
1.16 paf 1150:
1.91 paf 1151: static void _font(Request& r, MethodParams& params) {
1152: const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131 misha 1153: size_t alphabet_length=alphabet.length(r.charsets.source());
1154: if(!alphabet_length)
1.115 misha 1155: throw Exception(PARSER_RUNTIME,
1.91 paf 1156: 0,
1.37 parser 1157: "alphabet must not be empty");
1.84 paf 1158:
1.125 misha 1159: gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1160:
1.126 misha 1161: int spacebar_width=image->SX();
1.125 misha 1162: int monospace_width=0; // proportional
1163: int letter_spacing=1;
1164: if(params.count()>2){
1165: if(HashStringValue* options=params.as_no_junction(2, "param must be int or hash").get_hash()){
1166: // third option is hash
1167: if(params.count()>3)
1168: throw Exception(PARSER_RUNTIME,
1169: 0,
1170: "too many options were specified");
1171: int valid_options=0;
1172: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1173: valid_options++;
1174: spacebar_width=r.process_to_value(*vspacebar_width).as_int();
1175: }
1176: if(Value* vmonospace_width=options->get(monospace_width_name)){
1177: valid_options++;
1178: monospace_width=r.process_to_value(*vmonospace_width).as_int();
1179: if(!monospace_width)
1180: monospace_width=image->SX();
1181: }
1182: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1183: valid_options++;
1184: letter_spacing=r.process_to_value(*vletter_spacing).as_int();
1185: }
1186: if(valid_options!=options->count())
1.134 misha 1187: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1188: } else {
1189: // backward
1190: spacebar_width=params.as_int(2, "spacebar_width must be int", r);
1191: if(params.count()>3) {
1192: monospace_width=params.as_int(3, "monospace_width must be int", r);
1193: if(!monospace_width)
1194: monospace_width=image->SX();
1195: }
1196: }
1197: }
1198:
1.131 misha 1199: if(int remainder=image->SY() % alphabet_length)
1.115 misha 1200: throw Exception(PARSER_RUNTIME,
1.91 paf 1201: 0,
1.84 paf 1202: "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
1.131 misha 1203: image->SY(), alphabet_length, remainder);
1.37 parser 1204:
1.108 paf 1205: GET_SELF(r, VImage).set_font(new Font(
1.132 misha 1206: r.charsets.source(),
1.37 parser 1207: alphabet,
1.36 parser 1208: image,
1.131 misha 1209: image->SY() / alphabet_length, monospace_width, spacebar_width, letter_spacing));
1.16 paf 1210: }
1211:
1.91 paf 1212: static void _text(Request& r, MethodParams& params) {
1213: int x=params.as_int(0, "x must be int", r);
1214: int y=params.as_int(1, "y must be int", r);
1215: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1216:
1.91 paf 1217: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1218: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1219: }
1220:
1.91 paf 1221: static void _length(Request& r, MethodParams& params) {
1222: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1223:
1.91 paf 1224: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1225: r.write_no_lang(*new VInt(vimage.font().string_width(s)));
1.47 parser 1226: }
1227:
1.91 paf 1228: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1229: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1230:
1.108 paf 1231: image.Arc(
1.91 paf 1232: params.as_int(0, "center_x must be int", r),
1233: params.as_int(1, "center_y must be int", r),
1234: params.as_int(2, "width must be int", r),
1235: params.as_int(3, "height must be int", r),
1236: params.as_int(4, "start degrees must be int", r),
1237: params.as_int(5, "end degrees must be int", r),
1.108 paf 1238: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1239: }
1240:
1.91 paf 1241: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1242: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1243:
1.108 paf 1244: image.Sector(
1.91 paf 1245: params.as_int(0, "center_x must be int", r),
1246: params.as_int(1, "center_y must be int", r),
1247: params.as_int(2, "width must be int", r),
1248: params.as_int(3, "height must be int", r),
1249: params.as_int(4, "start degrees must be int", r),
1250: params.as_int(5, "end degrees must be int", r),
1.108 paf 1251: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1252: }
1253:
1.91 paf 1254: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1255: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1256:
1.91 paf 1257: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1258: image.Arc(
1.91 paf 1259: params.as_int(0, "center_x must be int", r),
1260: params.as_int(1, "center_y must be int", r),
1.50 parser 1261: size, //w
1262: size, //h
1.48 parser 1263: 0, //s
1264: 360, //e
1.108 paf 1265: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1266: }
1267:
1.108 paf 1268: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1269: Value& value=params.as_no_junction(index, msg);
1.53 parser 1270:
1.130 misha 1271: if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108 paf 1272: return static_cast<VImage *>(vimage)->image();
1.75 paf 1273: } else
1.115 misha 1274: throw Exception(PARSER_RUNTIME,
1.91 paf 1275: 0,
1.53 parser 1276: msg);
1277: }
1278:
1.91 paf 1279: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1280: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1281:
1.108 paf 1282: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1283:
1.91 paf 1284: int sx=params.as_int(1, "src_x must be int", r);
1285: int sy=params.as_int(2, "src_y must be int", r);
1286: int sw=params.as_int(3, "src_w must be int", r);
1287: int sh=params.as_int(4, "src_h must be int", r);
1288: int dx=params.as_int(5, "dest_x must be int", r);
1289: int dy=params.as_int(6, "dest_y must be int", r);
1290: if(params.count()>1+2+2+2) {
1291: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1292: int dh=(int)(params.count()>1+2+2+2+1?
1293: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1294: int tolerance=params.count()>1+2+2+2+2?
1295: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1296:
1.108 paf 1297: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1298: } else
1.108 paf 1299: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1300: }
1301:
1302: static void _pixel(Request& r, MethodParams& params) {
1303: gdImage& image=GET_SELF(r, VImage).image();
1304:
1.109 paf 1305: int x=params.as_int(0, "x must be int", r);
1306: int y=params.as_int(1, "y must be int", r);
1307:
1308: if(params.count()>2) {
1309: image.SetPixel(x, y,
1310: image.Color(params.as_int(2, "color must be int", r)));
1311: } else
1312: r.write_no_lang(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1313: }
1314:
1315:
1.22 paf 1316: // constructor
1317:
1.91 paf 1318: MImage::MImage(): Methoded("image") {
1.1 paf 1319: // ^image:measure[DATA]
1.22 paf 1320: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 1321:
1.25 paf 1322: // ^image.html[]
1323: // ^image.html[hash]
1.22 paf 1324: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1325:
1.25 paf 1326: // ^image.load[background.gif]
1.22 paf 1327: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1328:
1.25 paf 1329: // ^image.create[width;height] bgcolor=white
1330: // ^image.create[width;height;bgcolor]
1.22 paf 1331: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1332:
1.25 paf 1333: // ^image.gif[]
1.95 paf 1334: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1335:
1.25 paf 1336: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1337: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1338:
1.25 paf 1339: // ^image.fill(x;y;color)
1.22 paf 1340: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1341:
1.25 paf 1342: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1343: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1344:
1.25 paf 1345: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1346: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1347:
1.44 parser 1348: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1349: // ^image.replace(color-source;color-dest)
1350: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1351:
1352: // ^image.polyline(color)[table x:y]
1353: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1354:
1.44 parser 1355: // ^image.polygon(color)[table x:y]
1356: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1357:
1.44 parser 1358: // ^image.polybar(color)[table x:y]
1359: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1360:
1.125 misha 1361: // ^image.font[alPHAbet;font-file-name.gif]
1362: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1363: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1364: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1365: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1366:
1.125 misha 1367: // ^image.text(x;y)[text]
1.22 paf 1368: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1369:
1.125 misha 1370: // ^image.length[text]
1.47 parser 1371: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1372:
1.48 parser 1373: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1374: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1375:
1376: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1377: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1378:
1379: // ^image.circle(center x;center y;r;color)
1380: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1381:
1.56 parser 1382: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1383: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1384:
1385: // ^image.pixel(x;y)[(color)]
1.109 paf 1386: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1387: }
E-mail: