Annotation of parser3/src/classes/image.C, revision 1.151
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.150 moko 4: Copyright (c) 2001-2015 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.151 ! moko 28: volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.150 2015/10/26 01:21:54 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.91 paf 241: const String& file_name; const char* fname;
242: int f;
243:
1.77 paf 244: public:
1.91 paf 245: Measure_file_reader(int af, const String& afile_name, const char* afname):
246: file_name(afile_name), fname(afname), 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.118 misha 262: throw Exception(IMAGE_FORMAT,
1.77 paf 263: &file_name,
1.79 paf 264: "seek(value=%ld, whence=%d) failed: %s (%d), actual filename '%s'",
265: value, whence, strerror(errno), errno, fname);
1.77 paf 266: }
267:
1.91 paf 268: override long tell() { return lseek(f, 0, SEEK_CUR); }
1.79 paf 269:
1.77 paf 270: };
271:
272: class Measure_buf_reader: public Measure_reader {
1.91 paf 273:
274: const char* buf; size_t size;
275: const String& file_name;
276:
277: size_t offset;
278:
1.77 paf 279: public:
1.91 paf 280: Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):
1.77 paf 281: buf(abuf), size(asize), file_name(afile_name), offset(0) {
282: }
283:
1.91 paf 284: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 285: size_t to_read=min(limit, size-offset);
1.91 paf 286: abuf=buf+offset;
1.77 paf 287: offset+=to_read;
288: return to_read;
289: }
290:
1.91 paf 291: override void seek(long value, int whence) {
1.79 paf 292: size_t new_offset;
293: switch(whence) {
294: case SEEK_CUR: new_offset=offset+value; break;
295: case SEEK_SET: new_offset=(size_t)value; break;
1.91 paf 296: default:
297: throw Exception(0,
298: 0,
299: "whence #%d not supported", 0, whence);
300: break; // never
1.79 paf 301: }
302:
1.77 paf 303: if((ssize_t)new_offset<0 || new_offset>size)
1.118 misha 304: throw Exception(IMAGE_FORMAT,
1.77 paf 305: &file_name,
1.79 paf 306: "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0",
307: value, whence, new_offset, size);
1.77 paf 308: offset=new_offset;
309: }
310:
1.91 paf 311: override long tell() { return offset; }
1.79 paf 312:
1.1 paf 313: };
1.77 paf 314:
1.43 parser 315: #endif
1.1 paf 316:
1.72 paf 317: /// PNG file header
318: struct PNG_Header {
319: char dummy[12];
320: char signature[4]; //< must be "IHDR"
1.80 paf 321: uchar high_width[2]; //< image width high bytes [we ignore for now]
322: uchar width[2]; //< image width low bytes
323: uchar high_height[2]; //< image height high bytes [we ignore for now]
324: uchar height[4]; //< image height
1.72 paf 325: };
326:
1.21 paf 327: /// GIF file header
1.1 paf 328: struct GIF_Header {
1.72 paf 329: char signature[3]; // 'GIF'
1.1 paf 330: char version[3];
1.80 paf 331: uchar width[2];
332: uchar height[2];
1.1 paf 333: char dif;
334: char fonColor;
335: char nulls;
336: };
337:
1.31 parser 338: /// JPEG record head
339: struct JPG_Segment_head {
1.80 paf 340: uchar marker;
341: uchar code;
342: uchar length[2];
1.1 paf 343: };
1.21 paf 344: /// JPEG frame header
1.31 parser 345: struct JPG_Size_segment_body {
1.27 parser 346: char data; //< data precision of bits/sample
1.80 paf 347: uchar height[2]; //< image height
348: uchar width[2]; //< image width
1.27 parser 349: char numComponents; //< number of color components
1.1 paf 350: };
351:
1.79 paf 352: /// JPEG frame header
1.91 paf 353: struct JPG_Exif_segment_begin {
1.79 paf 354: char signature[6]; // Exif\0\0
355: };
356:
1.80 paf 357: /// JPEG Exif TIFF Header
358: struct JPG_Exif_TIFF_header {
359: uchar byte_align_identifier[2];
360: char dummy[2]; // always 000A [or 0A00]
361: uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
362: };
363:
364: // JPEG Exif IFD start
1.91 paf 365: struct JPG_Exif_IFD_begin {
1.80 paf 366: uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
367: };
368:
369: // TTTT ffff NNNNNNNN DDDDDDDD
370: struct JPG_Exif_IFD_entry {
371: uchar tag[2]; // Tag number, this shows a kind of data
372: uchar format[2]; // data format
373: uchar components_count[4]; // number of components
374: uchar value_or_offset_to_it[4]; // data value or offset to data value
375: };
376:
377: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
378:
1.118 misha 379: #define JPG_IFD_TAG_EXIF_GPS_OFFSET 0x8825
380:
1.89 paf 381: #define JPEG_EXIF_DATE_CHARS 20
382:
1.1 paf 383: //
384:
1.80 paf 385: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
386: return (ushort)((b1<<8) + b0);
1.33 parser 387: }
388:
1.80 paf 389: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
390: return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
1.33 parser 391: }
392:
1.80 paf 393: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
394: return is_big?x_endian_to_ushort(b[1], b[0]):
395: x_endian_to_ushort(b[0], b[1]);
1.1 paf 396: }
397:
1.80 paf 398: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
399: return is_big?x_endian_to_uint(b[3], b[2], b[1], b[0]):
400: x_endian_to_uint(b[0], b[1], b[2], b[3]);
401: }
402:
1.91 paf 403: static void measure_gif(const String& origin_string,
1.78 paf 404: Measure_reader& reader, ushort& width, ushort& height) {
1.1 paf 405:
1.91 paf 406: const char* buf;
1.102 paf 407: const size_t head_size=sizeof(GIF_Header);
408: if(reader.read(buf, head_size)<head_size)
1.118 misha 409: throw Exception(IMAGE_FORMAT,
1.91 paf 410: &origin_string,
1.34 parser 411: "not GIF file - too small");
1.31 parser 412: GIF_Header *head=(GIF_Header *)buf;
1.1 paf 413:
1.72 paf 414: if(strncmp(head->signature, "GIF", 3)!=0)
1.118 misha 415: throw Exception(IMAGE_FORMAT,
1.91 paf 416: &origin_string,
1.44 parser 417: "not GIF file - wrong signature");
1.1 paf 418:
1.80 paf 419: width=endian_to_ushort(false, head->width);
420: height=endian_to_ushort(false, head->height);
421: }
422:
1.91 paf 423: static Value* parse_IFD_entry_formatted_one_value(
1.80 paf 424: bool is_big,
425: ushort format,
426: size_t component_size,
427: const uchar *value) {
428: switch(format) {
429: case 1: // unsigned byte
1.91 paf 430: return new VInt((uchar)value[0]);
1.80 paf 431: case 3: // unsigned short
1.91 paf 432: return new VInt(endian_to_ushort(is_big, value));
1.80 paf 433: case 4: // unsigned long
434: // 'double' because parser's Int is signed
1.91 paf 435: return new VDouble(endian_to_uint(is_big, value));
1.80 paf 436: case 5: // unsigned rational
437: {
438: uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
439: uint denominator=endian_to_uint(is_big, value);
440: if(!denominator)
441: return 0;
1.91 paf 442: return new VDouble(((double)numerator)/denominator);
1.80 paf 443: }
444: case 6: // signed byte
1.91 paf 445: return new VInt((signed char)value[0]);
1.80 paf 446: case 8: // signed short
1.91 paf 447: return new VInt((signed short)endian_to_ushort(is_big, value));
1.80 paf 448: case 9: // signed long
1.91 paf 449: return new VInt((signed int)endian_to_uint(is_big, value));
1.80 paf 450: case 10: // signed rational
451: {
452: signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
453: uint denominator=endian_to_uint(is_big, value);
454: if(!denominator)
455: return 0;
1.91 paf 456: return new VDouble(numerator/denominator);
1.80 paf 457: }
458: /*
459: case 11: // single float
1.120 misha 460: @todo
1.80 paf 461: case 12: // double float
1.120 misha 462: @todo
1.80 paf 463: */
464: };
465:
466: return 0;
467: }
468:
1.89 paf 469: // date.C
1.147 moko 470: tm cstr_to_time_t(char *, const char **);
1.89 paf 471:
1.91 paf 472: static Value* parse_IFD_entry_formatted_value(bool is_big, ushort format,
473: size_t component_size, uint components_count,
474: const uchar *value) {
1.80 paf 475: if(format==2) { // ascii string, exception: the only type with varying size
1.91 paf 476: const char* cstr=(const char* )value;
477: size_t length=components_count;
1.89 paf 478: // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.91 paf 479: if(length==JPEG_EXIF_DATE_CHARS
1.111 paf 480: && isdigit((unsigned char)cstr[0])
1.91 paf 481: && cstr[length-1]==0) {
1.89 paf 482: char cstr_writable[JPEG_EXIF_DATE_CHARS];
483: strcpy(cstr_writable, cstr);
484:
1.106 paf 485: try {
1.147 moko 486: tm tmIn=cstr_to_time_t(cstr_writable, 0);
1.146 moko 487: return new VDate(tmIn);
1.106 paf 488: }
489: catch(...) { /*ignore bad date times*/ }
1.89 paf 490: }
491:
1.123 misha 492: return new VString(*new String(cstr, String::L_TAINTED));
1.80 paf 493: }
494:
495: if(components_count==1)
1.91 paf 496: return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80 paf 497:
1.91 paf 498: VHash* result=new VHash;
499: HashStringValue& hash=result->hash();
1.80 paf 500: for(uint i=0; i<components_count; i++, value+=component_size) {
1.91 paf 501: hash.put(
1.93 paf 502: String::Body::Format(i),
1.91 paf 503: parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80 paf 504: }
505:
1.91 paf 506: return result;
1.80 paf 507: }
508:
1.91 paf 509: static Value* parse_IFD_entry_value(
1.80 paf 510: bool is_big, Measure_reader& reader, long tiff_base,
511: JPG_Exif_IFD_entry& entry) {
512: size_t format2component_size[]={
513: 0, // undefined
514: 1, // unsigned byte
515: 1, // ascii string
516: 2, // unsigned short
517: 4, // unsigned long
518: 8, // unsigned rational
519: 1, // signed byte
520: 0, // undefined
521: 2, // signed short
522: 4, // signed long
523: 8, // signed rational
524: /*
525: 4, // single float
526: 8, // double float
527: */
528: };
529:
530: ushort format=endian_to_ushort(is_big, entry.format);
531: if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
532: return 0; // format out of range, ignoring
533:
534: size_t component_size=format2component_size[format];
535: if(component_size==0)
536: return 0; // undefined format
537:
538: // You can get the total data byte length by multiplies
539: // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
540: uint components_count=endian_to_uint(is_big, entry.components_count);
1.121 misha 541: uint value_size=component_size*components_count;
1.80 paf 542: // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91 paf 543: Value* result;
1.80 paf 544:
545: if(value_size<=4)
1.91 paf 546: result=parse_IFD_entry_formatted_value(
1.80 paf 547: is_big, format,
548: component_size, components_count,
549: entry.value_or_offset_to_it);
550: else {
551: long remembered=reader.tell();
552: {
553: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91 paf 554: const char* value;
1.80 paf 555: if(reader.read(value, value_size)<sizeof(value_size))
556: return 0;
1.91 paf 557: result=parse_IFD_entry_formatted_value(
1.80 paf 558: is_big, format,
559: component_size, components_count,
560: (const uchar*)value);
561: }
562: reader.seek(remembered, SEEK_SET);
563: }
564:
565: return result;
566: }
567:
1.91 paf 568: static void parse_IFD(HashStringValue& hash,
1.118 misha 569: bool is_big, Measure_reader& reader, long tiff_base, bool gps=false);
1.91 paf 570:
571: static void parse_IFD_entry(HashStringValue& hash,
572: bool is_big, Measure_reader& reader, long tiff_base,
1.118 misha 573: JPG_Exif_IFD_entry& entry, bool gps=false) {
1.80 paf 574: ushort tag=endian_to_ushort(is_big, entry.tag);
1.118 misha 575:
576: if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
1.80 paf 577: long remembered=reader.tell();
578: {
579: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.118 misha 580: parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps);
1.80 paf 581: }
582: reader.seek(remembered, SEEK_SET);
583: return;
584: }
1.118 misha 585:
1.91 paf 586: if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.118 misha 587: if(const char* name=(gps)?exif_gps_tag_value2name.get(tag):exif_tag_value2name.get(tag))
1.148 moko 588: hash.put(name, value);
1.80 paf 589: else
1.93 paf 590: hash.put(String::Body::Format(tag), value);
1.80 paf 591: }
1.1 paf 592: }
593:
1.91 paf 594: static void parse_IFD(
595: HashStringValue& hash,
1.118 misha 596: bool is_big, Measure_reader& reader, long tiff_base, bool gps) {
1.91 paf 597: const char* buf;
598: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80 paf 599: return;
1.91 paf 600: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80 paf 601:
602: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
603: for(int i=0; i<directory_entry_count; i++) {
604: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
605: return;
606:
1.118 misha 607: parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf, gps);
1.80 paf 608: }
609: // then goes: LLLLLLLL Offset to next IFD [not going there]
610: }
611:
1.91 paf 612: static Value* parse_exif(Measure_reader& reader, const String& origin_string) {
613: const char* buf;
614: if(reader.read(buf, sizeof(JPG_Exif_segment_begin))<sizeof(JPG_Exif_segment_begin))
1.118 misha 615: throw Exception(IMAGE_FORMAT,
1.91 paf 616: &origin_string,
1.80 paf 617: "not JPEG file - can not fully read Exif segment start");
618:
1.91 paf 619: JPG_Exif_segment_begin *start=(JPG_Exif_segment_begin *)buf;
1.80 paf 620: if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid?
621: return 0; // ignore invalid block
622:
623: uint tiff_base=reader.tell();
624: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
625: return 0;
626:
627: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
628: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
629:
630: uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
631: reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
632:
1.91 paf 633: VHash* vhash=new VHash;
1.80 paf 634:
635: // IFD
1.91 paf 636: parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80 paf 637:
1.91 paf 638: return vhash;
1.80 paf 639: }
640:
1.91 paf 641: static void measure_jpeg(const String& origin_string,
642: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.2 paf 643: // JFIF format markers
1.80 paf 644: const uchar MARKER=0xFF;
645: const uchar CODE_SIZE_A=0xC0;
646: const uchar CODE_SIZE_B=0xC1;
647: const uchar CODE_SIZE_C=0xC2;
648: const uchar CODE_SIZE_D=0xC3;
649: const uchar CODE_EXIF=0xE1;
1.2 paf 650:
1.91 paf 651: const char* buf;
1.18 paf 652: const size_t prefix_size=2;
1.31 parser 653: if(reader.read(buf, prefix_size)<prefix_size)
1.118 misha 654: throw Exception(IMAGE_FORMAT,
1.91 paf 655: &origin_string,
1.34 parser 656: "not JPEG file - too small");
1.80 paf 657: uchar *signature=(uchar *)buf;
1.1 paf 658:
1.31 parser 659: if(!(signature[0]==0xFF && signature[1]==0xD8))
1.118 misha 660: throw Exception(IMAGE_FORMAT,
1.91 paf 661: &origin_string,
1.44 parser 662: "not JPEG file - wrong signature");
1.31 parser 663:
664: while(true) {
1.80 paf 665: uint segment_base=reader.tell()+2/*marker,code*/;
1.31 parser 666: if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79 paf 667: break;
1.31 parser 668: JPG_Segment_head *head=(JPG_Segment_head *)buf;
669:
670: // Verify that it's a valid segment.
671: if(head->marker!=MARKER)
1.118 misha 672: throw Exception(IMAGE_FORMAT,
1.91 paf 673: &origin_string,
1.79 paf 674: "not JPEG file - marker not found");
675:
676: switch(head->code) {
1.118 misha 677: // http://park2.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
1.79 paf 678: case CODE_EXIF:
1.80 paf 679: if(exif && !*exif) // seen .jpg with some xml under EXIF tag, after real exif block :)
1.91 paf 680: *exif=parse_exif(reader, origin_string);
1.1 paf 681: break;
1.31 parser 682:
1.79 paf 683: case CODE_SIZE_A:
684: case CODE_SIZE_B:
685: case CODE_SIZE_C:
686: case CODE_SIZE_D:
687: {
688: // Segments that contain size info
689: if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
1.118 misha 690: throw Exception(IMAGE_FORMAT,
1.91 paf 691: &origin_string,
1.79 paf 692: "not JPEG file - can not fully read Size segment");
693: JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
694:
1.80 paf 695: width=endian_to_ushort(true, body->width);
696: height=endian_to_ushort(true, body->height);
1.79 paf 697: }
1.80 paf 698: return;
1.79 paf 699: };
700:
1.80 paf 701: reader.seek(segment_base+endian_to_ushort(true, head->length), SEEK_SET);
1.31 parser 702: }
703:
1.118 misha 704: throw Exception(IMAGE_FORMAT,
1.91 paf 705: &origin_string,
1.79 paf 706: "broken JPEG file - size frame not found");
1.1 paf 707: }
708:
1.91 paf 709: static void measure_png(const String& origin_string,
1.78 paf 710: Measure_reader& reader, ushort& width, ushort& height) {
1.72 paf 711:
1.91 paf 712: const char* buf;
1.102 paf 713: const size_t head_size=sizeof(PNG_Header);
714: if(reader.read(buf, head_size)<head_size)
1.118 misha 715: throw Exception(IMAGE_FORMAT,
1.91 paf 716: &origin_string,
1.72 paf 717: "not PNG file - too small");
718: PNG_Header *head=(PNG_Header *)buf;
719:
720: if(strncmp(head->signature, "IHDR", 4)!=0)
1.118 misha 721: throw Exception(IMAGE_FORMAT,
1.91 paf 722: &origin_string,
1.72 paf 723: "not PNG file - wrong signature");
724:
1.80 paf 725: width=endian_to_ushort(true, head->width);
726: height=endian_to_ushort(true, head->height);
1.72 paf 727: }
728:
1.1 paf 729: // measure center
730:
1.91 paf 731: static void measure(const String& file_name,
732: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.128 misha 733: const char* file_name_cstr=file_name.taint_cstr(String::L_FILE_SPEC);
1.91 paf 734: if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1 paf 735: cext++;
736: if(strcasecmp(cext, "GIF")==0)
1.91 paf 737: measure_gif(file_name, reader, width, height);
1.1 paf 738: else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.91 paf 739: measure_jpeg(file_name, reader, width, height, exif);
1.72 paf 740: else if(strcasecmp(cext, "PNG")==0)
1.91 paf 741: measure_png(file_name, reader, width, height);
1.1 paf 742: else
1.118 misha 743: throw Exception(IMAGE_FORMAT,
1.1 paf 744: &file_name,
745: "unhandled image file name extension '%s'", cext);
746: } else
1.118 misha 747: throw Exception(IMAGE_FORMAT,
1.1 paf 748: &file_name,
749: "can not determine image type - no file name extension");
750: }
751:
1.77 paf 752: // methods
1.1 paf 753:
1.40 parser 754: #ifndef DOXYGEN
1.77 paf 755: struct File_measure_action_info {
1.91 paf 756: ushort* width;
757: ushort* height;
758: Value** exif;
759: const String* file_name;
1.1 paf 760: };
1.40 parser 761: #endif
1.91 paf 762: static void file_measure_action(
1.98 paf 763: struct stat& /*finfo*/, int f,
764: const String& /*file_spec*/, const char* fname, bool /*as_text*/,
1.77 paf 765: void *context) {
766: File_measure_action_info& info=*static_cast<File_measure_action_info *>(context);
1.1 paf 767:
1.91 paf 768: Measure_file_reader reader(f, *info.file_name, fname);
769: measure(*info.file_name, reader, *info.width, *info.height, info.exif);
1.1 paf 770: }
771:
1.91 paf 772: static void _measure(Request& r, MethodParams& params) {
773: Value& data=params.as_no_junction(0, "data must not be code");
1.1 paf 774:
1.78 paf 775: ushort width=0;
776: ushort height=0;
1.91 paf 777: Value* exif=0;
778: const String* file_name;
1.100 paf 779: if((file_name=data.get_string())) {
780: File_measure_action_info info={
781: &width, &height,
782: &exif,
783: file_name
784: };
1.91 paf 785: file_read_action_under_lock(r.absolute(*file_name),
1.77 paf 786: "measure", file_measure_action, &info);
1.1 paf 787: } else {
1.129 misha 788: VFile* vfile=data.as_vfile(String::L_AS_IS);
1.91 paf 789: file_name=&vfile->fields().get(name_name)->as_string();
1.77 paf 790: Measure_buf_reader reader(
1.91 paf 791: vfile->value_ptr(),
792: vfile->value_size(),
1.77 paf 793: *file_name
794: );
1.91 paf 795: measure(*file_name, reader, width, height, &exif);
1.1 paf 796: }
797:
1.91 paf 798: GET_SELF(r, VImage).set(file_name, width, height, 0, exif);
1.1 paf 799: }
800:
1.40 parser 801: #ifndef DOXYGEN
1.4 paf 802: struct Attrib_info {
1.91 paf 803: String* tag; ///< html tag being constructed
804: HashStringValue* skip; ///< tag attributes not to append to tag string [to skip]
1.4 paf 805: };
1.40 parser 806: #endif
1.91 paf 807: static void append_attrib_pair(
1.133 misha 808: HashStringValue::key_type key,
809: HashStringValue::value_type value,
810: Attrib_info* info) {
811: // skip user-specified, internal(starting with "line-") attributes and border attribute with empty value
812: if(
813: (info->skip && info->skip->get(key))
814: || key.pos("line-")==0
815: || (key=="border" && !value->is_defined())
816: )
1.4 paf 817: return;
818:
1.133 misha 819: // src="a.gif" width="123" ismap[=-1]
1.91 paf 820: *info->tag << " " << key;
821: if(value->is_string() || value->as_int()>=0)
822: *info->tag << "=\"" << value->as_string() << "\"";
1.3 paf 823: }
1.91 paf 824: static void _html(Request& r, MethodParams& params) {
1.3 paf 825:
1.91 paf 826: String tag;
1.3 paf 827: tag << "<img";
1.4 paf 828:
1.91 paf 829: const HashStringValue& fields=GET_SELF(r, VImage).fields();
830: HashStringValue* attribs=0;
1.4 paf 831:
1.91 paf 832: if(params.count()) {
1.69 paf 833: // for backward compatibility: someday was ^html{}
1.133 misha 834: Value& vattribs=r.process_to_value(params[0], false/*don't intercept string*/);
1.145 moko 835: if(!vattribs.is_string()) { // allow empty
1.100 paf 836: if((attribs=vattribs.get_hash())) {
837: Attrib_info info={&tag, 0};
1.114 paf 838: attribs->for_each<Attrib_info*>(append_attrib_pair, &info);
1.39 parser 839: } else
1.115 misha 840: throw Exception(PARSER_RUNTIME,
1.91 paf 841: 0,
1.39 parser 842: "attributes must be hash");
1.145 moko 843: }
1.39 parser 844: }
1.4 paf 845:
1.91 paf 846: {
847: Attrib_info info={&tag, attribs};
1.114 paf 848: fields.for_each<Attrib_info*>(append_attrib_pair, &info);
1.91 paf 849: }
1.6 paf 850: tag << " />";
1.3 paf 851: r.write_pass_lang(tag);
852: }
1.8 paf 853:
1.68 paf 854: /// @test wrap FILE to auto-object
1.91 paf 855: static gdImage* load(Request& r,
1.16 paf 856: const String& file_name){
1.128 misha 857: const char* file_name_cstr=r.absolute(file_name).taint_cstr(String::L_FILE_SPEC);
1.16 paf 858: if(FILE *f=fopen(file_name_cstr, "rb")) {
1.91 paf 859: gdImage* image=new gdImage;
860: bool ok=image->CreateFromGif(f);
1.16 paf 861: fclose(f);
1.23 paf 862: if(!ok)
1.118 misha 863: throw Exception(IMAGE_FORMAT,
1.23 paf 864: &file_name,
865: "is not in GIF format");
1.91 paf 866: return image;
1.16 paf 867: } else {
1.68 paf 868: throw Exception("file.missing",
1.91 paf 869: 0,
1.16 paf 870: "can not open '%s'", file_name_cstr);
871: }
872: }
873:
874:
1.91 paf 875: static void _load(Request& r, MethodParams& params) {
1.116 misha 876: const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.6 paf 877:
1.91 paf 878: gdImage* image=load(r, file_name);
879: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 880: }
881:
1.91 paf 882: static void _create(Request& r, MethodParams& params) {
883: int width=params.as_int(0, "width must be int", r);
884: int height=params.as_int(1, "height must be int", r);
1.8 paf 885: int bgcolor_value=0xffFFff;
1.91 paf 886: if(params.count()>2)
887: bgcolor_value=params.as_int(2, "color must be int", r);
888: gdImage* image=new gdImage;
889: image->Create(width, height);
890: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
891: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 892: }
893:
1.91 paf 894: static void _gif(Request& r, MethodParams& params) {
1.108 paf 895: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 896:
1.138 misha 897: const String *file_name=params.count()>0?¶ms.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10 paf 898:
1.108 paf 899: gdBuf buf=image.Gif();
1.6 paf 900:
1.91 paf 901: VFile& vfile=*new VFile;
1.137 misha 902:
1.142 moko 903: vfile.set_binary(false/*not tainted*/, (const char *)buf.ptr, buf.size, file_name, new VString(*new String("image/gif")));
1.137 misha 904:
1.6 paf 905: r.write_no_lang(vfile);
906: }
907:
1.91 paf 908: static void _line(Request& r, MethodParams& params) {
1.108 paf 909: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 910:
1.108 paf 911: image.Line(
1.91 paf 912: params.as_int(0, "x0 must be int", r),
913: params.as_int(1, "y0 must be int", r),
914: params.as_int(2, "x1 must be int", r),
915: params.as_int(3, "y1 must be int", r),
1.108 paf 916: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 917: }
918:
1.91 paf 919: static void _fill(Request& r, MethodParams& params) {
1.108 paf 920: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 921:
1.108 paf 922: image.Fill(
1.91 paf 923: params.as_int(0, "x must be int", r),
924: params.as_int(1, "y must be int", r),
1.108 paf 925: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 926: }
927:
1.91 paf 928: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 929: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 930:
1.108 paf 931: image.Rectangle(
1.91 paf 932: params.as_int(0, "x0 must be int", r),
933: params.as_int(1, "y0 must be int", r),
934: params.as_int(2, "x1 must be int", r),
935: params.as_int(3, "y1 must be int", r),
1.108 paf 936: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 937: }
938:
1.91 paf 939: static void _bar(Request& r, MethodParams& params) {
1.108 paf 940: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 941:
1.108 paf 942: image.FilledRectangle(
1.91 paf 943: params.as_int(0, "x0 must be int", r),
944: params.as_int(1, "y0 must be int", r),
945: params.as_int(2, "x1 must be int", r),
946: params.as_int(3, "y1 must be int", r),
1.108 paf 947: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 948: }
949:
1.44 parser 950: #ifndef DOXYGEN
1.91 paf 951: static void add_point(Table::element_type row,
952: gdImage::Point **p) {
1.110 paf 953: if(row->count()!=2)
954: throw Exception(0,
955: 0,
956: "coordinates table must contain two columns: x and y values");
1.91 paf 957: (**p).x=row->get(0)->as_int();
958: (**p).y=row->get(1)->as_int();
1.44 parser 959: (*p)++;
960: }
961: #endif
1.135 misha 962: #ifndef DOXYGEN
963: static void add_point(int x, int y,
964: gdImage::Point **p) {
965: (**p).x=x;
966: (**p).y=y;
967: (*p)++;
968: }
969: #endif
1.91 paf 970: static void _replace(Request& r, MethodParams& params) {
1.135 misha 971: int src_color=params.as_int(0, "src color must be int", r);
972: int dest_color=params.as_int(1, "dest color must be int", r);
973:
1.108 paf 974: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 975:
1.135 misha 976: gdImage::Point* all_p=0;
977: size_t count=0;
978: if(params.count() == 3){
1.140 misha 979: Table* table=params.as_table(2, "coordinates");
1.135 misha 980: count=table->count();
981: all_p=new(PointerFreeGC) gdImage::Point[count];
982: gdImage::Point* add_p=all_p;
983: table->for_each(add_point, &add_p);
984: } else {
985: int max_x=image.SX()-1;
986: int max_y=image.SY()-1;
987: if(max_x > 0 && max_y > 0){
988: count=4;
989: all_p=new(PointerFreeGC) gdImage::Point[count];
990: gdImage::Point* add_p=all_p;
991: add_point(0, 0, &add_p);
992: add_point(max_x, 0, &add_p);
993: add_point(max_x, max_y, &add_p);
994: add_point(0, max_y, &add_p);
995: }
996: }
1.13 paf 997:
1.135 misha 998: if(count)
999: image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13 paf 1000: }
1001:
1.91 paf 1002: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 1003: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1004:
1.140 misha 1005: Table* table=params.as_table(1, "coordinates");
1.44 parser 1006:
1.92 paf 1007: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1008: gdImage::Point *add_p=all_p;
1009: table->for_each(add_point, &add_p);
1.108 paf 1010: image.Polygon(all_p, table->count(),
1011: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 1012: false/*not closed*/);
1013: }
1014:
1.91 paf 1015: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 1016: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1017:
1.140 misha 1018: Table* table=(Table*)params.as_table(1, "coordinates");
1.44 parser 1019:
1.92 paf 1020: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1021: gdImage::Point *add_p=all_p;
1022: table->for_each(add_point, &add_p);
1.108 paf 1023: image.Polygon(all_p, table->count(),
1024: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 1025: }
1026:
1.91 paf 1027: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 1028: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 1029:
1.140 misha 1030: Table* table=(Table*)params.as_table(1, "coordinates");
1.13 paf 1031:
1.92 paf 1032: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 1033: gdImage::Point *add_p=all_p;
1034: table->for_each(add_point, &add_p);
1.108 paf 1035: image.FilledPolygon(all_p, table->count(),
1036: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 1037: }
1038:
1.16 paf 1039: // font
1040:
1.85 paf 1041: #define Y(y)(y+index*height)
1.21 paf 1042:
1.91 paf 1043: // Font class
1044:
1.132 misha 1045: Font::Font(
1046: Charset& asource_charset,
1.91 paf 1047: const String& aalphabet,
1.125 misha 1048: gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.132 misha 1049: fsource_charset(asource_charset),
1050: height(aheight),
1051: monospace(amonospace),
1052: spacebarspace(aspacebarspace),
1.125 misha 1053: letterspacing(aletterspacing),
1.100 paf 1054: ifont(aifont),
1055: alphabet(aalphabet) {
1.132 misha 1056:
1057: if(fsource_charset.isUTF8()){
1058: size_t index=0;
1059: for(UTF8_string_iterator i(alphabet); i.has_next(); )
1060: fletter2index.put_dont_replace(i.next(), index++);
1061: }
1.91 paf 1062: }
1063:
1064: /* ******************************** char ********************************** */
1065:
1066: size_t Font::index_of(char ch) {
1067: if(ch==' ') return STRING_NOT_FOUND;
1068: return alphabet.pos(ch);
1069: }
1070:
1.132 misha 1071: size_t Font::index_of(XMLCh ch) {
1072: if(ch==' ') return STRING_NOT_FOUND;
1073: return fletter2index.get(ch);
1074: }
1075:
1.91 paf 1076: int Font::index_width(size_t index) {
1077: if(index==STRING_NOT_FOUND)
1078: return spacebarspace;
1079: int tr=ifont->GetTransparent();
1080: for(int x=ifont->SX()-1; x>=0; x--) {
1081: for(int y=0; y<height; y++)
1082: if(ifont->GetPixel(x, Y(y))!=tr)
1083: return x+1;
1.16 paf 1084: }
1.91 paf 1085: return 0;
1086: }
1087:
1.108 paf 1088: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 1089: if(index!=STRING_NOT_FOUND)
1.108 paf 1090: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 1091: }
1092:
1093: /* ******************************** string ********************************** */
1094:
1095: int Font::step_width(int index) {
1.125 misha 1096: return letterspacing + (monospace ? monospace : index_width(index));
1.91 paf 1097: }
1098:
1099: // counts trailing letter_spacing, consider this OK. useful for contiuations
1100: int Font::string_width(const String& s){
1101: const char* cstr=s.cstr();
1102: int result=0;
1.132 misha 1103:
1104: if(fsource_charset.isUTF8()){
1105: for(UTF8_string_iterator i(s); i.has_next(); )
1106: result+=step_width(index_of(i.next()));
1107: } else {
1108: for(const char* current=cstr; *current; current++)
1109: result+=step_width(index_of(*current));
1110: }
1111:
1.91 paf 1112: return result;
1113: }
1114:
1.108 paf 1115: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1116: const char* cstr=s.cstr();
1.132 misha 1117:
1118: if(fsource_charset.isUTF8()){
1119: for(UTF8_string_iterator i(s); i.has_next(); ){
1120: size_t index=index_of(i.next());
1121: index_display(image, x, y, index);
1122: x+=step_width(index);
1123: }
1124: } else {
1125: for(const char* current=cstr; *current; current++) {
1126: size_t index=index_of(*current);
1127: index_display(image, x, y, index);
1128: x+=step_width(index);
1129: }
1.87 paf 1130: }
1.91 paf 1131: }
1.87 paf 1132:
1.91 paf 1133: //
1.35 parser 1134:
1.16 paf 1135:
1.91 paf 1136: static void _font(Request& r, MethodParams& params) {
1137: const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131 misha 1138: size_t alphabet_length=alphabet.length(r.charsets.source());
1139: if(!alphabet_length)
1.115 misha 1140: throw Exception(PARSER_RUNTIME,
1.91 paf 1141: 0,
1.37 parser 1142: "alphabet must not be empty");
1.84 paf 1143:
1.125 misha 1144: gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
1145:
1.126 misha 1146: int spacebar_width=image->SX();
1.125 misha 1147: int monospace_width=0; // proportional
1148: int letter_spacing=1;
1149: if(params.count()>2){
1.144 moko 1150: if(HashStringValue* options=params[2].get_hash()){
1.125 misha 1151: // third option is hash
1152: if(params.count()>3)
1.144 moko 1153: throw Exception(PARSER_RUNTIME, 0, "too many params were specified");
1.125 misha 1154: int valid_options=0;
1155: if(Value* vspacebar_width=options->get(spacebar_width_name)){
1156: valid_options++;
1157: spacebar_width=r.process_to_value(*vspacebar_width).as_int();
1158: }
1159: if(Value* vmonospace_width=options->get(monospace_width_name)){
1160: valid_options++;
1161: monospace_width=r.process_to_value(*vmonospace_width).as_int();
1162: if(!monospace_width)
1163: monospace_width=image->SX();
1164: }
1165: if(Value* vletter_spacing=options->get(letter_spacing_name)){
1166: valid_options++;
1167: letter_spacing=r.process_to_value(*vletter_spacing).as_int();
1168: }
1169: if(valid_options!=options->count())
1.134 misha 1170: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125 misha 1171: } else {
1172: // backward
1.144 moko 1173: spacebar_width=params.as_int(2, "param must be int or hash", r);
1.125 misha 1174: if(params.count()>3) {
1175: monospace_width=params.as_int(3, "monospace_width must be int", r);
1176: if(!monospace_width)
1177: monospace_width=image->SX();
1178: }
1179: }
1180: }
1181:
1.131 misha 1182: if(int remainder=image->SY() % alphabet_length)
1.115 misha 1183: throw Exception(PARSER_RUNTIME,
1.91 paf 1184: 0,
1.84 paf 1185: "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
1.131 misha 1186: image->SY(), alphabet_length, remainder);
1.37 parser 1187:
1.108 paf 1188: GET_SELF(r, VImage).set_font(new Font(
1.132 misha 1189: r.charsets.source(),
1.37 parser 1190: alphabet,
1.36 parser 1191: image,
1.131 misha 1192: image->SY() / alphabet_length, monospace_width, spacebar_width, letter_spacing));
1.16 paf 1193: }
1194:
1.91 paf 1195: static void _text(Request& r, MethodParams& params) {
1196: int x=params.as_int(0, "x must be int", r);
1197: int y=params.as_int(1, "y must be int", r);
1198: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1199:
1.91 paf 1200: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1201: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1202: }
1203:
1.91 paf 1204: static void _length(Request& r, MethodParams& params) {
1205: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1206:
1.91 paf 1207: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1208: r.write_no_lang(*new VInt(vimage.font().string_width(s)));
1.47 parser 1209: }
1210:
1.91 paf 1211: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1212: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1213:
1.108 paf 1214: image.Arc(
1.91 paf 1215: params.as_int(0, "center_x must be int", r),
1216: params.as_int(1, "center_y must be int", r),
1217: params.as_int(2, "width must be int", r),
1218: params.as_int(3, "height must be int", r),
1219: params.as_int(4, "start degrees must be int", r),
1220: params.as_int(5, "end degrees must be int", r),
1.108 paf 1221: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1222: }
1223:
1.91 paf 1224: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1225: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1226:
1.108 paf 1227: image.Sector(
1.91 paf 1228: params.as_int(0, "center_x must be int", r),
1229: params.as_int(1, "center_y must be int", r),
1230: params.as_int(2, "width must be int", r),
1231: params.as_int(3, "height must be int", r),
1232: params.as_int(4, "start degrees must be int", r),
1233: params.as_int(5, "end degrees must be int", r),
1.108 paf 1234: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1235: }
1236:
1.91 paf 1237: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1238: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1239:
1.91 paf 1240: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1241: image.Arc(
1.91 paf 1242: params.as_int(0, "center_x must be int", r),
1243: params.as_int(1, "center_y must be int", r),
1.50 parser 1244: size, //w
1245: size, //h
1.48 parser 1246: 0, //s
1247: 360, //e
1.108 paf 1248: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1249: }
1250:
1.108 paf 1251: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1252: Value& value=params.as_no_junction(index, msg);
1.53 parser 1253:
1.130 misha 1254: if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108 paf 1255: return static_cast<VImage *>(vimage)->image();
1.75 paf 1256: } else
1.115 misha 1257: throw Exception(PARSER_RUNTIME,
1.91 paf 1258: 0,
1.53 parser 1259: msg);
1260: }
1261:
1.91 paf 1262: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1263: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1264:
1.108 paf 1265: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1266:
1.91 paf 1267: int sx=params.as_int(1, "src_x must be int", r);
1268: int sy=params.as_int(2, "src_y must be int", r);
1269: int sw=params.as_int(3, "src_w must be int", r);
1270: int sh=params.as_int(4, "src_h must be int", r);
1271: int dx=params.as_int(5, "dest_x must be int", r);
1272: int dy=params.as_int(6, "dest_y must be int", r);
1273: if(params.count()>1+2+2+2) {
1274: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1275: int dh=(int)(params.count()>1+2+2+2+1?
1276: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1277: int tolerance=params.count()>1+2+2+2+2?
1278: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1279:
1.108 paf 1280: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1281: } else
1.108 paf 1282: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1283: }
1284:
1285: static void _pixel(Request& r, MethodParams& params) {
1286: gdImage& image=GET_SELF(r, VImage).image();
1287:
1.109 paf 1288: int x=params.as_int(0, "x must be int", r);
1289: int y=params.as_int(1, "y must be int", r);
1290:
1291: if(params.count()>2) {
1292: image.SetPixel(x, y,
1293: image.Color(params.as_int(2, "color must be int", r)));
1294: } else
1295: r.write_no_lang(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1296: }
1297:
1298:
1.22 paf 1299: // constructor
1300:
1.91 paf 1301: MImage::MImage(): Methoded("image") {
1.1 paf 1302: // ^image:measure[DATA]
1.22 paf 1303: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 1304:
1.25 paf 1305: // ^image.html[]
1306: // ^image.html[hash]
1.22 paf 1307: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1308:
1.25 paf 1309: // ^image.load[background.gif]
1.22 paf 1310: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1311:
1.25 paf 1312: // ^image.create[width;height] bgcolor=white
1313: // ^image.create[width;height;bgcolor]
1.22 paf 1314: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1315:
1.25 paf 1316: // ^image.gif[]
1.95 paf 1317: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1318:
1.25 paf 1319: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1320: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1321:
1.25 paf 1322: // ^image.fill(x;y;color)
1.22 paf 1323: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1324:
1.25 paf 1325: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1326: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1327:
1.25 paf 1328: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1329: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1330:
1.44 parser 1331: // ^image.replace(color-source;color-dest)[table x:y]
1.135 misha 1332: // ^image.replace(color-source;color-dest)
1333: add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44 parser 1334:
1335: // ^image.polyline(color)[table x:y]
1336: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1337:
1.44 parser 1338: // ^image.polygon(color)[table x:y]
1339: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1340:
1.44 parser 1341: // ^image.polybar(color)[table x:y]
1342: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1343:
1.125 misha 1344: // ^image.font[alPHAbet;font-file-name.gif]
1345: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1346: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
1347: // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
1348: add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16 paf 1349:
1.125 misha 1350: // ^image.text(x;y)[text]
1.22 paf 1351: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1352:
1.125 misha 1353: // ^image.length[text]
1.47 parser 1354: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1355:
1.48 parser 1356: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1357: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1358:
1359: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1360: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1361:
1362: // ^image.circle(center x;center y;r;color)
1363: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1364:
1.56 parser 1365: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1366: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1367:
1368: // ^image.pixel(x;y)[(color)]
1.109 paf 1369: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1370: }
E-mail: