glVertex  5.5.2
glvertex_rawformat.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_RAWFORMAT_H
6 #define GLVERTEX_RAWFORMAT_H
7 
8 #include <math.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <string>
13 
14 #ifdef _MSC_VER
15 #define strdup _strdup
16 #define snprintf _snprintf
17 #endif
18 
19 // read raw data
20 // the raw file format is encoded into the filename
21 // as in name.size_cellformat_cellsize.raw
22 // e.g. name.256x256.raw (grayscale image)
23 // e.g. name.256x256_3.raw (rgb image)
24 // e.g. name.256x256_u2m.raw (16-bit msb unsigned grayscale image)
25 // e.g. name.256x256x256.raw (8-bit volume)
26 // e.g. name.256x256x256_2_100x100x50.raw (16-bit volume with voxel dimensions)
27 // cell format modifiers:
28 // 1 = 8 bit
29 // 2 = 16 bit
30 // 3 = rgb
31 // 4 = rgba
32 // 6 = rgb 16 bit
33 // 8 = rgba 16 bit
34 // u = unsigned
35 // s = signed
36 // m = msb
37 // l = lsb
38 // default modifiers = u1m
39 unsigned char *lglReadRawData(const char *filename,
40  long long *width,long long *height,long long *depth,
41  unsigned int *components=NULL,unsigned int *bits=NULL,bool *sign=NULL,bool *msb=NULL,
42  float *scalex=NULL,float *scaley=NULL,float *scalez=NULL); // meters
43 
44 // analyze raw file format
45 bool lglReadRawInfo(char *filename,
46  long long *width,long long *height,long long *depth,
47  unsigned int *components=NULL,unsigned int *bits=NULL,bool *sign=NULL,bool *msb=NULL,
48  float *scalex=NULL,float *scaley=NULL,float *scalez=NULL); // meters
49 
50 // write raw data
51 char *lglWriteRawData(const char *filename, // /wo suffix .raw
52  unsigned char *data,
53  long long width,long long height,long long depth=1,
54  unsigned int components=1,unsigned int bits=8,bool sign=false,bool msb=true,
55  float scalex=1.0f,float scaley=1.0f,float scalez=1.0f); // meters
56 
57 // define raw file format
58 char *lglMakeRawInfo(long long width,long long height,long long depth,
59  unsigned int components,unsigned int bits,bool sign,bool msb,
60  float scalex,float scaley,float scalez); // meters
61 
62 // convert a raw array to a 16-bit unsigned array
63 unsigned short int *lglConvertRaw(unsigned char *data,
64  long long width,long long height,long long depth,
65  unsigned int &components,
66  unsigned int &bits,bool sign,bool msb);
67 
68 // quantize 16-bit raw data to 8-bit using a linear mapping
69 unsigned char *lglStretchRaw(unsigned short int *data,
70  long long width,long long height,long long depth,
71  unsigned int components);
72 
73 // quantize 16-bit raw data to 8-bit using a non-linear mapping
74 unsigned char *lglQuantizeRaw(unsigned short int *data,
75  long long width,long long height,long long depth,
76  bool linear=false);
77 
78 // load and quantize raw data
79 unsigned char *lglLoadRawData(const char *filename,
80  long long *width,long long *height,long long *depth,
81  float *scalex=NULL,float *scaley=NULL,float *scalez=NULL); // meters
82 
83 // load and quantize raw image
84 unsigned char *lglLoadRawImage(const char *filename,
85  int *width,int *height,
86  int *components);
87 
88 // load and quantize raw image
89 unsigned char *lglLoadRawImage(std::string filename,
90  int *width,int *height,
91  int *components);
92 
93 // write raw image
94 bool lglWriteRawImage(const char *filename, // /wo suffix .raw
95  unsigned char *image,
96  int width,int height,
97  int components);
98 
99 // write raw image
100 bool lglWriteRawImage(std::string filename, // /wo suffix .raw
101  unsigned char *image,
102  int width,int height,
103  int components);
104 
105 // analyze raw file format
106 inline bool lglReadRawInfo(char *filename,
107  long long *width,long long *height,long long *depth,
108  unsigned int *components,unsigned int *bits,bool *sign,bool *msb,
109  float *scalex,float *scaley,float *scalez)
110 {
111  unsigned int rawcomps=1; // scalar
112  bool rawsign=false; // unsigned
113  unsigned int rawbits=8; // byte
114  bool rawmsb=true; // most significant byte first
115  int rawscalex=1000000,rawscaley=1000000,rawscalez=1000000; // 1E6um = 1m
116  int rawmaxscale=0; // 0um
117 
118  char *dot,*dotdot;
119  int count;
120 
121  dot=strrchr(filename,'.');
122 
123  if (dot==NULL) return(false);
124 
125  if (strcmp(dot,".raw")!=0) return(false);
126 
127  *dot='\0';
128  dotdot=strrchr(filename,'.');
129  *dot='.';
130 
131  if (dotdot==NULL) return(false);
132 
133  *width=*height=*depth=1;
134 
135  dotdot++;
136 
137  if (sscanf(dotdot,"%lldx%lld%n",width,height,&count)!=2) return(false);
138 
139  dotdot+=count;
140 
141  if (*dotdot=='x')
142  {
143  dotdot++;
144 
145  if (sscanf(dotdot,"%lld%n",depth,&count)!=1) return(false);
146 
147  dotdot+=count;
148  }
149 
150  if (*dotdot=='_')
151  {
152  dotdot++;
153 
154  while (*dotdot!='.' && *dotdot!='_')
155  {
156  switch (*dotdot)
157  {
158  case '1': rawcomps=1; rawbits=8; break; // char
159  case '2': rawcomps=1; rawbits=16; break; // short
160  case '3': rawcomps=3; rawbits=8; break; // rgb
161  case '4': rawcomps=4; rawbits=8; break; // rgba
162  case '6': rawcomps=3; rawbits=16; break; // rgb 16-bit
163  case '8': rawcomps=4; rawbits=16; break; // rgba 16-bit
164  case 'u': rawsign=false; break; // unsigned
165  case 's': rawsign=true; break; // signed
166  case 'm': rawmsb=true; break; // MSB
167  case 'l': rawmsb=false; break; // LSB
168  default: return(false);
169  }
170 
171  dotdot++;
172  }
173  }
174 
175  if (*dotdot=='_')
176  {
177  dotdot++;
178 
179  if (sscanf(dotdot,"%dx%d%n",&rawscalex,&rawscaley,&count)!=2) return(false);
180 
181  dotdot+=count;
182 
183  if (*dotdot=='x')
184  {
185  dotdot++;
186 
187  if (sscanf(dotdot,"%d%n",&rawscalez,&count)!=1) return(false);
188 
189  dotdot+=count;
190  }
191  }
192 
193  if (*dotdot!='.') return(false);
194 
195  if (bits==NULL)
196  {
197  if (rawcomps==1 && rawbits==16)
198  {
199  rawcomps=2;
200  rawbits=8;
201  }
202  else if (rawcomps==3 && rawbits==16)
203  {
204  rawcomps=6;
205  rawbits=8;
206  }
207  else if (rawcomps==4 && rawbits==16)
208  {
209  rawcomps=8;
210  rawbits=8;
211  }
212  }
213 
214  if (rawcomps!=1 && components==NULL) return(false);
215  if (rawbits!=8 && bits==NULL) return(false);
216  if (rawsign!=false && sign==NULL) return(false);
217  if (rawmsb!=true && msb==NULL) return(false);
218 
219  if (components!=NULL) *components=rawcomps;
220  if (bits!=NULL) *bits=rawbits;
221  if (sign!=NULL) *sign=rawsign;
222  if (msb!=NULL) *msb=rawmsb;
223 
224  if (rawscalex>rawmaxscale) rawmaxscale=rawscalex;
225  if (rawscaley>rawmaxscale) rawmaxscale=rawscaley;
226  if (rawscalez>rawmaxscale) rawmaxscale=rawscalez;
227 
228  if (rawmaxscale==0) return(false);
229 
230  if (scalex!=NULL) *scalex=rawscalex/1E6f;
231  if (scaley!=NULL) *scaley=rawscaley/1E6f;
232  if (scalez!=NULL) *scalez=rawscalez/1E6f;
233 
234  return(true);
235 }
236 
237 // define raw file format
238 inline char *lglMakeRawInfo(long long width,long long height,long long depth,
239  unsigned int components,unsigned int bits,bool sign,bool msb,
240  float scalex,float scaley,float scalez)
241 {
242  static const int maxlen=100;
243 
244  char info[maxlen];
245  float maxscale=0.0f;
246 
247  snprintf(info,maxlen,".%lldx%lld",width,height);
248  if (depth>1) snprintf(&info[strlen(info)],maxlen-strlen(info),"x%lld",depth);
249 
250  if (components!=1 || bits!=8 || sign!=false || msb!=true ||
251  scalex!=1.0f || scaley!=1.0f || scalez!=1.0f)
252  {
253  snprintf(&info[strlen(info)],maxlen-strlen(info),"_");
254 
255  if (sign==false) snprintf(&info[strlen(info)],maxlen-strlen(info),"u");
256  else snprintf(&info[strlen(info)],maxlen-strlen(info),"s");
257 
258  if (components==1 && bits==8) snprintf(&info[strlen(info)],maxlen-strlen(info),"1");
259  else if (components==1 && bits==16) snprintf(&info[strlen(info)],maxlen-strlen(info),"2");
260  else if (components==2 && bits==8) snprintf(&info[strlen(info)],maxlen-strlen(info),"2");
261  else if (components==3 && bits==8) snprintf(&info[strlen(info)],maxlen-strlen(info),"3");
262  else if (components==4 && bits==8) snprintf(&info[strlen(info)],maxlen-strlen(info),"4");
263  else if (components==3 && bits==16) snprintf(&info[strlen(info)],maxlen-strlen(info),"6");
264  else if (components==4 && bits==16) snprintf(&info[strlen(info)],maxlen-strlen(info),"8");
265  else return(NULL);
266 
267  if (components==2 || bits==16)
268  {
269  if (msb==true) snprintf(&info[strlen(info)],maxlen-strlen(info),"m");
270  else snprintf(&info[strlen(info)],maxlen-strlen(info),"l");
271  }
272 
273  if (scalex>maxscale) maxscale=scalex;
274  if (scaley>maxscale) maxscale=scaley;
275  if (scalez>maxscale) maxscale=scalez;
276 
277  if (maxscale==0.0f) return(NULL);
278 
279  if (scalex!=1.0f || scaley!=1.0f || scalez!=1.0f)
280  {
281  snprintf(&info[strlen(info)],maxlen-strlen(info),"_%dx%d",int(1E6f*scalex+0.5f),int(1E6f*scaley+0.5f));
282  if (depth>1) snprintf(&info[strlen(info)],maxlen-strlen(info),"x%d",int(1E6f*scalez+0.5f));
283  }
284  }
285 
286  snprintf(&info[strlen(info)],maxlen-strlen(info),".raw");
287 
288  return(strdup(info));
289 }
290 
291 // remove .raw suffix
292 inline char *lglRemoveRawSuffix(const char *filename)
293 {
294  char *filename2,*dot;
295 
296  long long rawwidth,rawheight,rawdepth;
297  unsigned int rawcomps,rawbits;
298  bool rawsign,rawmsb;
299  float rawscalex,rawscaley,rawscalez;
300 
301  filename2=strdup(filename);
302 
303  if (lglReadRawInfo(filename2,
304  &rawwidth,&rawheight,&rawdepth,
305  &rawcomps,&rawbits,&rawsign,&rawmsb,
306  &rawscalex,&rawscaley,&rawscalez))
307  {
308  dot=strrchr(filename2,'.');
309  if (dot!=NULL)
310  {
311  *dot='\0';
312  dot=strrchr(filename2,'.');
313  if (dot!=NULL) *dot='\0';
314  }
315  }
316  else
317  {
318  dot=strrchr(filename2,'.');
319  if (dot!=NULL)
320  if (strcmp(dot,".raw")==0) *dot='\0';
321  }
322 
323  return(filename2);
324 }
325 
326 // append raw file format suffix
327 inline char *lglAppendRawInfo(const char *filename,
328  long long width,long long height,long long depth,
329  unsigned int components,unsigned int bits,bool sign,bool msb,
330  float scalex,float scaley,float scalez)
331 {
332  char *filename2;
333  char *info;
334  char *filename3;
335 
336  // define raw info
337  info=lglMakeRawInfo(width,height,depth,
338  components,bits,sign,msb,
339  scalex,scaley,scalez);
340 
341  if (info==NULL) return(NULL);
342 
343  // remove suffix
344  filename2=lglRemoveRawSuffix(filename);
345 
346  // append raw info to filename
347  filename3=(char *)malloc(strlen(filename2)+strlen(info)+1);
348  if (filename3!=NULL)
349  {
350  memcpy(filename3,filename2,strlen(filename2));
351  memcpy(filename3+strlen(info),info,strlen(info)+1);
352  }
353  free(filename2);
354  free(info);
355 
356  return(filename3);
357 }
358 
359 // read raw data
360 inline unsigned char *lglReadRawData(const char *filename,
361  long long *width,long long *height,long long *depth,
362  unsigned int *components,unsigned int *bits,bool *sign,bool *msb,
363  float *scalex,float *scaley,float *scalez)
364 {
365  FILE *file;
366 
367  char *name;
368 
369  unsigned char *volume;
370  long long bytes;
371 
372  // open raw file
373  if ((file=fopen(filename,"rb"))==NULL) return(NULL);
374 
375  // analyze raw info
376  name=strdup(filename);
377  if (!lglReadRawInfo(name,
378  width,height,depth,
379  components,bits,sign,msb,
380  scalex,scaley,scalez))
381  {
382  free(name);
383  fclose(file);
384  return(NULL);
385  }
386  free(name);
387 
388  bytes=(*width)*(*height)*(*depth)*(*components);
389 
390  if (bits!=NULL)
391  if (*bits==16) bytes*=2;
392 
393  if ((volume=(unsigned char *)malloc((size_t)bytes))==NULL) return(NULL);
394 
395  // read raw chunk
396  if (fread(volume,(size_t)bytes,1,file)!=1)
397  {
398  free(volume);
399  fclose(file);
400  return(NULL);
401  }
402 
403  fclose(file);
404 
405  return(volume);
406 }
407 
408 // write raw data
409 inline char *lglWriteRawData(const char *filename, // /wo suffix .raw
410  unsigned char *volume,
411  long long width,long long height,long long depth,
412  unsigned int components,unsigned int bits,bool sign,bool msb,
413  float scalex,float scaley,float scalez)
414 {
415  FILE *file;
416 
417  char *output;
418  long long bytes;
419 
420  // make raw info
421  output=lglAppendRawInfo(filename,
422  width,height,depth,
423  components,bits,sign,msb,
424  scalex,scaley,scalez);
425 
426  if (output==NULL) return(NULL);
427 
428  // open raw output file
429  if ((file=fopen(output,"wb"))==NULL)
430  {
431  free(output);
432  return(NULL);
433  }
434 
435  bytes=width*height*depth*components;
436 
437  if (bits==16) bytes*=2;
438  else if (bits==32) bytes*=4;
439 
440  // write raw chunk
441  if (fwrite(volume,(size_t)bytes,1,file)!=1)
442  {
443  fclose(file);
444  return(NULL);
445  }
446 
447  fclose(file);
448 
449  return(output);
450 }
451 
452 // convert a raw array to a 16-bit unsigned array
453 inline unsigned short int *lglConvertRaw(unsigned char *data,
454  long long width,long long height,long long depth,
455  unsigned int &components,
456  unsigned int &bits,bool sign,bool msb)
457 {
458  long long i;
459 
460  unsigned short int *shorts=NULL;
461  long long cells=width*height*depth;
462 
463  if (components==2 && bits==8)
464  {
465  components=1;
466  bits=16;
467  }
468  else if (components==6 && bits==8)
469  {
470  components=3;
471  bits=16;
472  }
473  else if (components==8 && bits==8)
474  {
475  components=4;
476  bits=16;
477  }
478 
479  cells*=components;
480 
481  if (bits==8)
482  {
483  if ((shorts=(unsigned short int *)malloc((size_t)cells*sizeof(unsigned short int)))==NULL) return(NULL);
484 
485  if (sign)
486  for (i=0; i<cells; i++) shorts[i]=data[i]+128;
487  else
488  for (i=0; i<cells; i++) shorts[i]=data[i];
489  }
490  else if (bits==16)
491  {
492  if ((shorts=(unsigned short int *)malloc((size_t)cells*sizeof(unsigned short int)))==NULL) return(NULL);
493 
494  if (msb)
495  if (sign)
496  for (i=0; i<cells; i++) shorts[i]=(signed short)(256*data[i<<1]+data[(i<<1)+1])+32768;
497  else
498  for (i=0; i<cells; i++) shorts[i]=(unsigned short)(256*data[i<<1]+data[(i<<1)+1]);
499  else
500  if (sign)
501  for (i=0; i<cells; i++) shorts[i]=(signed short)(data[i<<1]+256*data[(i<<1)+1])+32768;
502  else
503  for (i=0; i<cells; i++) shorts[i]=(unsigned short)(data[i<<1]+256*data[(i<<1)+1]);
504  }
505 
506  return(shorts);
507 }
508 
509 // quantize 16-bit raw data to 8-bit using a linear mapping
510 inline unsigned char *lglStretchRaw(unsigned short int *data,
511  long long width,long long height,long long depth,
512  unsigned int components)
513 {
514  long long i;
515 
516  unsigned char *data2;
517  long long cells;
518 
519  int v,vmin,vmax;
520 
521  cells=width*height*depth*components;
522 
523  vmin=65535;
524  vmax=0;
525 
526  for (i=0; i<cells; i++)
527  {
528  v=data[i];
529  if (v<vmin) vmin=v;
530  if (v>vmax) vmax=v;
531  }
532 
533  if (vmin==vmax) vmax=vmin+1;
534 
535  if ((data2=(unsigned char *)malloc((size_t)cells))==NULL)
536  return(NULL);
537 
538  for (i=0; i<cells; i++)
539  data2[i]=(int)((data[i]-vmin)*255.0/(vmax-vmin)+0.5);
540 
541  return(data2);
542 }
543 
544 // helper to get a raw value
545 inline int lglGetRawValue(unsigned short int *data,
546  long long width,long long height,long long /*depth*/,
547  long long i,long long j,long long k)
548 {
549  return(data[i+(j+k*height)*width]);
550 }
551 
552 // helper to get a gradient magnitude value
553 inline double lglGetRawGradMag(unsigned short int *data,
554  long long width,long long height,long long depth,
555  long long i,long long j,long long k)
556 {
557  double gx,gy,gz;
558 
559  if (i>0)
560  if (i<width-1) gx=(lglGetRawValue(data,width,height,depth,i+1,j,k)-lglGetRawValue(data,width,height,depth,i-1,j,k))/2.0;
561  else gx=lglGetRawValue(data,width,height,depth,i,j,k)-lglGetRawValue(data,width,height,depth,i-1,j,k);
562  else
563  if (i<width-1) gx=lglGetRawValue(data,width,height,depth,i+1,j,k)-lglGetRawValue(data,width,height,depth,i,j,k);
564  else gx=0.0;
565 
566  if (j>0)
567  if (j<height-1) gy=(lglGetRawValue(data,width,height,depth,i,j+1,k)-lglGetRawValue(data,width,height,depth,i,j-1,k))/2.0;
568  else gy=lglGetRawValue(data,width,height,depth,i,j,k)-lglGetRawValue(data,width,height,depth,i,j-1,k);
569  else
570  if (j<height-1) gy=lglGetRawValue(data,width,height,depth,i,j+1,k)-lglGetRawValue(data,width,height,depth,i,j,k);
571  else gy=0.0;
572 
573  if (k>0)
574  if (k<depth-1) gz=(lglGetRawValue(data,width,height,depth,i,j,k+1)-lglGetRawValue(data,width,height,depth,i,j,k-1))/2.0;
575  else gz=lglGetRawValue(data,width,height,depth,i,j,k)-lglGetRawValue(data,width,height,depth,i,j,k-1);
576  else
577  if (k<depth-1) gz=lglGetRawValue(data,width,height,depth,i,j,k+1)-lglGetRawValue(data,width,height,depth,i,j,k);
578  else gz=0.0;
579 
580  return(sqrt(gx*gx+gy*gy+gz*gz));
581 }
582 
583 // quantize 16-bit raw data to 8-bit using a non-linear mapping
584 inline unsigned char *lglQuantizeRaw(unsigned short int *data,
585  long long width,long long height,long long depth,
586  bool linear)
587 {
588  long long i,j,k;
589 
590  unsigned char *data2;
591  long long cells;
592 
593  int v,vmin,vmax;
594 
595  double *err,eint;
596 
597  bool done;
598 
599  vmin=65535;
600  vmax=0;
601 
602  for (k=0; k<depth; k++)
603  for (j=0; j<height; j++)
604  for (i=0; i<width; i++)
605  {
606  v=lglGetRawValue(data,width,height,depth,i,j,k);
607  if (v<vmin) vmin=v;
608  if (v>vmax) vmax=v;
609  }
610 
611  if (vmin==vmax) vmax=vmin+1;
612 
613  if (vmax-vmin<256) linear=true;
614 
615  err=new double[65536];
616 
617  if (linear)
618  for (i=0; i<65536; i++) err[i]=255*(double)(i-vmin)/(vmax-vmin);
619  else
620  {
621  for (i=0; i<65536; i++) err[i]=0.0;
622 
623  for (k=0; k<depth; k++)
624  for (j=0; j<height; j++)
625  for (i=0; i<width; i++)
626  err[lglGetRawValue(data,width,height,depth,i,j,k)]+=sqrt(lglGetRawGradMag(data,width,height,depth,i,j,k));
627 
628  for (i=0; i<65536; i++) err[i]=pow(err[i],1.0/3);
629 
630  err[vmin]=err[vmax]=0.0;
631 
632  for (k=0; k<256; k++)
633  {
634  for (eint=0.0,i=0; i<65536; i++) eint+=err[i];
635 
636  done=true;
637 
638  for (i=0; i<65536; i++)
639  if (err[i]>eint/256)
640  {
641  err[i]=eint/256;
642  done=false;
643  }
644 
645  if (done) break;
646  }
647 
648  for (i=1; i<65536; i++) err[i]+=err[i-1];
649 
650  if (err[65535]>0.0f)
651  for (i=0; i<65536; i++) err[i]*=255.0/err[65535];
652  }
653 
654  cells=width*height*depth;
655 
656  if ((data2=(unsigned char *)malloc((size_t)cells))==NULL)
657  {
658  delete err;
659  return(NULL);
660  }
661 
662  for (i=0; i<cells; i++)
663  data2[i]=(int)(err[data[i]]+0.5);
664 
665  delete err;
666 
667  return(data2);
668 }
669 
670 // load and quantize raw data
671 inline unsigned char *lglLoadRawData(const char *filename,
672  long long *width,long long *height,long long *depth,
673  float *scalex,float *scaley,float *scalez) // meters
674 {
675  unsigned char *data=NULL;
676  unsigned int components=0;
677  unsigned int bits=0;
678  bool sign=false,msb=false;
679 
680  data=lglReadRawData(filename,
681  width,height,depth,
682  &components,&bits,&sign,&msb,
683  scalex,scaley,scalez);
684 
685  if (data)
686  if ((bits==8 && sign) || bits==16)
687  {
688  unsigned short int *shorts=lglConvertRaw(data,*width,*height,*depth,components,bits,sign,msb);
689  free(data);
690 
691  unsigned char *data2;
692 
693  if (components==1)
694  data2=lglQuantizeRaw(shorts,*width,*height,*depth);
695  else
696  data2=lglStretchRaw(shorts,*width,*height,*depth,components);
697 
698  free(shorts);
699 
700  data=data2;
701  }
702 
703  return(data);
704 }
705 
706 // load and quantize raw data
707 inline unsigned char *lglLoadRawData(std::string filename,
708  long long *width,long long *height,long long *depth,
709  float *scalex,float *scaley,float *scalez) // meters
710 {
711  return(lglLoadRawData(filename.c_str(),
712  width,height,depth,
713  scalex,scaley,scalez));
714 }
715 
716 // load and quantize raw image
717 inline unsigned char *lglLoadRawImage(const char *filename,
718  int *width,int *height,
719  int *components)
720 {
721  unsigned char *data=NULL;
722  long long lwidth=0,lheight=0,ldepth=0;
723  unsigned int ucomponents=0;
724  unsigned int bits=0;
725  bool sign=false,msb=false;
726 
727  data=lglReadRawData(filename,
728  &lwidth,&lheight,&ldepth,
729  &ucomponents,&bits,&sign,&msb);
730 
731  if (data)
732  {
733  if (ldepth!=1)
734  {
735  free(data);
736  return(NULL);
737  }
738 
739  if ((bits==8 && sign) || bits==16)
740  {
741  unsigned short int *shorts=lglConvertRaw(data,lwidth,lheight,ldepth,ucomponents,bits,sign,msb);
742  free(data);
743 
744  unsigned char *data2=lglStretchRaw(shorts,lwidth,lheight,ldepth,ucomponents);
745  free(shorts);
746 
747  data=data2;
748  }
749 
750  *width=(int)lwidth;
751  *height=(int)lheight;
752  *components=ucomponents;
753  }
754 
755  return(data);
756 }
757 
758 // load and quantize raw image
759 inline unsigned char *lglLoadRawImage(std::string filename,
760  int *width,int *height,
761  int *components)
762 {
763  return(lglLoadRawImage(filename.c_str(),
764  width,height,
765  components));
766 }
767 
768 // write raw image
769 inline bool lglWriteRawImage(const char *filename, // /wo suffix .raw
770  unsigned char *image,
771  int width,int height,
772  int components)
773 {
774  char *raw;
775 
776  raw=lglWriteRawData(filename,
777  image,
778  width,height,1,
779  components);
780 
781  if (raw!=NULL)
782  {
783  free(raw);
784  return(true);
785  }
786 
787  return(false);
788 }
789 
790 // write raw image
791 inline bool lglWriteRawImage(std::string filename, // /wo suffix .raw
792  unsigned char *image,
793  int width,int height,
794  int components)
795 {
796  return(lglWriteRawImage(filename.c_str(),
797  image,
798  width,height,
799  components));
800 }
801 
802 #endif
dot
double dot(const vec2 &a, const vec2 &b)
inner product
Definition: glslmath.h:241