Annotation of parser3/src/classes/image.C, revision 1.160
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.160 ! moko 28: volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.159 2017/02/06 16:17:12 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.40 parser 739: #ifndef DOXYGEN
1.4 paf 740: struct Attrib_info {
1.91 paf 741: String* tag; ///< html tag being constructed
742: HashStringValue* skip; ///< tag attributes not to append to tag string [to skip]
1.4 paf 743: };
1.40 parser 744: #endif
1.155 moko 745: static void append_attrib_pair(HashStringValue::key_type key, HashStringValue::value_type value, Attrib_info* info) {
1.133 misha 746: // skip user-specified, internal(starting with "line-") attributes and border attribute with empty value
747: if(
748: (info->skip && info->skip->get(key))
749: || key.pos("line-")==0
750: || (key=="border" && !value->is_defined())
751: )
1.4 paf 752: return;
753:
1.133 misha 754: // src="a.gif" width="123" ismap[=-1]
1.91 paf 755: *info->tag << " " << key;
756: if(value->is_string() || value->as_int()>=0)
757: *info->tag << "=\"" << value->as_string() << "\"";
1.3 paf 758: }
1.91 paf 759: static void _html(Request& r, MethodParams& params) {
1.3 paf 760:
1.91 paf 761: String tag;
1.3 paf 762: tag << "<img";
1.4 paf 763:
1.91 paf 764: const HashStringValue& fields=GET_SELF(r, VImage).fields();
765: HashStringValue* attribs=0;
1.4 paf 766:
1.91 paf 767: if(params.count()) {
1.69 paf 768: // for backward compatibility: someday was ^html{}
1.154 moko 769: Value& vattribs=r.process(params[0]);
1.145 moko 770: if(!vattribs.is_string()) { // allow empty
1.100 paf 771: if((attribs=vattribs.get_hash())) {
772: Attrib_info info={&tag, 0};
1.114 paf 773: attribs->for_each<Attrib_info*>(append_attrib_pair, &info);
1.39 parser 774: } else
1.155 moko 775: throw Exception(PARSER_RUNTIME, 0, "attributes must be hash");
1.145 moko 776: }
1.39 parser 777: }
1.4 paf 778:
1.91 paf 779: {
780: Attrib_info info={&tag, attribs};
1.114 paf 781: fields.for_each<Attrib_info*>(append_attrib_pair, &info);
1.91 paf 782: }
1.6 paf 783: tag << " />";
1.157 moko 784: r.write(tag);
1.3 paf 785: }
1.8 paf 786:
1.68 paf 787: /// @test wrap FILE to auto-object
1.155 moko 788: static gdImage* load(Request& r, const String& file_name){
1.128 misha 789: const char* file_name_cstr=r.absolute(file_name).taint_cstr(String::L_FILE_SPEC);
1.159 moko 790: if(FILE *f=pa_fopen(file_name_cstr, "rb")) {
1.91 paf 791: gdImage* image=new gdImage;
792: bool ok=image->CreateFromGif(f);
1.16 paf 793: fclose(f);
1.23 paf 794: if(!ok)
1.155 moko 795: throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");
1.91 paf 796: return image;
1.16 paf 797: } else {
1.155 moko 798: throw Exception("file.missing", 0, "can not open '%s'", file_name_cstr);
1.16 paf 799: }
800: }
801:
802:
1.91 paf 803: static void _load(Request& r, MethodParams& params) {
1.116 misha 804: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.6 paf 805:
1.91 paf 806: gdImage* image=load(r, file_name);
807: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 808: }
809:
1.91 paf 810: static void _create(Request& r, MethodParams& params) {
811: int width=params.as_int(0, "width must be int", r);
812: int height=params.as_int(1, "height must be int", r);
1.8 paf 813: int bgcolor_value=0xffFFff;
1.91 paf 814: if(params.count()>2)
815: bgcolor_value=params.as_int(2, "color must be int", r);
816: gdImage* image=new gdImage;
817: image->Create(width, height);
818: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
819: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 820: }
821:
1.91 paf 822: static void _gif(Request& r, MethodParams& params) {
1.108 paf 823: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 824:
1.138 misha 825: const String *file_name=params.count()>0?¶ms.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10 paf 826:
1.108 paf 827: gdBuf buf=image.Gif();
1.6 paf 828:
1.91 paf 829: VFile& vfile=*new VFile;
1.137 misha 830:
1.142 moko 831: vfile.set_binary(false/*not tainted*/, (const char *)buf.ptr, buf.size, file_name, new VString(*new String("image/gif")));
1.137 misha 832:
1.157 moko 833: r.write(vfile);
1.6 paf 834: }
835:
1.91 paf 836: static void _line(Request& r, MethodParams& params) {
1.108 paf 837: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 838:
1.108 paf 839: image.Line(
1.91 paf 840: params.as_int(0, "x0 must be int", r),
841: params.as_int(1, "y0 must be int", r),
842: params.as_int(2, "x1 must be int", r),
843: params.as_int(3, "y1 must be int", r),
1.108 paf 844: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 845: }
846:
1.91 paf 847: static void _fill(Request& r, MethodParams& params) {
1.108 paf 848: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 849:
1.108 paf 850: image.Fill(
1.91 paf 851: params.as_int(0, "x must be int", r),
852: params.as_int(1, "y must be int", r),
1.108 paf 853: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 854: }
855:
1.91 paf 856: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 857: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 858:
1.108 paf 859: image.Rectangle(
1.91 paf 860: params.as_int(0, "x0 must be int", r),
861: params.as_int(1, "y0 must be int", r),
862: params.as_int(2, "x1 must be int", r),
863: params.as_int(3, "y1 must be int", r),
1.108 paf 864: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 865: }
866:
1.91 paf 867: static void _bar(Request& r, MethodParams& params) {
1.108 paf 868: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 869:
1.108 paf 870: image.FilledRectangle(
1.91 paf 871: params.as_int(0, "x0 must be int", r),
872: params.as_int(1, "y0 must be int", r),
873: params.as_int(2, "x1 must be int", r),
874: params.as_int(3, "y1 must be int", r),
1.108 paf 875: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 876: }
877:
1.44 parser 878: #ifndef DOXYGEN
1.155 moko 879: static void add_point(Table::element_type row, gdImage::Point **p) {
1.110 paf 880: if(row->count()!=2)
1.155 moko 881: throw Exception(0, 0, "coordinates table must contain two columns: x and y values");
1.91 paf 882: (**p).x=row->get(0)->as_int();
883: (**p).y=row->get(1)->as_int();
1.44 parser 884: (*p)++;
885: }
886: #endif
1.135 misha 887: #ifndef DOXYGEN
1.155 moko 888: static void add_point(int x, int y, gdImage::Point **p) {
1.135 misha 889: (**p).x=x;
890: (**p).y=y;
891: (*p)++;
892: }
893: #endif
1.91 paf 894: static void _replace(Request& r, MethodParams& params) {
1.135 misha 895: int src_color=params.as_int(0, "src color must be int", r);
896: int dest_color=params.as_int(1, "dest color must be int", r);
897:
1.108 paf 898: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 899:
1.135 misha 900: gdImage::Point* all_p=0;
901: size_t count=0;
902: if(params.count() == 3){
1.140 misha 903: Table* table=params.as_table(2, "coordinates");
1.135 misha 904: count=table->count();
905: all_p=new(PointerFreeGC) gdImage::Point[count];
906: gdImage::Point* add_p=all_p;
907: table->for_each(add_point, &add_p);
908: } else {
909: int max_x=image.SX()-1;
910: int max_y=image.SY()-1;
911: if(max_x > 0 && max_y > 0){
912: count=4;
913: all_p=new(PointerFreeGC) gdImage::Point[count];
914: gdImage::Point* add_p=all_p;
915: add_point(0, 0, &add_p);
916: add_point(max_x, 0, &add_p);
917: add_point(max_x, max_y, &add_p);
918: add_point(0, max_y, &add_p);
919: }
920: }
1.13 paf 921:
1.135 misha 922: if(count)
923: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 924: }
925:
1.91 paf 926: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 927: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 928:
1.140 misha 929: Table* table=params.as_table(1, "coordinates");
1.44 parser 930:
1.92 paf 931: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 932: gdImage::Point *add_p=all_p;
933: table->for_each(add_point, &add_p);
1.108 paf 934: image.Polygon(all_p, table->count(),
935: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 936: false/*not closed*/);
937: }
938:
1.91 paf 939: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 940: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 941:
1.140 misha 942: Table* table=(Table*)params.as_table(1, "coordinates");
1.44 parser 943:
1.92 paf 944: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 945: gdImage::Point *add_p=all_p;
946: table->for_each(add_point, &add_p);
1.108 paf 947: image.Polygon(all_p, table->count(),
948: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 949: }
950:
1.91 paf 951: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 952: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 953:
1.140 misha 954: Table* table=(Table*)params.as_table(1, "coordinates");
1.13 paf 955:
1.92 paf 956: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 957: gdImage::Point *add_p=all_p;
958: table->for_each(add_point, &add_p);
1.108 paf 959: image.FilledPolygon(all_p, table->count(),
960: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 961: }
962:
1.16 paf 963: // font
964:
1.85 paf 965: #define Y(y)(y+index*height)
1.21 paf 966:
1.91 paf 967: // Font class
968:
1.155 moko 969: Font::Font(Charset& asource_charset, const String& aalphabet, gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.158 moko 970: letterspacing(aletterspacing),
1.132 misha 971: height(aheight),
972: monospace(amonospace),
973: spacebarspace(aspacebarspace),
1.100 paf 974: ifont(aifont),
1.158 moko 975: alphabet(aalphabet),
976: fsource_charset(asource_charset){
1.132 misha 977:
978: if(fsource_charset.isUTF8()){
979: size_t index=0;
980: for(UTF8_string_iterator i(alphabet); i.has_next(); )
981: fletter2index.put_dont_replace(i.next(), index++);
982: }
1.91 paf 983: }
984:
985: /* ******************************** char ********************************** */
986:
987: size_t Font::index_of(char ch) {
988: if(ch==' ') return STRING_NOT_FOUND;
989: return alphabet.pos(ch);
990: }
991:
1.132 misha 992: size_t Font::index_of(XMLCh ch) {
993: if(ch==' ') return STRING_NOT_FOUND;
994: return fletter2index.get(ch);
995: }
996:
1.91 paf 997: int Font::index_width(size_t index) {
998: if(index==STRING_NOT_FOUND)
999: return spacebarspace;
1000: int tr=ifont->GetTransparent();
1001: for(int x=ifont->SX()-1; x>=0; x--) {
1002: for(int y=0; y<height; y++)
1003: if(ifont->GetPixel(x, Y(y))!=tr)
1004: return x+1;
1.16 paf 1005: }
1.91 paf 1006: return 0;
1007: }
1008:
1.108 paf 1009: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1010: if(index!=STRING_NOT_FOUND)
1.108 paf 1011: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1012: }
1013:
1014: /* ******************************** string ********************************** */
1015:
1016: int Font::step_width(int index) {
1.125 misha 1017: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1018: }
1019:
1020: // counts trailing letter_spacing, consider this OK. useful for contiuations
1021: int Font::string_width(const String& s){
1022: const char* cstr=s.cstr();
1023: int result=0;
1.132 misha 1024:
1025: if(fsource_charset.isUTF8()){
1026: for(UTF8_string_iterator i(s); i.has_next(); )
1027: result+=step_width(index_of(i.next()));
1028: } else {
1029: for(const char* current=cstr; *current; current++)
1030: result+=step_width(index_of(*current));
1031: }
1032:
1.91 paf 1033: return result;
1034: }
1035:
1.108 paf 1036: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1037: const char* cstr=s.cstr();
1.132 misha 1038:
1039: if(fsource_charset.isUTF8()){
1040: for(UTF8_string_iterator i(s); i.has_next(); ){
1041: size_t index=index_of(i.next());
1042: index_display(image, x, y, index);
1043: x+=step_width(index);
1044: }
1045: } else {
1046: for(const char* current=cstr; *current; current++) {
1047: size_t index=index_of(*current);
1048: index_display(image, x, y, index);
1049: x+=step_width(index);
1050: }
1.87 paf 1051: }
1.91 paf 1052: }
1.87 paf 1053:
1.91 paf 1054: //
1.35 parser 1055:
1.16 paf 1056:
1.91 paf 1057: static void _font(Request& r, MethodParams& params) {
1058: const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131 misha 1059: size_t alphabet_length=alphabet.length(r.charsets.source());
1060: if(!alphabet_length)
1.155 moko 1061: throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");
1.84 paf 1062:
1.125 misha 1063: gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1064:
1.126 misha 1065: int spacebar_width=image->SX();
1.125 misha 1066: int monospace_width=0; // proportional
1067: int letter_spacing=1;
1068: if(params.count()>2){
1.144 moko 1069: if(HashStringValue* options=params[2].get_hash()){
1.125 misha 1070: // third option is hash
1071: if(params.count()>3)
1.144 moko 1072: throw Exception(PARSER_RUNTIME, 0, "too many params were specified");
1.125 misha 1073: int valid_options=0;
1074: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1075: valid_options++;
1.153 moko 1076: spacebar_width=r.process(*vspacebar_width).as_int();
1.125 misha 1077: }
1078: if(Value* vmonospace_width=options->get(monospace_width_name)){
1079: valid_options++;
1.153 moko 1080: monospace_width=r.process(*vmonospace_width).as_int();
1.125 misha 1081: if(!monospace_width)
1082: monospace_width=image->SX();
1083: }
1084: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1085: valid_options++;
1.153 moko 1086: letter_spacing=r.process(*vletter_spacing).as_int();
1.125 misha 1087: }
1088: if(valid_options!=options->count())
1.134 misha 1089: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1090: } else {
1091: // backward
1.144 moko 1092: spacebar_width=params.as_int(2, "param must be int or hash", r);
1.125 misha 1093: if(params.count()>3) {
1094: monospace_width=params.as_int(3, "monospace_width must be int", r);
1095: if(!monospace_width)
1096: monospace_width=image->SX();
1097: }
1098: }
1099: }
1100:
1.131 misha 1101: if(int remainder=image->SY() % alphabet_length)
1.155 moko 1102: 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 1103:
1.155 moko 1104: 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 1105: }
1106:
1.91 paf 1107: static void _text(Request& r, MethodParams& params) {
1108: int x=params.as_int(0, "x must be int", r);
1109: int y=params.as_int(1, "y must be int", r);
1110: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1111:
1.91 paf 1112: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1113: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1114: }
1115:
1.91 paf 1116: static void _length(Request& r, MethodParams& params) {
1117: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1118:
1.91 paf 1119: VImage& vimage=GET_SELF(r, VImage);
1.157 moko 1120: r.write(*new VInt(vimage.font().string_width(s)));
1.47 parser 1121: }
1122:
1.91 paf 1123: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1124: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1125:
1.108 paf 1126: image.Arc(
1.91 paf 1127: params.as_int(0, "center_x must be int", r),
1128: params.as_int(1, "center_y must be int", r),
1129: params.as_int(2, "width must be int", r),
1130: params.as_int(3, "height must be int", r),
1131: params.as_int(4, "start degrees must be int", r),
1132: params.as_int(5, "end degrees must be int", r),
1.108 paf 1133: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1134: }
1135:
1.91 paf 1136: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1137: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1138:
1.108 paf 1139: image.Sector(
1.91 paf 1140: params.as_int(0, "center_x must be int", r),
1141: params.as_int(1, "center_y must be int", r),
1142: params.as_int(2, "width must be int", r),
1143: params.as_int(3, "height must be int", r),
1144: params.as_int(4, "start degrees must be int", r),
1145: params.as_int(5, "end degrees must be int", r),
1.108 paf 1146: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1147: }
1148:
1.91 paf 1149: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1150: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1151:
1.91 paf 1152: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1153: image.Arc(
1.91 paf 1154: params.as_int(0, "center_x must be int", r),
1155: params.as_int(1, "center_y must be int", r),
1.50 parser 1156: size, //w
1157: size, //h
1.48 parser 1158: 0, //s
1159: 360, //e
1.108 paf 1160: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1161: }
1162:
1.108 paf 1163: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1164: Value& value=params.as_no_junction(index, msg);
1.53 parser 1165:
1.130 misha 1166: if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108 paf 1167: return static_cast<VImage *>(vimage)->image();
1.75 paf 1168: } else
1.155 moko 1169: throw Exception(PARSER_RUNTIME, 0, msg);
1.53 parser 1170: }
1171:
1.91 paf 1172: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1173: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1174:
1.108 paf 1175: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1176:
1.91 paf 1177: int sx=params.as_int(1, "src_x must be int", r);
1178: int sy=params.as_int(2, "src_y must be int", r);
1179: int sw=params.as_int(3, "src_w must be int", r);
1180: int sh=params.as_int(4, "src_h must be int", r);
1181: int dx=params.as_int(5, "dest_x must be int", r);
1182: int dy=params.as_int(6, "dest_y must be int", r);
1183: if(params.count()>1+2+2+2) {
1184: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1185: int dh=(int)(params.count()>1+2+2+2+1?
1186: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1187: int tolerance=params.count()>1+2+2+2+2?
1188: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1189:
1.108 paf 1190: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1191: } else
1.108 paf 1192: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1193: }
1194:
1195: static void _pixel(Request& r, MethodParams& params) {
1196: gdImage& image=GET_SELF(r, VImage).image();
1197:
1.109 paf 1198: int x=params.as_int(0, "x must be int", r);
1199: int y=params.as_int(1, "y must be int", r);
1200:
1201: if(params.count()>2) {
1202: image.SetPixel(x, y,
1203: image.Color(params.as_int(2, "color must be int", r)));
1204: } else
1.157 moko 1205: r.write(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1206: }
1207:
1208:
1.22 paf 1209: // constructor
1210:
1.91 paf 1211: MImage::MImage(): Methoded("image") {
1.1 paf 1212: // ^image:measure[DATA]
1.22 paf 1213: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 1214:
1.25 paf 1215: // ^image.html[]
1216: // ^image.html[hash]
1.22 paf 1217: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1218:
1.25 paf 1219: // ^image.load[background.gif]
1.22 paf 1220: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1221:
1.25 paf 1222: // ^image.create[width;height] bgcolor=white
1223: // ^image.create[width;height;bgcolor]
1.22 paf 1224: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1225:
1.25 paf 1226: // ^image.gif[]
1.95 paf 1227: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1228:
1.25 paf 1229: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1230: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1231:
1.25 paf 1232: // ^image.fill(x;y;color)
1.22 paf 1233: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1234:
1.25 paf 1235: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1236: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1237:
1.25 paf 1238: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1239: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1240:
1.44 parser 1241: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1242: // ^image.replace(color-source;color-dest)
1243: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1244:
1245: // ^image.polyline(color)[table x:y]
1246: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1247:
1.44 parser 1248: // ^image.polygon(color)[table x:y]
1249: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1250:
1.44 parser 1251: // ^image.polybar(color)[table x:y]
1252: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1253:
1.125 misha 1254: // ^image.font[alPHAbet;font-file-name.gif]
1255: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1256: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1257: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1258: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1259:
1.125 misha 1260: // ^image.text(x;y)[text]
1.22 paf 1261: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1262:
1.125 misha 1263: // ^image.length[text]
1.47 parser 1264: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1265:
1.48 parser 1266: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1267: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1268:
1269: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1270: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1271:
1272: // ^image.circle(center x;center y;r;color)
1273: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1274:
1.56 parser 1275: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1276: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1277:
1278: // ^image.pixel(x;y)[(color)]
1.109 paf 1279: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1280: }
E-mail: