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