"Fossies" - the Fresh Open Source Software Archive

Member "mapgenerator-1.1.2/src/Frame_Buffer.cpp" (26 Oct 2003, 4001 Bytes) of package /linux/www/old/mapgenerator-1.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Frame_Buffer.cpp" see the Fossies "Dox" file reference documentation.

    1 //
    2 // Copyright 2001 Jim Hourihan
    3 //
    4 // This library is free software; you can redistribute it and/or
    5 // modify it under the terms of the GNU Library General Public
    6 // License as published by the Free Software Foundation; either
    7 // version 2 of the License, or (at your option) any later version.
    8 //
    9 // This library is distributed in the hope that it will be useful,
   10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12 // Library General Public License for more details.
   13 //
   14 // You should have received a copy of the GNU Library General Public
   15 // License along with this library; if not, write to the Free Software
   16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   17 // USA.
   18 //
   19 
   20 #include "Frame_Buffer.h"
   21 
   22 Frame_Buffer::Frame_Buffer(size_t w, size_t h, size_t components)
   23     : _w(w), _h(h), _buffer(0), _components(components)
   24 {
   25     _buffer = new Component[_w*_h*_components];
   26 }
   27 
   28 Frame_Buffer::~Frame_Buffer()
   29 {
   30     delete [] _buffer;
   31 }
   32 
   33 Frame_Buffer * Frame_Buffer::resize(int w,int h){
   34     Frame_Buffer *n=this;
   35     if(h<n->height()){
   36         double next=((double)n->height()/(n->height()-h));
   37         double first=next/2.0;
   38         n=min_y(n,first,next,h,n->height()-h);
   39     }
   40     if(w<n->width()){
   41         double next=((double)n->width()/(n->width()-w));
   42         double first=next/2.0;
   43         n=min_x(n,first,next,w,n->width()-w);
   44     }
   45     if(w>n->width()){
   46         double next=((double)w/(w-n->width()));
   47         double first=next/2.0;
   48         n=max_x(n,first,next,w,w-n->width());
   49     }
   50     if(h>n->height()){
   51         double next=((double)h/(h-n->height()));
   52         double first=next/2.0;
   53         n=max_y(n,first,next,w,h,h-n->height());
   54     }
   55     return n;
   56 }
   57 
   58 Frame_Buffer * Frame_Buffer::min_y(Frame_Buffer * fb,double first,double next,int h,int del){
   59     unsigned char *tmp;
   60     unsigned char *buf=fb->buffer();
   61     Frame_Buffer *newi=new Frame_Buffer(fb->width(),h,fb->components());
   62     tmp=newi->buffer();
   63     int max=fb->width()*fb->height()*fb->components();
   64     double line=first;
   65     int a=0,d=0;
   66     for(int i=0;i<max;i++){
   67         int l=(int)(((int)(i/fb->components()))/fb->width());
   68         if(l>(int)line){
   69             line+=next;
   70             d++;
   71         }
   72         if(l!=(int)line || d>=del){
   73             tmp[a]=buf[i];
   74             a++;
   75         }
   76     }
   77     return newi;
   78 }
   79 
   80 Frame_Buffer * Frame_Buffer::min_x(Frame_Buffer * fb,double first,double next,int w,int del){
   81     unsigned char *tmp;
   82     unsigned char *buf=fb->buffer();
   83     Frame_Buffer *newi=new Frame_Buffer(w,fb->height(),fb->components());
   84     tmp=newi->buffer();
   85     int max=fb->width()*fb->height()*fb->components();
   86     double row=first;
   87     int a=0;
   88     int d=0;
   89     for(int i=0;i<max;i++){
   90         int r=(int)(((int)(i/fb->components()))%fb->width());
   91         if(r>(int)row) row+=next;
   92         if(r==0){
   93             row=first;
   94             d=0;
   95         }
   96         if(r!=(int)row || d/fb->components()>=del){
   97             tmp[a]=buf[i];
   98             a++;
   99         }
  100         else d++;
  101     }
  102     return newi;
  103 }
  104 
  105 Frame_Buffer * Frame_Buffer::max_x(Frame_Buffer * fb, double first, double next,int w,int max){
  106     unsigned char *tmp;
  107     unsigned char *buf=fb->buffer();
  108     Frame_Buffer *newi=new Frame_Buffer(w,fb->height(),fb->components());
  109     tmp=newi->buffer();
  110     int maxn=w*fb->height()*fb->components();
  111     double row=first;
  112     int a=0, m=0;
  113     for(int i=0;i<maxn;i++){
  114         int r=(int)(((int)(i/fb->components()))%w);
  115         if(r>(int)row) row+=next;
  116         if(r==0){
  117             row=first;
  118             m=0;
  119         }
  120         if(r==(int)row && m/fb->components()<max){
  121             tmp[i]=tmp[i-fb->components()];
  122             m++;
  123         }
  124         else{
  125             tmp[i]=buf[a];
  126             a++;
  127         }
  128     }
  129     return newi;
  130 }
  131 
  132 Frame_Buffer * Frame_Buffer::max_y(Frame_Buffer * fb, double first, double next,int w,int h,int max){
  133     unsigned char *tmp;
  134     unsigned char *buf=fb->buffer();
  135     Frame_Buffer *newi=new Frame_Buffer(fb->width(),h,fb->components());
  136     tmp=newi->buffer();
  137     int maxn=fb->width()*h*fb->components();
  138     double line=first;
  139     int a=0, m=0;
  140     for(int i=0;i<maxn;i++){
  141         int l=(int)(((int)(i/fb->components()))/w);
  142         if(l>(int)line) line+=next;
  143         if(l==(int)line && (m/fb->components())/fb->width()<max){
  144             tmp[i]=tmp[i-fb->components()*fb->width()];
  145             m++;
  146         }
  147         else{
  148             tmp[i]=buf[a];
  149             a++;
  150         }
  151     }
  152     return newi;
  153 }