Annotation of parser3/src/classes/image.C, revision 1.137
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.137 ! misha 8: static const char * const IDENT_IMAGE_C="$Date: 2010-10-21 15:06:27 $";
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.95 paf 893: const String *file_name=0;
894: if(params.count()>0)
1.116 misha 895: file_name=¶ms.as_string(0, FILE_NAME_MUST_BE_STRING);
1.10 paf 896:
1.108 paf 897: gdBuf buf=image.Gif();
1.6 paf 898:
1.91 paf 899: VFile& vfile=*new VFile;
1.137 ! misha 900:
1.20 paf 901: vfile.set(false/*not tainted*/,
1.95 paf 902: (const char*)buf.ptr, buf.size,
1.128 misha 903: file_name? file_name->taint_cstr(String::L_FILE_SPEC): 0,
1.137 ! misha 904: new VString(*new String("image/gif")));
! 905:
! 906: vfile.set_mode(false/*binary*/);
1.6 paf 907:
908: r.write_no_lang(vfile);
909: }
910:
1.91 paf 911: static void _line(Request& r, MethodParams& params) {
1.108 paf 912: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 913:
1.108 paf 914: image.Line(
1.91 paf 915: params.as_int(0, "x0 must be int", r),
916: params.as_int(1, "y0 must be int", r),
917: params.as_int(2, "x1 must be int", r),
918: params.as_int(3, "y1 must be int", r),
1.108 paf 919: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 920: }
921:
1.91 paf 922: static void _fill(Request& r, MethodParams& params) {
1.108 paf 923: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 924:
1.108 paf 925: image.Fill(
1.91 paf 926: params.as_int(0, "x must be int", r),
927: params.as_int(1, "y must be int", r),
1.108 paf 928: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 929: }
930:
1.91 paf 931: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 932: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 933:
1.108 paf 934: image.Rectangle(
1.91 paf 935: params.as_int(0, "x0 must be int", r),
936: params.as_int(1, "y0 must be int", r),
937: params.as_int(2, "x1 must be int", r),
938: params.as_int(3, "y1 must be int", r),
1.108 paf 939: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 940: }
941:
1.91 paf 942: static void _bar(Request& r, MethodParams& params) {
1.108 paf 943: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 944:
1.108 paf 945: image.FilledRectangle(
1.91 paf 946: params.as_int(0, "x0 must be int", r),
947: params.as_int(1, "y0 must be int", r),
948: params.as_int(2, "x1 must be int", r),
949: params.as_int(3, "y1 must be int", r),
1.108 paf 950: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 951: }
952:
1.44 parser 953: #ifndef DOXYGEN
1.91 paf 954: static void add_point(Table::element_type row,
955: gdImage::Point **p) {
1.110 paf 956: if(row->count()!=2)
957: throw Exception(0,
958: 0,
959: "coordinates table must contain two columns: x and y values");
1.91 paf 960: (**p).x=row->get(0)->as_int();
961: (**p).y=row->get(1)->as_int();
1.44 parser 962: (*p)++;
963: }
964: #endif
1.135 misha 965: #ifndef DOXYGEN
966: static void add_point(int x, int y,
967: gdImage::Point **p) {
968: (**p).x=x;
969: (**p).y=y;
970: (*p)++;
971: }
972: #endif
1.91 paf 973: static void _replace(Request& r, MethodParams& params) {
1.135 misha 974: int src_color=params.as_int(0, "src color must be int", r);
975: int dest_color=params.as_int(1, "dest color must be int", r);
976:
1.108 paf 977: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 978:
1.135 misha 979: gdImage::Point* all_p=0;
980: size_t count=0;
981: if(params.count() == 3){
982: Table* table=params.as_no_junction(2, COORDINATES_MUST_NOT_BE_CODE).get_table();
983: if(!table)
984: throw Exception(PARSER_RUNTIME,
985: 0,
986: "coordinates must be table");
987: count=table->count();
988: all_p=new(PointerFreeGC) gdImage::Point[count];
989: gdImage::Point* add_p=all_p;
990: table->for_each(add_point, &add_p);
991: } else {
992: int max_x=image.SX()-1;
993: int max_y=image.SY()-1;
994: if(max_x > 0 && max_y > 0){
995: count=4;
996: all_p=new(PointerFreeGC) gdImage::Point[count];
997: gdImage::Point* add_p=all_p;
998: add_point(0, 0, &add_p);
999: add_point(max_x, 0, &add_p);
1000: add_point(max_x, max_y, &add_p);
1001: add_point(0, max_y, &add_p);
1002: }
1003: }
1.13 paf 1004:
1.135 misha 1005: if(count)
1006: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 1007: }
1008:
1.91 paf 1009: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 1010: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1011:
1.116 misha 1012: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1013: if(!table)
1.119 misha 1014: throw Exception(PARSER_RUNTIME,
1.91 paf 1015: 0,
1.44 parser 1016: "coordinates must be table");
1017:
1.92 paf 1018: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1019: gdImage::Point *add_p=all_p;
1020: table->for_each(add_point, &add_p);
1.108 paf 1021: image.Polygon(all_p, table->count(),
1022: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 1023: false/*not closed*/);
1024: }
1025:
1.91 paf 1026: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 1027: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1028:
1.116 misha 1029: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1030: if(!table)
1.119 misha 1031: throw Exception(PARSER_RUNTIME,
1.91 paf 1032: 0,
1.44 parser 1033: "coordinates must be table");
1034:
1.92 paf 1035: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1036: gdImage::Point *add_p=all_p;
1037: table->for_each(add_point, &add_p);
1.108 paf 1038: image.Polygon(all_p, table->count(),
1039: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 1040: }
1041:
1.91 paf 1042: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 1043: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1044:
1.116 misha 1045: Table* table=params.as_no_junction(1, COORDINATES_MUST_NOT_BE_CODE).get_table();
1.44 parser 1046: if(!table)
1.115 misha 1047: throw Exception(PARSER_RUNTIME,
1.91 paf 1048: 0,
1.44 parser 1049: "coordinates must be table");
1.13 paf 1050:
1.92 paf 1051: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1052: gdImage::Point *add_p=all_p;
1053: table->for_each(add_point, &add_p);
1.108 paf 1054: image.FilledPolygon(all_p, table->count(),
1055: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 1056: }
1057:
1.16 paf 1058: // font
1059:
1.85 paf 1060: #define Y(y)(y+index*height)
1.21 paf 1061:
1.91 paf 1062: // Font class
1063:
1.132 misha 1064: Font::Font(
1065: Charset& asource_charset,
1.91 paf 1066: const String& aalphabet,
1.125 misha 1067: gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.132 misha 1068: fsource_charset(asource_charset),
1069: height(aheight),
1070: monospace(amonospace),
1071: spacebarspace(aspacebarspace),
1.125 misha 1072: letterspacing(aletterspacing),
1.100 paf 1073: ifont(aifont),
1074: alphabet(aalphabet) {
1.132 misha 1075:
1076: if(fsource_charset.isUTF8()){
1077: size_t index=0;
1078: for(UTF8_string_iterator i(alphabet); i.has_next(); )
1079: fletter2index.put_dont_replace(i.next(), index++);
1080: }
1.91 paf 1081: }
1082:
1083: /* ******************************** char ********************************** */
1084:
1085: size_t Font::index_of(char ch) {
1086: if(ch==' ') return STRING_NOT_FOUND;
1087: return alphabet.pos(ch);
1088: }
1089:
1.132 misha 1090: size_t Font::index_of(XMLCh ch) {
1091: if(ch==' ') return STRING_NOT_FOUND;
1092: return fletter2index.get(ch);
1093: }
1094:
1.91 paf 1095: int Font::index_width(size_t index) {
1096: if(index==STRING_NOT_FOUND)
1097: return spacebarspace;
1098: int tr=ifont->GetTransparent();
1099: for(int x=ifont->SX()-1; x>=0; x--) {
1100: for(int y=0; y<height; y++)
1101: if(ifont->GetPixel(x, Y(y))!=tr)
1102: return x+1;
1.16 paf 1103: }
1.91 paf 1104: return 0;
1105: }
1106:
1.108 paf 1107: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1108: if(index!=STRING_NOT_FOUND)
1.108 paf 1109: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1110: }
1111:
1112: /* ******************************** string ********************************** */
1113:
1114: int Font::step_width(int index) {
1.125 misha 1115: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1116: }
1117:
1118: // counts trailing letter_spacing, consider this OK. useful for contiuations
1119: int Font::string_width(const String& s){
1120: const char* cstr=s.cstr();
1121: int result=0;
1.132 misha 1122:
1123: if(fsource_charset.isUTF8()){
1124: for(UTF8_string_iterator i(s); i.has_next(); )
1125: result+=step_width(index_of(i.next()));
1126: } else {
1127: for(const char* current=cstr; *current; current++)
1128: result+=step_width(index_of(*current));
1129: }
1130:
1.91 paf 1131: return result;
1132: }
1133:
1.108 paf 1134: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1135: const char* cstr=s.cstr();
1.132 misha 1136:
1137: if(fsource_charset.isUTF8()){
1138: for(UTF8_string_iterator i(s); i.has_next(); ){
1139: size_t index=index_of(i.next());
1140: index_display(image, x, y, index);
1141: x+=step_width(index);
1142: }
1143: } else {
1144: for(const char* current=cstr; *current; current++) {
1145: size_t index=index_of(*current);
1146: index_display(image, x, y, index);
1147: x+=step_width(index);
1148: }
1.87 paf 1149: }
1.91 paf 1150: }
1.87 paf 1151:
1.91 paf 1152: //
1.35 parser 1153:
1.16 paf 1154:
1.91 paf 1155: static void _font(Request& r, MethodParams& params) {
1156: const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131 misha 1157: size_t alphabet_length=alphabet.length(r.charsets.source());
1158: if(!alphabet_length)
1.115 misha 1159: throw Exception(PARSER_RUNTIME,
1.91 paf 1160: 0,
1.37 parser 1161: "alphabet must not be empty");
1.84 paf 1162:
1.125 misha 1163: gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1164:
1.126 misha 1165: int spacebar_width=image->SX();
1.125 misha 1166: int monospace_width=0; // proportional
1167: int letter_spacing=1;
1168: if(params.count()>2){
1169: if(HashStringValue* options=params.as_no_junction(2, "param must be int or hash").get_hash()){
1170: // third option is hash
1171: if(params.count()>3)
1172: throw Exception(PARSER_RUNTIME,
1173: 0,
1174: "too many options were specified");
1175: int valid_options=0;
1176: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1177: valid_options++;
1178: spacebar_width=r.process_to_value(*vspacebar_width).as_int();
1179: }
1180: if(Value* vmonospace_width=options->get(monospace_width_name)){
1181: valid_options++;
1182: monospace_width=r.process_to_value(*vmonospace_width).as_int();
1183: if(!monospace_width)
1184: monospace_width=image->SX();
1185: }
1186: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1187: valid_options++;
1188: letter_spacing=r.process_to_value(*vletter_spacing).as_int();
1189: }
1190: if(valid_options!=options->count())
1.134 misha 1191: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1192: } else {
1193: // backward
1194: spacebar_width=params.as_int(2, "spacebar_width must be int", r);
1195: if(params.count()>3) {
1196: monospace_width=params.as_int(3, "monospace_width must be int", r);
1197: if(!monospace_width)
1198: monospace_width=image->SX();
1199: }
1200: }
1201: }
1202:
1.131 misha 1203: if(int remainder=image->SY() % alphabet_length)
1.115 misha 1204: throw Exception(PARSER_RUNTIME,
1.91 paf 1205: 0,
1.84 paf 1206: "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
1.131 misha 1207: image->SY(), alphabet_length, remainder);
1.37 parser 1208:
1.108 paf 1209: GET_SELF(r, VImage).set_font(new Font(
1.132 misha 1210: r.charsets.source(),
1.37 parser 1211: alphabet,
1.36 parser 1212: image,
1.131 misha 1213: image->SY() / alphabet_length, monospace_width, spacebar_width, letter_spacing));
1.16 paf 1214: }
1215:
1.91 paf 1216: static void _text(Request& r, MethodParams& params) {
1217: int x=params.as_int(0, "x must be int", r);
1218: int y=params.as_int(1, "y must be int", r);
1219: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1220:
1.91 paf 1221: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1222: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1223: }
1224:
1.91 paf 1225: static void _length(Request& r, MethodParams& params) {
1226: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1227:
1.91 paf 1228: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1229: r.write_no_lang(*new VInt(vimage.font().string_width(s)));
1.47 parser 1230: }
1231:
1.91 paf 1232: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1233: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1234:
1.108 paf 1235: image.Arc(
1.91 paf 1236: params.as_int(0, "center_x must be int", r),
1237: params.as_int(1, "center_y must be int", r),
1238: params.as_int(2, "width must be int", r),
1239: params.as_int(3, "height must be int", r),
1240: params.as_int(4, "start degrees must be int", r),
1241: params.as_int(5, "end degrees must be int", r),
1.108 paf 1242: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1243: }
1244:
1.91 paf 1245: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1246: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1247:
1.108 paf 1248: image.Sector(
1.91 paf 1249: params.as_int(0, "center_x must be int", r),
1250: params.as_int(1, "center_y must be int", r),
1251: params.as_int(2, "width must be int", r),
1252: params.as_int(3, "height must be int", r),
1253: params.as_int(4, "start degrees must be int", r),
1254: params.as_int(5, "end degrees must be int", r),
1.108 paf 1255: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1256: }
1257:
1.91 paf 1258: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1259: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1260:
1.91 paf 1261: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1262: image.Arc(
1.91 paf 1263: params.as_int(0, "center_x must be int", r),
1264: params.as_int(1, "center_y must be int", r),
1.50 parser 1265: size, //w
1266: size, //h
1.48 parser 1267: 0, //s
1268: 360, //e
1.108 paf 1269: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1270: }
1271:
1.108 paf 1272: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1273: Value& value=params.as_no_junction(index, msg);
1.53 parser 1274:
1.130 misha 1275: if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108 paf 1276: return static_cast<VImage *>(vimage)->image();
1.75 paf 1277: } else
1.115 misha 1278: throw Exception(PARSER_RUNTIME,
1.91 paf 1279: 0,
1.53 parser 1280: msg);
1281: }
1282:
1.91 paf 1283: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1284: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1285:
1.108 paf 1286: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1287:
1.91 paf 1288: int sx=params.as_int(1, "src_x must be int", r);
1289: int sy=params.as_int(2, "src_y must be int", r);
1290: int sw=params.as_int(3, "src_w must be int", r);
1291: int sh=params.as_int(4, "src_h must be int", r);
1292: int dx=params.as_int(5, "dest_x must be int", r);
1293: int dy=params.as_int(6, "dest_y must be int", r);
1294: if(params.count()>1+2+2+2) {
1295: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1296: int dh=(int)(params.count()>1+2+2+2+1?
1297: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1298: int tolerance=params.count()>1+2+2+2+2?
1299: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1300:
1.108 paf 1301: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1302: } else
1.108 paf 1303: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1304: }
1305:
1306: static void _pixel(Request& r, MethodParams& params) {
1307: gdImage& image=GET_SELF(r, VImage).image();
1308:
1.109 paf 1309: int x=params.as_int(0, "x must be int", r);
1310: int y=params.as_int(1, "y must be int", r);
1311:
1312: if(params.count()>2) {
1313: image.SetPixel(x, y,
1314: image.Color(params.as_int(2, "color must be int", r)));
1315: } else
1316: r.write_no_lang(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1317: }
1318:
1319:
1.22 paf 1320: // constructor
1321:
1.91 paf 1322: MImage::MImage(): Methoded("image") {
1.1 paf 1323: // ^image:measure[DATA]
1.22 paf 1324: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 1325:
1.25 paf 1326: // ^image.html[]
1327: // ^image.html[hash]
1.22 paf 1328: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1329:
1.25 paf 1330: // ^image.load[background.gif]
1.22 paf 1331: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1332:
1.25 paf 1333: // ^image.create[width;height] bgcolor=white
1334: // ^image.create[width;height;bgcolor]
1.22 paf 1335: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1336:
1.25 paf 1337: // ^image.gif[]
1.95 paf 1338: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1339:
1.25 paf 1340: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1341: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1342:
1.25 paf 1343: // ^image.fill(x;y;color)
1.22 paf 1344: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1345:
1.25 paf 1346: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1347: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1348:
1.25 paf 1349: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1350: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1351:
1.44 parser 1352: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1353: // ^image.replace(color-source;color-dest)
1354: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1355:
1356: // ^image.polyline(color)[table x:y]
1357: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1358:
1.44 parser 1359: // ^image.polygon(color)[table x:y]
1360: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1361:
1.44 parser 1362: // ^image.polybar(color)[table x:y]
1363: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1364:
1.125 misha 1365: // ^image.font[alPHAbet;font-file-name.gif]
1366: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1367: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1368: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1369: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1370:
1.125 misha 1371: // ^image.text(x;y)[text]
1.22 paf 1372: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1373:
1.125 misha 1374: // ^image.length[text]
1.47 parser 1375: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1376:
1.48 parser 1377: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1378: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1379:
1380: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1381: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1382:
1383: // ^image.circle(center x;center y;r;color)
1384: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1385:
1.56 parser 1386: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1387: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1388:
1389: // ^image.pixel(x;y)[(color)]
1.109 paf 1390: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1391: }
E-mail: