--- parser3/src/classes/image.C 2002/04/10 09:53:14 1.69 +++ parser3/src/classes/image.C 2002/05/16 16:18:19 1.72 @@ -4,7 +4,7 @@ Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - $Id: image.C,v 1.69 2002/04/10 09:53:14 paf Exp $ + $Id: image.C,v 1.72 2002/05/16 16:18:19 paf Exp $ */ /* @@ -25,10 +25,6 @@ #include "pa_vfile.h" #include "pa_vimage.h" -// defines - -#define IMAGE_CLASS_NAME "image" - // class class MImage : public Methoded { @@ -83,9 +79,19 @@ private: }; #endif +/// PNG file header +struct PNG_Header { + char dummy[12]; + char signature[4]; //< must be "IHDR" + unsigned char high_width[2]; //< image width high bytes [we ignore for now] + unsigned char width[2]; //< image width low bytes + unsigned char high_height[2]; //< image height high bytes [we ignore for now] + unsigned char height[4]; //< image height +}; + /// GIF file header struct GIF_Header { - char type[3]; // 'GIF' + char signature[3]; // 'GIF' char version[3]; unsigned char width[2]; unsigned char height[2]; @@ -133,7 +139,7 @@ void measure_gif(Pool& pool, const Strin "not GIF file - too small"); GIF_Header *head=(GIF_Header *)buf; - if(strncmp(head->type, "GIF", 3)!=0) + if(strncmp(head->signature, "GIF", 3)!=0) throw Exception("image.format", origin_string, "not GIF file - wrong signature"); @@ -199,6 +205,26 @@ void measure_jpeg(Pool& pool, const Stri "broken JPEG file - size frame not found"); } +void measure_png(Pool& pool, const String *origin_string, + Measure_reader& reader, int& width, int& height) { + + void *buf; + const int head_size=sizeof(PNG_Header); + if(reader.read(buf, head_size)signature, "IHDR", 4)!=0) + throw Exception("image.format", + origin_string, + "not PNG file - wrong signature"); + + width=big_endian_to_int(head->width); + height=big_endian_to_int(head->height); +} + // measure center void measure(Pool& pool, const String& file_name, @@ -209,6 +235,8 @@ void measure(Pool& pool, const String& f measure_gif(pool, &file_name, reader, width, height); else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) measure_jpeg(pool, &file_name, reader, width, height); + else if(strcasecmp(cext, "PNG")==0) + measure_png(pool, &file_name, reader, width, height); else throw Exception("image.format", &file_name, @@ -316,7 +344,7 @@ static void _html(Request& r, const Stri if(params->size()) { // for backward compatibility: someday was ^html{} Value &vattribs=r.process_to_value(params->get(0), - 0/*no name*/, + /*0/*no name* /,*/ false/*don't intercept string*/); if(vattribs.is_defined()) // allow 'void' if(attribs=vattribs.get_hash(&method_name)) { @@ -693,9 +721,7 @@ static void _length(Request& r, const St VImage& vimage=*static_cast(r.self); if(vimage.image) if(vimage.font) { - VInt& result=*new(pool) VInt(pool, vimage.font->string_width(s)); - result.set_name(method_name); - r.write_assign_lang(result); + r.write_no_lang(*new(pool) VInt(pool, vimage.font->string_width(s))); } else throw Exception("parser.runtime", &method_name, @@ -814,10 +840,7 @@ static void _copy(Request& r, const Stri // constructor -MImage::MImage(Pool& apool) : Methoded(apool) { - set_name(*NEW String(pool(), IMAGE_CLASS_NAME)); - - +MImage::MImage(Pool& apool) : Methoded(apool, "image") { // ^image:measure[DATA] add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);