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