--- parser3/src/classes/image.C 2003/03/04 12:38:15 1.90.2.13 +++ parser3/src/classes/image.C 2003/03/18 16:00:06 1.90.2.15.2.2 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_IMAGE_C="$Date: 2003/03/04 12:38:15 $"; +static const char* IDENT_IMAGE_C="$Date: 2003/03/18 16:00:06 $"; /* jpegsize: gets the width and height (in pixels) of a jpeg file @@ -55,7 +55,7 @@ public: EXIF_tag_value2name() { // image JPEG Exif #define EXIF_TAG(tag, name) \ - exif_tag_value2name.put(tag, #name); + put(tag, #name); // Tags used by IFD0 (main image) EXIF_TAG(0x010e, ImageDescription); EXIF_TAG(0x010f, Make); @@ -162,20 +162,20 @@ public: class Measure_file_reader: public Measure_reader { - Pool& pool; - StringPtr file_name; const char* fname; + ; + const String& file_name; const char* fname; int f; public: - Measure_file_reader(Pool& apool, int af, StringPtr afile_name, const char* afname): - pool(apool), file_name(afile_name), fname(afname), f(af) { + Measure_file_reader(int af, const String& afile_name, const char* afname): + pool(), file_name(afile_name), fname(afname), f(af) { } override size_t read(const char *&abuf, size_t limit) { if(limit==0) return 0; - char* lbuf=new(pool) char[limit]; + char* lbuf=new char[limit]; size_t read_size=(size_t)::read(f, lbuf, limit); abuf=lbuf; if(ssize_t(read_size)<0 || read_size>limit) throw Exception(0, @@ -201,12 +201,12 @@ public: class Measure_buf_reader: public Measure_reader { const char* buf; size_t size; - StringPtr file_name; + const String& file_name; size_t offset; public: - Measure_buf_reader(const char* abuf, size_t asize, StringPtr afile_name): + Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name): buf(abuf), size(asize), file_name(afile_name), offset(0) { } @@ -327,7 +327,7 @@ inline uint endian_to_uint(bool is_big, x_endian_to_uint(b[0], b[1], b[2], b[3]); } -static void measure_gif(Pool& pool, StringPtr origin_string, +static void measure_gifconst String* origin_string, Measure_reader& reader, ushort& width, ushort& height) { const char *buf; @@ -347,7 +347,7 @@ static void measure_gif(Pool& pool, Stri height=endian_to_ushort(false, head->height); } -static ValuePtr parse_IFD_entry_formatted_one_value(Pool& pool, +static ValuePtr parse_IFD_entry_formatted_one_value( bool is_big, ushort format, size_t component_size, @@ -394,9 +394,9 @@ static ValuePtr parse_IFD_entry_formatte } // date.C -time_t cstr_to_time_t(char *cstr, StringPtr report_error_origin); +time_t cstr_to_time_t(char *cstr, const String& report_error_origin); -static ValuePtr parse_IFD_entry_formatted_value(Pool& pool, +static ValuePtr parse_IFD_entry_formatted_value( bool is_big, ushort format, size_t component_size, uint components_count, const uchar *value) { @@ -411,14 +411,14 @@ static ValuePtr parse_IFD_entry_formatte strcpy(cstr_writable, cstr); time_t t=cstr_to_time_t(cstr_writable, - StringPtr(0)/* do not throw exception, just return bad result */); + 0/* do not throw exception, just return bad result */); if(t>=0) return ValuePtr(new VDate(t)); } if(const char* premature_zero_pos=(const char* )memchr(cstr, 0, size)) size=premature_zero_pos-cstr; - return ValuePtr(new VString(StringPtr(new String(cstr, size, true/*tainted*/)))); + return ValuePtr(new VString(String* (new String(cstr, size, true/*tainted*/)))); } if(components_count==1) @@ -427,8 +427,8 @@ static ValuePtr parse_IFD_entry_formatte VHashPtr result(new VHash); HashStringValue& hash=result->hash(Exception::undefined_source); for(uint i=0; i=sizeof(format2component_size)/sizeof(format2component_size[0])) - return 0; // format out of range, ignoring + return ValuePtr(0); // format out of range, ignoring size_t component_size=format2component_size[format]; if(component_size==0) - return 0; // undefined format + return ValuePtr(0); // undefined format // You can get the total data byte length by multiplies // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area @@ -483,7 +483,7 @@ static ValuePtr parse_IFD_entry_value(Po reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET); const char *value; if(reader.read(value, value_size)signature, "Exif\0\0", 4+2)!=0) //signature invalid? - return 0; // ignore invalid block + return ValuePtr(0); // ignore invalid block uint tiff_base=reader.tell(); if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel @@ -572,7 +572,7 @@ static ValuePtr parse_exif(Pool& pool, return vhash; } -static void measure_jpeg(Pool& pool, StringPtr origin_string, +static void measure_jpegconst String* origin_string, Measure_reader& reader, ushort& width, ushort& height, ValuePtr* exif) { // JFIF format markers const uchar MARKER=0xFF; @@ -640,7 +640,7 @@ static void measure_jpeg(Pool& pool, Str "broken JPEG file - size frame not found"); } -static void measure_png(Pool& pool, StringPtr origin_string, +static void measure_pngconst String* origin_string, Measure_reader& reader, ushort& width, ushort& height) { const char *buf; @@ -662,9 +662,9 @@ static void measure_png(Pool& pool, Stri // measure center -static void measure(Pool& pool, StringPtr file_name, +static void measureconst String& file_name, Measure_reader& reader, ushort& width, ushort& height, ValuePtr* exif) { - CharPtr file_name_cstr=file_name->cstr(String::UL_FILE_SPEC); + const char* file_name_cstr=file_name->cstr(String::UL_FILE_SPEC); if(const char* cext=strrchr(file_name_cstr, '.')) { cext++; if(strcasecmp(cext, "GIF")==0) @@ -690,12 +690,12 @@ struct File_measure_action_info { ushort* width; ushort* height; ValuePtr* exif; - StringPtr file_name; + const String& file_name; }; #endif -static void file_measure_action(Pool& pool, +static void file_measure_action( struct stat& finfo, int f, - StringPtr file_spec, const char* fname, bool as_text, + const String& file_spec, const char* fname, bool as_text, void *context) { File_measure_action_info& info=*static_cast(context); @@ -703,15 +703,14 @@ static void file_measure_action(Pool& po measure(pool, info.file_name, reader, *info.width, *info.height, info.exif); } -static void _measure(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _measure(Request& r, const String& method_name, MethodParams* params) { ValuePtr data=params->as_no_junction(0, "data must not be code"); ushort width=0; ushort height=0; ValuePtr exif(0); - StringPtr file_name; + const String& file_name; if(file_name=data->get_string(&pool)) { File_measure_action_info info; info.width=&width; @@ -721,7 +720,7 @@ static void _measure(Request& r, StringP file_read_action_under_lock(pool, r.absolute(file_name), "measure", file_measure_action, &info); } else { - VFilePtr vfile=data->as_vfile(pool); + VFilePtr vfile=data->as_vfile(); file_name=vfile->fields().get(name_name)->as_string(&pool); Measure_buf_reader reader( vfile->value_ptr(), @@ -736,7 +735,7 @@ static void _measure(Request& r, StringP #ifndef DOXYGEN struct Attrib_info { - Pool* pool; + ; String* tag; ///< html tag being constructed HashStringValue* skip; ///< tag attributes not to append to tag string [to skip] }; @@ -754,8 +753,7 @@ static void append_attrib_pair( if(value->is_string() || value->as_int()>=0) *info->tag << "=\"" << value->as_string(info->pool) << "\""; } -static void _html(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _html(Request& r, const String& method_name, MethodParams* params) { String tag; tag << "cstr(String::UL_FILE_SPEC); + const char* file_name_cstr=r.absolute(file_name)->cstr(String::UL_FILE_SPEC); if(FILE *f=fopen(file_name_cstr, "rb")) { gdImagePtr image(new gdImage); bool ok=image->CreateFromGif(f); @@ -804,23 +801,21 @@ static gdImagePtr load(Request& r, Strin } else { throw Exception("file.missing", method_name, - "can not open '%s'", file_name_cstr); + "can not open '%s'", file_name_cstr.get()); return gdImagePtr(0); } } -static void _load(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _load(Request& r, const String& method_name, MethodParams* params) { - StringPtr file_name=params->as_string(0, "file name must not be code"); + const String& file_name=params->as_string(0, "file name must not be code"); gdImagePtr image=load(r, method_name, file_name); GET_SELF(r, VImage).set(file_name, image->SX(), image->SY(), image); } -static void _create(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _create(Request& r, const String& method_name, MethodParams* params) { int width=params->as_int(0, "width must be int", r); int height=params->as_int(1, "height must be int", r); @@ -830,11 +825,10 @@ static void _create(Request& r, StringPt gdImagePtr image(new gdImage); image->Create(width, height); image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value)); - GET_SELF(r, VImage).set(StringPtr(0), width, height, image); + GET_SELF(r, VImage).set(0, width, height, image); } -static void _gif(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _gif(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -847,15 +841,14 @@ static void _gif(Request& r, StringPtr m String out; image->Gif(pool, out); VFilePtr vfile(new VFile); - ValuePtr content_type(new VString(StringPtr(new String("image/gif")))); + ValuePtr content_type(new VString(String* (new String("image/gif")))); vfile->set(pool, false/*not tainted*/, - out.cstr(pool), out.size(), 0, content_type); + out.cstr(), out.size(), 0, content_type); r.write_no_lang(vfile); } -static void _line(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _line(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -871,8 +864,7 @@ static void _line(Request& r, StringPtr image->Color(params->as_int(4, "color must be int", r))); } -static void _fill(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _fill(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -886,8 +878,7 @@ static void _fill(Request& r, StringPtr image->Color(params->as_int(2, "color must be int", r))); } -static void _rectangle(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _rectangle(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -903,8 +894,7 @@ static void _rectangle(Request& r, Strin image->Color(params->as_int(4, "color must be int", r))); } -static void _bar(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _bar(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -928,8 +918,7 @@ static void add_point(Table::element_typ (*p)++; } #endif -static void _replace(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _replace(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -943,7 +932,7 @@ static void _replace(Request& r, StringP method_name, "coordinates must be table"); - gdImage::Point *all_p=new(pool) gdImage::Point[table->count()]; + gdImage::Point *all_p=new gdImage::Point[table->count()]; gdImage::Point *add_p=all_p; table->for_each(add_point, &add_p); image->FilledPolygonReplaceColor(all_p, table->count(), @@ -951,8 +940,7 @@ static void _replace(Request& r, StringP image->Color(params->as_int(1, "dest color must be int", r))); } -static void _polyline(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _polyline(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -966,7 +954,7 @@ static void _polyline(Request& r, String method_name, "coordinates must be table"); - gdImage::Point* all_p=new(pool) gdImage::Point[table->count()]; + gdImage::Point* all_p=new gdImage::Point[table->count()]; gdImage::Point *add_p=all_p; table->for_each(add_point, &add_p); image->Polygon(all_p, table->count(), @@ -974,8 +962,7 @@ static void _polyline(Request& r, String false/*not closed*/); } -static void _polygon(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _polygon(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -989,15 +976,14 @@ static void _polygon(Request& r, StringP method_name, "coordinates must be table"); - gdImage::Point* all_p=new(pool) gdImage::Point[table->count()]; + gdImage::Point* all_p=new gdImage::Point[table->count()]; gdImage::Point *add_p=all_p; table->for_each(add_point, &add_p); image->Polygon(all_p, table->count(), image->Color(params->as_int(0, "color must be int", r))); } -static void _polybar(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _polybar(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -1011,7 +997,7 @@ static void _polybar(Request& r, StringP method_name, "coordinates must be table"); - gdImage::Point* all_p=new(pool) gdImage::Point[table->count()]; + gdImage::Point* all_p=new gdImage::Point[table->count()]; gdImage::Point *add_p=all_p; table->for_each(add_point, &add_p); image->FilledPolygon(all_p, table->count(), @@ -1022,80 +1008,72 @@ static void _polybar(Request& r, StringP #define Y(y)(y+index*height) -/// simple gdImage-based font storage & text output -class Font: public PA_Object { -public: - - const static int letter_spacing; - int height; ///< Font heigth - int monospace; ///< Default char width - int spacebarspace; ///< spacebar width - gdImagePtr ifont; - StringPtr alphabet; - - Font(//Pool& pool, - StringPtr aalphabet, - gdImagePtr aifont, int aheight, int amonospace, int aspacebarspace): - alphabet(aalphabet), - height(aheight), monospace(amonospace), spacebarspace(aspacebarspace), - ifont(aifont) { - } - - /* ******************************** char ********************************** */ - - int index_of(char ch) { - if(ch==' ') return -1; - return alphabet->pos(&ch, 1); - } - - int index_width(int index) { - if(index<0) - return spacebarspace; - int tr=ifont->GetTransparent(); - for(int x=ifont->SX()-1; x>=0; x--) { - for(int y=0; yGetPixel(x, Y(y))!=tr) - return x+1; - } - return 0; - } - - void index_display(gdImagePtr image, int x, int y, int index){ - if(index>=0) - ifont->Copy(*image, x, y, 0, Y(0), index_width(index), height); - } - - /* ******************************** string ********************************** */ - - int step_width(int index) { - return letter_spacing + (monospace ? monospace : index_width(index)); - } +// Font class + +const int Font::letter_spacing=1; + +Font::Font(//, + const String& aalphabet, + gdImagePtr aifont, int aheight, int amonospace, int aspacebarspace): + alphabet(aalphabet), + height(aheight), monospace(amonospace), spacebarspace(aspacebarspace), + ifont(aifont) { +} + +/* ******************************** char ********************************** */ - // counts trailing letter_spacing, consider this OK. useful for contiuations - int string_width(StringPtr s){ - CharPtr cstr=s->cstr(); - int result=0; - for(const char* current=cstr; *current; current++) - result+=step_width(index_of(*current)); - return result; +int Font::index_of(char ch) { + if(ch==' ') return -1; + return alphabet->pos(&ch, 1); +} + +int Font::index_width(int index) { + if(index<0) + return spacebarspace; + int tr=ifont->GetTransparent(); + for(int x=ifont->SX()-1; x>=0; x--) { + for(int y=0; yGetPixel(x, Y(y))!=tr) + return x+1; } - - void string_display(gdImagePtr image, int x, int y, StringPtr s){ - CharPtr cstr=s->cstr(); - for(const char* current=cstr; *current; current++) { - int index=index_of(*current); - index_display(image, x, y, index); - x+=step_width(index); - } + return 0; +} + +void Font::index_display(gdImagePtr image, int x, int y, int index){ + if(index>=0) + ifont->Copy(*image, x, y, 0, Y(0), index_width(index), height); +} + +/* ******************************** string ********************************** */ + +int Font::step_width(int index) { + return letter_spacing + (monospace ? monospace : index_width(index)); +} + +// counts trailing letter_spacing, consider this OK. useful for contiuations +int Font::string_width(const String& s){ + const char* cstr=s->cstr(); + int result=0; + for(const char* current=cstr; *current; current++) + result+=step_width(index_of(*current)); + return result; +} + +void Font::string_display(gdImagePtr image, int x, int y, const String& s){ + const char* cstr=s->cstr(); + for(const char* current=cstr; *current; current++) { + int index=index_of(*current); + index_display(image, x, y, index); + x+=step_width(index); } - -}; -const int Font::letter_spacing=1; +} + +// + -static void _font(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _font(Request& r, const String& method_name, MethodParams* params) { - StringPtr alphabet=params->as_string(0, "alphabet must not be code"); + const String& alphabet=params->as_string(0, "alphabet must not be code"); gdImagePtr image=load(r, method_name, params->as_string(1, "file_name must not be code")); int spacebar_width=params->as_int(2, "spacebar_width must be int", r); int monospace_width; @@ -1123,12 +1101,11 @@ static void _font(Request& r, StringPtr image->SY() / alphabet->size(), monospace_width, spacebar_width)); } -static void _text(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _text(Request& r, const String& method_name, MethodParams* params) { int x=params->as_int(0, "x must be int", r); int y=params->as_int(1, "y must be int", r); - StringPtr s=params->as_string(2, "text must not be code"); + const String& s=params->as_string(2, "text must not be code"); VImage& vimage=GET_SELF(r, VImage); if(vimage.image) @@ -1144,10 +1121,9 @@ static void _text(Request& r, StringPtr "does not contain an image"); } -static void _length(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _length(Request& r, const String& method_name, MethodParams* params) { - const StringPtr s=params->as_string(0, "text must not be code"); + const const String& s=params->as_string(0, "text must not be code"); VImage& vimage=GET_SELF(r, VImage); if(vimage.image) @@ -1163,8 +1139,7 @@ static void _length(Request& r, StringPt "does not contain an image"); } -static void _arc(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _arc(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -1182,8 +1157,7 @@ static void _arc(Request& r, StringPtr m image->Color(params->as_int(6, "cx must be int", r))); } -static void _sector(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _sector(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -1201,8 +1175,7 @@ static void _sector(Request& r, StringPt image->Color(params->as_int(6, "color must be int", r))); } -static void _circle(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _circle(Request& r, const String& method_name, MethodParams* params) { gdImagePtr image=GET_SELF(r, VImage).image; if(!image) @@ -1221,7 +1194,7 @@ static void _circle(Request& r, StringPt image->Color(params->as_int(3, "color must be int", r))); } -gdImagePtr as_image(Pool& pool, StringPtr method_name, MethodParams* params, +gdImagePtr as_imageconst String& method_name, MethodParams* params, int index, const char* msg) { gdImagePtr src(0); @@ -1241,8 +1214,7 @@ gdImagePtr as_image(Pool& pool, StringPt return src; } -static void _copy(Request& r, StringPtr method_name, MethodParams* params) { - Pool& pool=r.pool(); +static void _copy(Request& r, const String& method_name, MethodParams* params) { gdImagePtr dest=GET_SELF(r, VImage).image; if(!dest)