IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40730


Ignore:
Timestamp:
May 14, 2019, 2:38:41 PM (7 years ago)
Author:
eugene
Message:

add anti-aliasing smoothing to bDraw functions

Location:
branches/eam_branches/ohana.20190329/src/libkapa
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20190329/src/libkapa/include/kapa.h

    r40558 r40730  
    172172  int Nx, Ny, Nbyte;
    173173  bDrawColor **pixels;
     174  char **mask;
    174175  png_color *palette;
    175176  int Npalette;
     177
    176178  // current drawing values:
    177179  int bWeight;
     
    256258int KapaGetImageData (int fd, KapaImageData *graphmode);
    257259int KapaSetToolbox (int fd, int location);
     260int KapaSetSmoothSigma (int fd, float sigma);
    258261
    259262/* KapaColors */
     
    292295bDrawBuffer *bDrawBufferCreate (int Nx, int Ny, int Nbyte, png_color *palette, int Npalette);
    293296void bDrawBufferFree (bDrawBuffer *buffer);
     297int bDrawMerge (bDrawBuffer *base, bDrawBuffer *layer);
    294298void bDrawSetBuffer (bDrawBuffer *buffer);
    295299void bDrawSetColor (bDrawBuffer *buffer, bDrawColor color);
     
    312316void bDrawTriOpen  (bDrawBuffer *buffer, double x1, double y1, double x2, double y2, double x3, double y3);
    313317void bDrawTriFill  (bDrawBuffer *buffer, double x1, double y1, double dx, double dy);
     318
     319void bDrawSmooth (bDrawBuffer *buffer, float sigma);
    314320
    315321/* bDrawRotFont.c */
  • branches/eam_branches/ohana.20190329/src/libkapa/src/KapaWindow.c

    r40570 r40730  
    455455}
    456456
     457int KapaSetSmoothSigma (int fd, float sigma) {
     458
     459  KiiSendCommand (fd, 4, "SIGM");
     460  KiiSendMessage (fd, "%4.1f ", sigma);
     461  KiiWaitAnswer (fd, "DONE");
     462  return (TRUE);
     463}
  • branches/eam_branches/ohana.20190329/src/libkapa/src/bDrawFuncs.c

    r40572 r40730  
    11# include <kapa_internal.h>
    22
    3 // move these to the bDrawBuffer type
    4 // static int bWeight;
    5 // static int bType;
    6 // static bDrawColor bColor;
    7 // static bDrawColor bColor_R;
    8 // static bDrawColor bColor_G;
    9 // static bDrawColor bColor_B;
    10 // static bDrawBuffer *bBuffer;
     3// buffer->pixels carries the plot image
     4// buffer->mask is 0 if the pixel is untouched, 1 if the pixel has data
    115
    126void bDrawCircleSingle (bDrawBuffer *buffer, double xc, double yc, double radius);
     7
     8int bDrawMerge (bDrawBuffer *base, bDrawBuffer *layer) {
     9
     10  // the two bDrawBuffers must match in size and depth
     11
     12  if (base->Nx != layer->Nx) return FALSE;
     13  if (base->Ny != layer->Ny) return FALSE;
     14  if (base->Nbyte != layer->Nbyte) return FALSE;
     15
     16  for (int j = 0; j < base->Ny; j++) {
     17    for (int i = 0; i < base->Nx; i++) {
     18
     19      if (!layer->mask[j][i]) continue;
     20     
     21      // completely opaque top layer:
     22      if (base->Nbyte == 1) {
     23        base->pixels[j][i] = layer->pixels[j][i];
     24      } else {
     25        base->pixels[j][3*i+0] = layer->pixels[j][3*i+0];
     26        base->pixels[j][3*i+1] = layer->pixels[j][3*i+1];
     27        base->pixels[j][3*i+2] = layer->pixels[j][3*i+2];
     28      }
     29    }
     30  }
     31  return TRUE;
     32}
    1333
    1434// create a drawing buffer with either 1 or 3 byte colors
     
    3555
    3656  ALLOCATE (buffer[0].pixels, bDrawColor *, Ny);
     57  ALLOCATE (buffer[0].mask, char *, Ny);
    3758  for (i = 0; i < Ny; i++) {
    3859    ALLOCATE (buffer[0].pixels[i], bDrawColor, Nbyte*Nx);
     60    ALLOCATE (buffer[0].mask[i], char, Nx);
    3961    for (j = 0; j < Nx; j++) {
    4062      if (Nbyte == 1) {
     
    4567        buffer[0].pixels[i][3*j+2] = white_B;
    4668      }
     69      buffer[0].mask[i][j] = 0; // for now: 0 = no data, 1 = data
    4770    }
    4871  }
     
    6386  for (i = 0; i < buffer[0].Ny; i++) {
    6487    free (buffer[0].pixels[i]);
     88    free (buffer[0].mask[i]);
    6589  }
    6690  free (buffer[0].pixels);
    67   free (buffer[0].palette);
     91  free (buffer[0].mask);
     92  // free (buffer[0].palette);
    6893  free (buffer);
    6994  return;
     
    121146    buffer[0].pixels[y][x] = buffer->bColor;
    122147  } else {
    123     // buffer[0].pixels[y][3*x+0] = buffer->bColor_R;
    124     // buffer[0].pixels[y][3*x+1] = buffer->bColor_G;
    125     // buffer[0].pixels[y][3*x+2] = buffer->bColor_B;
    126     buffer[0].pixels[y][3*x+0] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[y][3*x+0] + alpha * buffer->bColor_R));
     148    buffer[0].pixels[y][3*x+0] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[y][3*x+0] + alpha * buffer->bColor_R)); // was just buffer->bColor_R, etc
    127149    buffer[0].pixels[y][3*x+1] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[y][3*x+1] + alpha * buffer->bColor_G));
    128150    buffer[0].pixels[y][3*x+2] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[y][3*x+2] + alpha * buffer->bColor_B));
    129151  }
     152  buffer[0].mask[y][x] = 1;
    130153  return;
    131154}
     
    299322      buffer[0].pixels[Y][3*i+1] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[Y][3*i+1] + alpha * buffer->bColor_G));
    300323      buffer[0].pixels[Y][3*i+2] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[Y][3*i+2] + alpha * buffer->bColor_B));
    301       // buffer[0].pixels[Y][3*i+0] = buffer->bColor_R;
    302       // buffer[0].pixels[Y][3*i+1] = buffer->bColor_G;
    303       // buffer[0].pixels[Y][3*i+2] = buffer->bColor_B;
    304     }
     324    }
     325    buffer[0].mask[Y][i] = 1;
    305326  }
    306327  return;
     
    326347      buffer[0].pixels[i][3*X+1] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[i][3*X+1] + alpha * buffer->bColor_G));
    327348      buffer[0].pixels[i][3*X+2] = MAX (0x00, MIN(0xff, beta * buffer[0].pixels[i][3*X+2] + alpha * buffer->bColor_B));
    328       // buffer[0].pixels[i][3*X+0] = buffer->bColor_R;
    329       // buffer[0].pixels[i][3*X+1] = buffer->bColor_G;
    330       // buffer[0].pixels[i][3*X+2] = buffer->bColor_B;
    331     }
     349    }
     350    buffer[0].mask[i][X] = 1;
    332351  }
    333352  return;
     
    648667}
    649668
     669// run a smoothing kernel on each channel for anti-aliasing
     670// XXX I need to consider the mask as well
     671void bDrawSmooth (bDrawBuffer *buffer, float sigma) {
     672
     673  // generate a temp buffer for storage of the smoothed result
     674  int Nx = buffer[0].Nx;
     675  int Ny = buffer[0].Ny;
     676  ALLOCATE_PTR (temp, bDrawColor, Nx*Ny);
     677
     678  //* build a 1D gaussian
     679  int Nsigma = 2;
     680  int Ns = (int) (Nsigma*sigma + 0.5);
     681  int Ngauss = 2*Ns + 1;
     682  ALLOCATE_PTR (gaussnorm, float, Ngauss);
     683  float *gauss = &gaussnorm[Ns];
     684  for (int i = -Ns; i < Ns + 1; i++) {
     685    gauss[i] = exp ((i*i)/(-2*sigma*sigma));
     686  }
     687
     688  // NOTE XXX: need to handle Nbyte = 1 or 3
     689  int Nchannel = 3;
     690
     691  // we need to smooth the 3 channels independently, do this as three passes
     692  for (int ch = 0; ch < Nchannel; ch++) {
     693
     694    // smooth in X direction
     695    for (int j = 0; j < Ny; j++) {
     696      bDrawColor *vi = (bDrawColor *) &buffer[0].pixels[j][ch];
     697      bDrawColor *vo = &temp[j*Nx];
     698      for (int i = 0; i < Nx; i++, vo++, vi += Nchannel) {
     699        float g = 0.0, s = 0.0;
     700        for (int n = -Ns; n <= Ns; n++) {
     701          if (i+n < 0) continue;
     702          if (i+n >= Nx) continue;
     703          s += gauss[n]*vi[Nchannel*n];
     704          g += gauss[n];
     705        }
     706        *vo = s / g;
     707      }
     708    }
     709
     710    // NOTE: output maps back into original image
     711    // careful on the counting
     712
     713    /* smooth in Y direction */
     714    for (int i = 0; i < Nx; i++) {
     715      bDrawColor *vi = &temp[i];
     716      for (int j = 0; j < Ny; j++) {
     717        float g = 0.0, s = 0.0;
     718        for (int n = -Ns; n <= Ns; n++) {
     719          if (j+n < 0) continue;
     720          if (j+n >= Ny) continue;
     721          s += gauss[n]*vi[(n+j)*Nx]; // vi is a single-index array, so this is stepping by rows
     722          g += gauss[n];
     723        }
     724        buffer[0].pixels[j][Nchannel*i + ch] = s / g;
     725      }
     726    }
     727  }
     728
     729  // smooth the mask
     730  ALLOCATE_PTR (temp_mask, float, Nx*Ny);
     731
     732  // smooth in X direction
     733  for (int j = 0; j < Ny; j++) {
     734    char *vi = (char *) &buffer[0].mask[j][0];
     735    float *vo = &temp_mask[j*Nx];
     736    for (int i = 0; i < Nx; i++, vo++, vi++) {
     737        float g = 0.0, s = 0.0;
     738        for (int n = -Ns; n <= Ns; n++) {
     739          if (i+n < 0) continue;
     740          if (i+n >= Nx) continue;
     741          s += gauss[n]*vi[n];
     742          g += gauss[n];
     743        }
     744        *vo = s / g;
     745    }
     746  }
     747
     748  // NOTE: output maps back into original image
     749  // careful on the counting
     750
     751  /* smooth in Y direction */
     752  for (int i = 0; i < Nx; i++) {
     753    float *vi = &temp_mask[i];
     754    for (int j = 0; j < Ny; j++) {
     755        float g = 0.0, s = 0.0;
     756        for (int n = -Ns; n <= Ns; n++) {
     757          if (j+n < 0) continue;
     758          if (j+n >= Ny) continue;
     759          s += gauss[n]*vi[(n+j)*Nx]; // vi is a single-index array, so this is stepping by rows
     760          g += gauss[n];
     761        }
     762        buffer[0].mask[j][i] = (s / g > 0.05) ? 1 : 0;
     763    }
     764  }
     765
     766  free (temp);
     767  free (temp_mask);
     768  free (gaussnorm);
     769
     770  return;
     771}
     772
    650773/*
     774NOTES on Bresenham-style circles:
    651775the discriminant of inside or outside the circle is:
    652776
Note: See TracChangeset for help on using the changeset viewer.