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