IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 8, 2025, 4:29:52 PM (12 months ago)
Author:
eugene
Message:

merge changes from eam_branches/ipp-20230313

Location:
trunk/Ohana
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Ohana

  • trunk/Ohana/src/kapa2/src

    • Property svn:ignore set to
      FindColormap.c
  • trunk/Ohana/src/kapa2/src/SetColormap.c

    r41341 r42821  
    2121  float scale, blueRef, redRef, greenRef;
    2222  Graphic *graphic;
     23
     24  if (inName && !strcasecmp(inName, "list")) {
     25    ListColormaps ();
     26    return TRUE;
     27  }
    2328
    2429  graphic = GetGraphic();
     
    214219
    215220  /* anuenue */
    216   if (!strncmp (graphic->colormapName, "file:", 5) ||
    217       !strncmp (graphic->colormapName, "lgcy:", 5) ||
    218       !strncmp (graphic->colormapName, "cetf:", 5) ||
    219       !strncmp (graphic->colormapName, "cetr:", 5) ||
    220       !strncmp (graphic->colormapName, "csvf:", 5)) {
    221 
    222     FILE *f = fopen (&graphic->colormapName[5], "r");
    223     if (!f) {
    224       fprintf (stderr, "failed to open colormap file %s\n", &graphic->colormapName[5]);
     221  KapaColorMapMode mode = KAPA_CM_STATIC;
     222
     223  // we should already have handled the static colormaps by this point
     224  char *basename = ParseColormapName (graphic->colormapName, &mode);
     225  if (mode == KAPA_CM_STATIC) {
     226    fprintf (stderr, "unknown static colormap name %s\n", graphic->colormapName);
     227    free (basename);
     228    return FALSE;
     229  }   
     230
     231  // look in local directory and installation directory
     232  char *truename = FindColormap (basename);
     233  FREE (basename);
     234
     235  if (!truename) {
     236      fprintf (stderr, "cannot find colormap file %s\n", &graphic->colormapName[5]);
    225237      return FALSE;
    226     }
    227     char line[1024];
    228 
    229     int lastIndex = 0;
    230     float lastRed = 0.0;
    231     float lastBlue = 0.0;
    232     float lastGreen = 0.0;
    233 
    234     int isCSV = !strncmp (graphic->colormapName, "csvf:", 5);
    235     int isCET = !strncmp (graphic->colormapName, "cetf:", 5);
    236     int isCETRev = !strncmp (graphic->colormapName, "cetr:", 5);
    237     int isLegacy = !strncmp (graphic->colormapName, "lgcy:", 5);
    238 
    239     float fracIndex, fracRed, fracBlue, fracGreen;
    240 
    241     if (isCET || isCETRev) fracIndex = 0.0;
    242 
    243     while (scan_line_maxlen (f, line, 1024) != EOF) {
    244       // file contains f R G B : f = 0.0 - 1.0, R,G,B = 0.0 - 1.0
    245 
    246       int Nscan;
    247       if (isCSV) {
    248         Nscan = sscanf (line, "%f,%f,%f,%f", &fracIndex, &fracRed, &fracGreen, &fracBlue);
    249         if (Nscan != 4) continue;
    250       }
    251       if (isCET || isCETRev) {
    252         Nscan = sscanf (line, "%f,%f,%f", &fracRed, &fracGreen, &fracBlue);
    253         fracIndex += 1.0 / 256.0;
    254         if (Nscan != 3) continue;
    255       }
    256       if (isLegacy) {
    257         Nscan = sscanf (line, "%f %f %f %f", &fracIndex, &fracRed, &fracBlue, &fracGreen);
    258         if (Nscan != 4) continue;
    259       }
    260       if (!isCSV && !isLegacy && !isCET && !isCETRev) {
    261         Nscan = sscanf (line, "%f %f %f %f", &fracIndex, &fracRed, &fracGreen, &fracBlue);
    262         if (Nscan != 4) continue;
    263       }
    264 
    265       int nextIndex = fracIndex * MaxValue;
    266       if (nextIndex <= lastIndex) {
    267         lastRed = fracRed;
    268         lastBlue = fracBlue;
    269         lastGreen = fracGreen;
    270         continue;
    271       }
    272       float dRdI = (fracRed   - lastRed)   / (float) (nextIndex - lastIndex);
    273       float dBdI = (fracBlue  - lastBlue)  / (float) (nextIndex - lastIndex);
    274       float dGdI = (fracGreen - lastGreen) / (float) (nextIndex - lastIndex);
    275       for (i = lastIndex; i < nextIndex; i++) {
    276         float redValue   = 0xffff * (dRdI * (i - lastIndex) + lastRed);
    277         float blueValue  = 0xffff * (dBdI * (i - lastIndex) + lastBlue);
    278         float greenValue = 0xffff * (dGdI * (i - lastIndex) + lastGreen);
    279         SETVALUE (graphic[0].cmap[i].red,   redValue,   0, 0xffff);
    280         SETVALUE (graphic[0].cmap[i].blue,  blueValue,  0, 0xffff);
    281         SETVALUE (graphic[0].cmap[i].green, greenValue, 0, 0xffff);
    282       }
     238  }
     239 
     240  FILE *f = fopen (truename, "r");
     241  if (!f) {
     242    fprintf (stderr, "failed to open colormap file %s\n", truename);
     243    FREE (truename);
     244    return FALSE;
     245  }
     246  FREE (truename);
     247
     248  char line[1024];
     249 
     250  int lastIndex = 0;
     251  float lastRed = 0.0;
     252  float lastBlue = 0.0;
     253  float lastGreen = 0.0;
     254 
     255  float fracIndex, fracRed, fracBlue, fracGreen;
     256
     257  if (mode == KAPA_CM_CET || mode == KAPA_CM_CET_REV) fracIndex = 0.0;
     258
     259  int checkFormat = TRUE;
     260
     261  while (scan_line_maxlen (f, line, 1024) != EOF) {
     262    // file contains f R G B : f = 0.0 - 1.0, R,G,B = 0.0 - 1.0
     263    // skip any lines with comment character:
     264
     265    char *p = line;
     266    while ((*p != 0) && OHANA_WHITESPACE(*p)) { p++; }
     267    if (*p == 0) { continue; }
     268    if (*p == '#') { continue; }
     269
     270    // attempt to confirm the format (and warn if it does not seem to match)
     271    if (checkFormat) {
     272      int Nscan1 = sscanf (line, "%f,%f,%f,%f", &fracIndex, &fracRed, &fracGreen, &fracBlue);
     273      int Nscan2 = sscanf (line, "%f,%f,%f", &fracRed, &fracGreen, &fracBlue);
     274      int Nscan3 = sscanf (line, "%f %f %f %f", &fracIndex, &fracRed, &fracBlue, &fracGreen);
     275
     276      if (Nscan1 == 4) {
     277        if (mode != KAPA_CM_CSV) { fprintf (stderr, "warning: %s appears to be format csvf: not %s\n line: %s\n", graphic->colormapName, ParseColormapModeFromEnum(mode), line); }
     278      }
     279      if (Nscan2 == 3) {
     280        if ((mode != KAPA_CM_CET) && (mode != KAPA_CM_CET_REV)) { fprintf (stderr, "warning: %s appears to be format cetf: or cetf: not %s\n line: %s\n", graphic->colormapName, ParseColormapModeFromEnum(mode), line); }
     281      }
     282      if (Nscan3 == 4) {
     283        if (mode != KAPA_CM_RAW) { fprintf (stderr, "warning: %s appears to be format file: not %s\n line: %s\n", graphic->colormapName, ParseColormapModeFromEnum(mode), line); }
     284      }
     285      checkFormat = FALSE;
     286    }
     287
     288    int Nscan;
     289    if (mode == KAPA_CM_CSV) {
     290      Nscan = sscanf (line, "%f,%f,%f,%f", &fracIndex, &fracRed, &fracGreen, &fracBlue);
     291      if (Nscan != 4) continue;
     292    }
     293    if ((mode == KAPA_CM_CET || mode == KAPA_CM_CET_REV)) {
     294      Nscan = sscanf (line, "%f,%f,%f", &fracRed, &fracGreen, &fracBlue);
     295      fracIndex += 1.0 / 256.0;
     296      if (Nscan != 3) continue;
     297    }
     298    if (mode == KAPA_CM_LEGACY) {
     299      Nscan = sscanf (line, "%f %f %f %f", &fracIndex, &fracRed, &fracBlue, &fracGreen);
     300      if (Nscan != 4) continue;
     301    }
     302    if (mode == KAPA_CM_RAW) {
     303      Nscan = sscanf (line, "%f %f %f %f", &fracIndex, &fracRed, &fracGreen, &fracBlue);
     304      if (Nscan != 4) continue;
     305    }
     306   
     307    int nextIndex = fracIndex * MaxValue;
     308    if (nextIndex <= lastIndex) {
    283309      lastRed = fracRed;
    284310      lastBlue = fracBlue;
    285311      lastGreen = fracGreen;
    286       lastIndex = nextIndex;
    287     }
    288     fclose (f);
    289 
    290     if ((int) (MaxValue*fracIndex) < MaxValue) {
    291       float dRdI = (1.0 - lastRed)   / (float) (MaxValue - lastIndex);
    292       float dBdI = (1.0 - lastBlue)  / (float) (MaxValue - lastIndex);
    293       float dGdI = (1.0 - lastGreen) / (float) (MaxValue - lastIndex);
    294       for (i = lastIndex; i < MaxValue; i++) {
    295         float redValue   = 0xffff * (dRdI * (i - lastIndex) + lastRed);
    296         float blueValue  = 0xffff * (dBdI * (i - lastIndex) + lastBlue);
    297         float greenValue = 0xffff * (dGdI * (i - lastIndex) + lastGreen);
    298         SETVALUE (graphic[0].cmap[i].red,   redValue,   0, 0xffff);
    299         SETVALUE (graphic[0].cmap[i].blue,  blueValue,  0, 0xffff);
    300         SETVALUE (graphic[0].cmap[i].green, greenValue, 0, 0xffff);
    301       }
    302     }
    303 
    304     // reverse the color sequence:
    305     if (isCETRev) {
    306       for (i = 0; i < MaxValue / 2; i++) { 
    307         unsigned short tmp;
    308         // fprintf (stderr, "swap: %d %d\n", graphic[0].cmap[i].red, graphic[0].cmap[MaxValue - 1 - i].red);
    309         tmp = graphic[0].cmap[i].red;   graphic[0].cmap[i].red   = graphic[0].cmap[MaxValue - 1 - i].red;   graphic[0].cmap[MaxValue - 1 - i].red   = tmp;
    310         tmp = graphic[0].cmap[i].blue;  graphic[0].cmap[i].blue  = graphic[0].cmap[MaxValue - 1 - i].blue;  graphic[0].cmap[MaxValue - 1 - i].blue  = tmp;
    311         tmp = graphic[0].cmap[i].green; graphic[0].cmap[i].green = graphic[0].cmap[MaxValue - 1 - i].green; graphic[0].cmap[MaxValue - 1 - i].green = tmp;
    312       }
    313     }
    314 
    315     goto store_colors;
    316   }
    317 
    318   return (FALSE);
     312      continue;
     313    }
     314    float dRdI = (fracRed   - lastRed)   / (float) (nextIndex - lastIndex);
     315    float dBdI = (fracBlue  - lastBlue)  / (float) (nextIndex - lastIndex);
     316    float dGdI = (fracGreen - lastGreen) / (float) (nextIndex - lastIndex);
     317    for (i = lastIndex; i < nextIndex; i++) {
     318      float redValue   = 0xffff * (dRdI * (i - lastIndex) + lastRed);
     319      float blueValue  = 0xffff * (dBdI * (i - lastIndex) + lastBlue);
     320      float greenValue = 0xffff * (dGdI * (i - lastIndex) + lastGreen);
     321      SETVALUE (graphic[0].cmap[i].red,   redValue,   0, 0xffff);
     322      SETVALUE (graphic[0].cmap[i].blue,  blueValue,  0, 0xffff);
     323      SETVALUE (graphic[0].cmap[i].green, greenValue, 0, 0xffff);
     324    }
     325    lastRed = fracRed;
     326    lastBlue = fracBlue;
     327    lastGreen = fracGreen;
     328    lastIndex = nextIndex;
     329  }
     330  fclose (f);
     331
     332  if ((int) (MaxValue*fracIndex) < MaxValue) {
     333    float dRdI = (1.0 - lastRed)   / (float) (MaxValue - lastIndex);
     334    float dBdI = (1.0 - lastBlue)  / (float) (MaxValue - lastIndex);
     335    float dGdI = (1.0 - lastGreen) / (float) (MaxValue - lastIndex);
     336    for (i = lastIndex; i < MaxValue; i++) {
     337      float redValue   = 0xffff * (dRdI * (i - lastIndex) + lastRed);
     338      float blueValue  = 0xffff * (dBdI * (i - lastIndex) + lastBlue);
     339      float greenValue = 0xffff * (dGdI * (i - lastIndex) + lastGreen);
     340      SETVALUE (graphic[0].cmap[i].red,   redValue,   0, 0xffff);
     341      SETVALUE (graphic[0].cmap[i].blue,  blueValue,  0, 0xffff);
     342      SETVALUE (graphic[0].cmap[i].green, greenValue, 0, 0xffff);
     343    }
     344  }
     345
     346  // reverse the color sequence:
     347  if (mode == KAPA_CM_CET_REV) {
     348    for (i = 0; i < MaxValue / 2; i++) { 
     349      unsigned short tmp;
     350      // fprintf (stderr, "swap: %d %d\n", graphic[0].cmap[i].red, graphic[0].cmap[MaxValue - 1 - i].red);
     351      tmp = graphic[0].cmap[i].red;   graphic[0].cmap[i].red   = graphic[0].cmap[MaxValue - 1 - i].red;   graphic[0].cmap[MaxValue - 1 - i].red   = tmp;
     352      tmp = graphic[0].cmap[i].blue;  graphic[0].cmap[i].blue  = graphic[0].cmap[MaxValue - 1 - i].blue;  graphic[0].cmap[MaxValue - 1 - i].blue  = tmp;
     353      tmp = graphic[0].cmap[i].green; graphic[0].cmap[i].green = graphic[0].cmap[MaxValue - 1 - i].green; graphic[0].cmap[MaxValue - 1 - i].green = tmp;
     354    }
     355  }
    319356
    320357 store_colors:
     
    333370  return (TRUE);
    334371}
    335 
Note: See TracChangeset for help on using the changeset viewer.