2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 #include "Texture.hxx"
16 float Texture::GrayR[256];
17 float Texture::GrayG[256];
18 float Texture::GrayB[256];
19 unsigned char Texture::RGB8R[256];
20 unsigned char Texture::RGB8G[256];
21 unsigned char Texture::RGB8B[256];
22 unsigned char Texture::R12[256];
23 const bool Texture::isGrayTableInit = Texture::initGrayTables();
24 const bool Texture::isRGB8TableInit = Texture::initRGB8Tables();
25 const bool Texture::isR12TableInit = Texture::initR12Table();
27 #define _DOUBLE_TO_UCHAR_(x) ((x) < 0 ? 0 : ((x) > 1 ? 0xFF : ((unsigned char)((x) * 0xFF))))
29 bool Texture::getImage(void const * data, const int numElements, const DataType datatype, const ImageType imagetype, void ** dest, unsigned int * dataSize, GLType * gltype)
31 unsigned int size = 0;
39 case MATPLOT_HM3_Char :
40 case MATPLOT_HM3_UChar :
41 case MATPLOT_HM4_Char :
42 case MATPLOT_HM4_UChar :
43 size = numElements * sizeof(int);
45 case MATPLOT_HM3_Double :
46 case MATPLOT_HM4_Double :
47 size = numElements * sizeof(float) * 3;
49 case MATPLOT_HM1_Char :
50 case MATPLOT_HM1_UChar :
53 size = numElements * 3;
57 size = numElements * sizeof(int);
61 size = numElements * sizeof(short);
63 case MATPLOT_HM1_Double :
65 size = numElements * sizeof(int);
72 case MATPLOT_HM3_Char :
73 case MATPLOT_HM3_UChar :
74 case MATPLOT_HM4_Char :
75 case MATPLOT_HM4_UChar :
76 size = numElements * sizeof(int);
78 case MATPLOT_HM3_Double :
79 case MATPLOT_HM4_Double :
80 size = numElements * sizeof(float) * 4;
82 case MATPLOT_HM1_Char :
83 case MATPLOT_HM1_UChar :
86 size = numElements * 4;
90 size = numElements * sizeof(int);
94 size = numElements * sizeof(short);
96 case MATPLOT_HM1_Double :
98 size = numElements * sizeof(int);
106 if (datatype == MATPLOT_Short || datatype == MATPLOT_UShort)
108 size = numElements * sizeof(short);
110 else if (datatype == MATPLOT_HM3_Double || datatype == MATPLOT_HM4_Double || datatype == MATPLOT_Double)
112 size = numElements * sizeof(float);
122 case MATPLOT_HM1_Char :
123 case MATPLOT_HM1_UChar :
130 size = numElements * sizeof(int);
133 case MATPLOT_UShort :
134 size = numElements * sizeof(short);
136 case MATPLOT_HM1_Double :
137 case MATPLOT_Double :
138 size = numElements * sizeof(double);
142 case MATPLOT_RGB_332 :
145 case MATPLOT_RGB_444 :
146 case MATPLOT_RGB_555 :
147 case MATPLOT_RGBA_4444 :
148 case MATPLOT_RGBA_5551 :
149 size = numElements * sizeof(short);
153 if (size != *dataSize)
157 *dest = new unsigned char[size];
166 case MATPLOT_HM3_Char :
167 case MATPLOT_HM3_UChar :
168 case MATPLOT_HM4_Char :
169 case MATPLOT_HM4_UChar :
171 const unsigned char * R = (const unsigned char *)data;
172 const unsigned char * G = R + numElements;
173 const unsigned char * B = G + numElements;
174 unsigned char * ucdata = (unsigned char *) * dest;
177 for (int i = 0; i < numElements; i++)
180 ucdata[k + 1] = G[i];
181 ucdata[k + 2] = B[i];
184 *gltype = MATPLOT_GL_RGB;
187 case MATPLOT_HM3_Double :
188 case MATPLOT_HM4_Double :
190 const double * R = (const double *)data;
191 const double * G = R + numElements;
192 const double * B = G + numElements;
193 float * fdata = (float *) * dest;
196 for (int i = 0; i < numElements; i++)
198 fdata[k] = (float)R[i];
199 fdata[k + 1] = (float)G[i];
200 fdata[k + 2] = (float)B[i];
204 *gltype = MATPLOT_GL_RGB_FLOAT;
207 case MATPLOT_HM1_Char :
208 case MATPLOT_HM1_UChar :
211 memcpy(*dest, data, size);
212 *gltype = MATPLOT_GL_RGB;
216 const unsigned int * ui = (const unsigned int *)data;
217 unsigned char * ucdata = (unsigned char *) * dest;
220 for (int i = 0; i < numElements; i++)
222 ucdata[k] = (ui[i] >> 16) & 0xFF;
223 ucdata[k + 1] = (ui[i] >> 8) & 0xFF;
224 ucdata[k + 2] = ui[i] & 0xFF;
228 *gltype = MATPLOT_GL_RGB;
233 const unsigned int * ui = (const unsigned int *)data;
234 unsigned char * ucdata = (unsigned char *) * dest;
237 for (int i = 0; i < numElements; i++)
239 ucdata[k] = (ui[i] >> 24) & 0xFF;
240 ucdata[k + 1] = (ui[i] >> 16) & 0xFF;
241 ucdata[k + 2] = (ui[i] >> 8) & 0xFF;
245 *gltype = MATPLOT_GL_RGB;
250 memcpy(*dest, data, size);
251 *gltype = MATPLOT_GL_RGBA_4444;
254 case MATPLOT_UShort :
256 memcpy(*dest, data, size);
257 *gltype = MATPLOT_GL_RGBA_5551;
260 case MATPLOT_HM1_Double :
261 case MATPLOT_Double :
263 const double * d = (const double *)data;
264 unsigned char * ucdata = (unsigned char *) * dest;
267 for (int i = 0; i < numElements; i++)
269 ucdata[k] = (((unsigned int)d[i]) >> 16) & 0xFF;
270 ucdata[k + 1] = (((unsigned int)d[i]) >> 8) & 0xFF;
271 ucdata[k + 2] = ((unsigned int)d[i]) & 0xFF;
274 *gltype = MATPLOT_GL_RGB;
284 case MATPLOT_HM3_Char :
285 case MATPLOT_HM3_UChar :
287 const unsigned char * R = (const unsigned char *)data;
288 const unsigned char * G = R + numElements;
289 const unsigned char * B = G + numElements;
290 unsigned char * ucdata = (unsigned char *) * dest;
293 for (int i = 0; i < numElements; i++)
296 ucdata[k + 1] = G[i];
297 ucdata[k + 2] = B[i];
298 ucdata[k + 3] = 0xFF;
301 *gltype = MATPLOT_GL_RGBA_BYTE;
304 case MATPLOT_HM4_Char :
305 case MATPLOT_HM4_UChar :
307 const unsigned char * R = (const unsigned char *)data;
308 const unsigned char * G = R + numElements;
309 const unsigned char * B = G + numElements;
310 const unsigned char * A = B + numElements;
311 unsigned char * ucdata = (unsigned char *) * dest;
314 for (int i = 0; i < numElements; i++)
317 ucdata[k + 1] = G[i];
318 ucdata[k + 2] = B[i];
319 ucdata[k + 3] = A[i];
322 *gltype = MATPLOT_GL_RGBA_BYTE;
325 case MATPLOT_HM3_Double :
327 const double * R = (const double *)data;
328 const double * G = R + numElements;
329 const double * B = G + numElements;
330 float * fdata = (float *) * dest;
333 for (int i = 0; i < numElements; i++)
335 fdata[k] = (float)R[i];
336 fdata[k + 1] = (float)G[i];
337 fdata[k + 2] = (float)B[i];
341 *gltype = MATPLOT_GL_RGBA_FLOAT;
344 case MATPLOT_HM4_Double :
346 const double * R = (const double *)data;
347 const double * G = R + numElements;
348 const double * B = G + numElements;
349 const double * A = B + numElements;
350 float * fdata = (float *) * dest;
353 for (int i = 0; i < numElements; i++)
355 fdata[k] = (float)R[i];
356 fdata[k + 1] = (float)G[i];
357 fdata[k + 2] = (float)B[i];
358 fdata[k + 3] = (float)A[i];
361 *gltype = MATPLOT_GL_RGBA_FLOAT;
364 case MATPLOT_HM1_Char :
365 case MATPLOT_HM1_UChar :
368 memcpy(*dest, data, size);
369 *gltype = MATPLOT_GL_RGBA_BYTE;
373 const unsigned int * ui = (const unsigned int *)data;
374 unsigned char * ucdata = (unsigned char *) * dest;
377 for (int i = 0; i < numElements; i++)
379 ucdata[k] = (ui[i] >> 16) & 0xFF;
380 ucdata[k + 1] = (ui[i] >> 8) & 0xFF;
381 ucdata[k + 2] = ui[i] & 0xFF;
382 ucdata[k + 3] = 0xFF;
385 *gltype = MATPLOT_GL_RGBA_BYTE;
390 const unsigned int * ui = (const unsigned int *)data;
391 unsigned char * ucdata = (unsigned char *) * dest;
394 for (int i = 0; i < numElements; i++)
396 ucdata[k] = (ui[i] >> 24) & 0xFF;
397 ucdata[k + 1] = (ui[i] >> 16) & 0xFF;
398 ucdata[k + 2] = (ui[i] >> 8) & 0xFF;
399 ucdata[k + 3] = ui[i] & 0xFF;
402 *gltype = MATPLOT_GL_RGBA_BYTE;
407 memcpy(*dest, data, size);
408 *gltype = MATPLOT_GL_RGBA_4444;
411 case MATPLOT_UShort :
413 memcpy(*dest, data, size);
414 *gltype = MATPLOT_GL_RGBA_5551;
417 case MATPLOT_HM1_Double :
418 case MATPLOT_Double :
420 const double * d = (const double *)data;
421 unsigned char * ucdata = (unsigned char *) * dest;
424 for (int i = 0; i < numElements; i++)
426 ucdata[k] = (((unsigned int)d[i]) >> 24) & 0xFF;
427 ucdata[k + 1] = (((unsigned int)d[i]) >> 16) & 0xFF;
428 ucdata[k + 2] = (((unsigned int)d[i]) >> 8) & 0xFF;
429 ucdata[k + 3] = ((unsigned int)d[i]) & 0xFF;
432 *gltype = MATPLOT_GL_RGBA_BYTE;
441 case MATPLOT_HM3_Char :
442 case MATPLOT_HM3_UChar :
443 case MATPLOT_HM4_Char :
444 case MATPLOT_HM4_UChar :
446 const unsigned char * R = (const unsigned char *)data;
447 const unsigned char * G = R + numElements;
448 const unsigned char * B = G + numElements;
449 unsigned char * ucdata = (unsigned char *) * dest;
451 for (int i = 0; i < numElements; i++)
453 ucdata[i] = (unsigned char) (GrayR[R[i]] + GrayG[G[i]] + GrayB[B[i]]);
455 *gltype = MATPLOT_GL_GRAY;
458 case MATPLOT_HM3_Double :
459 case MATPLOT_HM4_Double :
461 const double * R = (const double *)data;
462 const double * G = R + numElements;
463 const double * B = G + numElements;
464 float * fdata = (float *) * dest;
466 for (int i = 0; i < numElements; i++)
468 fdata[i] = (float) (0.299 * R[i] + 0.587 * G[i] + 0.114 * B[i]);
470 *gltype = MATPLOT_GL_GRAY_FLOAT;
473 case MATPLOT_HM1_Char :
474 case MATPLOT_HM1_UChar :
478 memcpy(*dest, data, size);
479 *gltype = MATPLOT_GL_GRAY;
482 case MATPLOT_UShort :
485 memcpy(*dest, data, size);
486 *gltype = MATPLOT_GL_GRAY_16;
491 const unsigned int * ui = (const unsigned int *)data;
492 unsigned char * ucdata = (unsigned char *) * dest;
494 for (int i = 0; i < numElements; i++)
496 ucdata[i] = (unsigned char) (GrayR[(ui[i] >> 24) & 0xFF] + GrayG[(ui[i] >> 16) & 0xFF] + GrayB[(ui[i] >> 8) & 0xFF]);
498 *gltype = MATPLOT_GL_GRAY;
503 const unsigned int * ui = (const unsigned int *)data;
504 unsigned char * ucdata = (unsigned char *) * dest;
506 for (int i = 0; i < numElements; i++)
508 ucdata[i] = (unsigned char) (GrayR[(ui[i] >> 16) & 0xFF] + GrayG[(ui[i] >> 8) & 0xFF] + GrayB[ui[i] & 0xFF]);
510 *gltype = MATPLOT_GL_GRAY;
513 case MATPLOT_HM1_Double :
514 case MATPLOT_Double :
516 const double * d = (const double *)data;
517 float * fdata = (float *) * dest;
519 for (int i = 0; i < numElements; i++)
521 fdata[i] = (float)d[i];
523 *gltype = MATPLOT_GL_GRAY_FLOAT;
531 memcpy(*dest, data, size);
532 *gltype = MATPLOT_GL_RGBA_BYTE;
539 case MATPLOT_HM3_Char :
540 case MATPLOT_HM3_UChar :
541 case MATPLOT_HM4_Char :
542 case MATPLOT_HM4_UChar :
544 memcpy(*dest, data, numElements);
545 *gltype = MATPLOT_GL_RED;
548 case MATPLOT_HM3_Double :
549 case MATPLOT_HM4_Double :
550 case MATPLOT_HM1_Double :
551 case MATPLOT_Double :
553 const double * d = (const double *)data;
554 float * fdata = (float *) * dest;
556 for (int i = 0; i < numElements; i++)
558 fdata[i] = (float)d[i];
560 *gltype = MATPLOT_GL_RED_FLOAT;
563 case MATPLOT_HM1_Char :
564 case MATPLOT_HM1_UChar :
568 memcpy(*dest, data, size);
569 *gltype = MATPLOT_GL_RED;
573 case MATPLOT_UShort :
575 memcpy(*dest, data, size);
576 *gltype = MATPLOT_GL_RED_16;
581 const unsigned int * ui = (const unsigned int *)data;
582 unsigned char * ucdata = (unsigned char *) * dest;
584 for (int i = 0; i < numElements; i++)
586 ucdata[i] = (unsigned char) ((ui[i] >> 16) & 0xFF);
588 *gltype = MATPLOT_GL_RED;
593 const unsigned int * ui = (const unsigned int *)data;
594 unsigned char * ucdata = (unsigned char *) * dest;
596 for (int i = 0; i < numElements; i++)
598 ucdata[i] = (unsigned char) ((ui[i] >> 24) & 0xFF);
600 *gltype = MATPLOT_GL_RED;
610 case MATPLOT_HM3_Char :
611 case MATPLOT_HM3_UChar :
612 case MATPLOT_HM4_Char :
613 case MATPLOT_HM4_UChar :
615 memcpy(*dest, (unsigned char *)data + numElements, numElements);
616 *gltype = MATPLOT_GL_GREEN;
619 case MATPLOT_HM3_Double :
620 case MATPLOT_HM4_Double :
622 const double * d = (const double *)data + numElements;
623 float * fdata = (float *) * dest;
625 for (int i = 0; i < numElements; i++)
627 fdata[i] = (float)d[i];
629 *gltype = MATPLOT_GL_GREEN_FLOAT;
632 case MATPLOT_HM1_Char :
633 case MATPLOT_HM1_UChar :
637 memcpy(*dest, data, size);
638 *gltype = MATPLOT_GL_GREEN;
642 case MATPLOT_UShort :
644 memcpy(*dest, data, size);
645 *gltype = MATPLOT_GL_GREEN_16;
650 const unsigned int * ui = (const unsigned int *)data;
651 unsigned char * ucdata = (unsigned char *) * dest;
653 for (int i = 0; i < numElements; i++)
655 ucdata[i] = (unsigned char) ((ui[i] >> 8) & 0xFF);
657 *gltype = MATPLOT_GL_GREEN;
662 const unsigned int * ui = (const unsigned int *)data;
663 unsigned char * ucdata = (unsigned char *) * dest;
665 for (int i = 0; i < numElements; i++)
667 ucdata[i] = (unsigned char) ((ui[i] >> 16) & 0xFF);
669 *gltype = MATPLOT_GL_GREEN;
672 case MATPLOT_HM1_Double :
673 case MATPLOT_Double :
675 const double * d = (const double *)data;
676 float * fdata = (float *) * dest;
678 for (int i = 0; i < numElements; i++)
680 fdata[i] = (float)d[i];
682 *gltype = MATPLOT_GL_GREEN_FLOAT;
692 case MATPLOT_HM3_Char :
693 case MATPLOT_HM3_UChar :
694 case MATPLOT_HM4_Char :
695 case MATPLOT_HM4_UChar :
697 memcpy(*dest, (unsigned char *)data + 2 * numElements, numElements);
698 *gltype = MATPLOT_GL_BLUE;
701 case MATPLOT_HM3_Double :
702 case MATPLOT_HM4_Double :
704 const double * d = (const double *)data + 2 * numElements;
705 float * fdata = (float *) * dest;
707 for (int i = 0; i < numElements; i++)
709 fdata[i] = (float)d[i];
711 *gltype = MATPLOT_GL_BLUE_FLOAT;
714 case MATPLOT_HM1_Char :
715 case MATPLOT_HM1_UChar :
719 memcpy(*dest, data, size);
720 *gltype = MATPLOT_GL_BLUE;
724 case MATPLOT_UShort :
726 memcpy(*dest, data, size);
727 *gltype = MATPLOT_GL_BLUE_16;
732 const unsigned int * ui = (const unsigned int *)data;
733 unsigned char * ucdata = (unsigned char *) * dest;
735 for (int i = 0; i < numElements; i++)
737 ucdata[i] = (unsigned char) (ui[i] & 0xFF);
739 *gltype = MATPLOT_GL_BLUE;
744 const unsigned int * ui = (const unsigned int *)data;
745 unsigned char * ucdata = (unsigned char *) * dest;
747 for (int i = 0; i < numElements; i++)
749 ucdata[i] = (unsigned char) ((ui[i] >> 8) & 0xFF);
751 *gltype = MATPLOT_GL_BLUE;
754 case MATPLOT_HM1_Double :
755 case MATPLOT_Double :
757 const double * d = (const double *)data;
758 float * fdata = (float *) * dest;
760 for (int i = 0; i < numElements; i++)
762 fdata[i] = (float)d[i];
764 *gltype = MATPLOT_GL_BLUE_FLOAT;
770 case MATPLOT_RGB_332 :
774 case MATPLOT_HM3_Char :
775 case MATPLOT_HM3_UChar :
776 case MATPLOT_HM4_Char :
777 case MATPLOT_HM4_UChar :
779 const unsigned char * R = (const unsigned char *)data;
780 const unsigned char * G = R + numElements;
781 const unsigned char * B = G + numElements;
782 unsigned char * ucdata = (unsigned char *) * dest;
783 for (int i = 0; i < numElements; i++)
785 ucdata[i] = (unsigned char)(RGB8R[R[i]] | RGB8G[G[i]] | RGB8B[B[i]]);
787 *gltype = MATPLOT_GL_RGB_332;
790 case MATPLOT_HM3_Double :
791 case MATPLOT_HM4_Double :
793 const double * R = (const double *)data;
794 const double * G = R + numElements;
795 const double * B = G + numElements;
796 unsigned char * ucdata = (unsigned char *) * dest;
797 for (int i = 0; i < numElements; i++)
799 ucdata[i] = (unsigned char)(RGB8R[_DOUBLE_TO_UCHAR_(R[i])] | RGB8G[_DOUBLE_TO_UCHAR_(G[i])] | RGB8B[_DOUBLE_TO_UCHAR_(B[i])]);
801 *gltype = MATPLOT_GL_RGB_332;
804 case MATPLOT_HM1_Char :
805 case MATPLOT_HM1_UChar :
809 memcpy(*dest, data, size);
810 *gltype = MATPLOT_GL_RGB_332;
815 const unsigned int * ui = (const unsigned int *)data;
816 unsigned char * ucdata = (unsigned char *) * dest;
818 for (int i = 0; i < numElements; i++)
820 ucdata[i] = (unsigned char)(RGB8R[(ui[i] & 0xFF0000) >> 16] | RGB8G[(ui[i] & 0xFF00) >> 8] | RGB8B[ui[i] & 0xFF]);
822 *gltype = MATPLOT_GL_RGB_332;
827 const unsigned int * ui = (const unsigned int *)data;
828 unsigned char * ucdata = (unsigned char *) * dest;
830 for (int i = 0; i < numElements; i++)
832 ucdata[i] = (unsigned char)(RGB8R[(ui[i] & 0xFF000000) >> 24] | RGB8G[(ui[i] & 0xFF0000) >> 16] | RGB8B[(ui[i] & 0xFF00) >> 8]);
834 *gltype = MATPLOT_GL_RGB_332;
839 const unsigned short * us = (const unsigned short *)data;
840 unsigned char * ucdata = (unsigned char *) * dest;
842 for (int i = 0; i < numElements; i++)
844 ucdata[i] = (unsigned char)(((us[i] & 0xE00) >> 4) | ((us[i] & 0xE0) >> 3) | ((us[i] & 0xC) >> 2));
846 *gltype = MATPLOT_GL_RGB_332;
849 case MATPLOT_UShort :
851 const unsigned short * us = (const unsigned short *)data;
852 unsigned char * ucdata = (unsigned char *) * dest;
854 for (int i = 0; i < numElements; i++)
856 ucdata[i] = (unsigned char)(((us[i] & 0xE000) >> 8) | ((us[i] & 0xE00) >> 7) | ((us[i] & 0xC0) >> 6));
858 *gltype = MATPLOT_GL_RGB_332;
861 case MATPLOT_HM1_Double :
862 case MATPLOT_Double :
864 const double * d = (const double *)data;
865 unsigned char * ucdata = (unsigned char *) * dest;
867 for (int i = 0; i < numElements; i++)
869 const unsigned int ui = (unsigned int)d[i];
870 ucdata[i] = (unsigned char)(RGB8R[(ui & 0xFF000000) >> 24] | RGB8G[(ui & 0xFF0000) >> 16] | RGB8B[(ui & 0xFF00) >> 8]);
872 *gltype = MATPLOT_GL_RGB_332;
878 case MATPLOT_RGB_444 :
882 case MATPLOT_HM3_Char :
883 case MATPLOT_HM3_UChar :
884 case MATPLOT_HM4_Char :
885 case MATPLOT_HM4_UChar :
887 const unsigned char * R = (const unsigned char *)data;
888 const unsigned char * G = R + numElements;
889 const unsigned char * B = G + numElements;
890 unsigned short * usdata = (unsigned short *) * dest;
891 for (int i = 0; i < numElements; i++)
893 usdata[i] = (unsigned short)((R12[R[i]] << 12) | (R12[G[i]] << 8) | (R12[B[i]] << 4) | 0xF);
895 *gltype = MATPLOT_GL_RGBA_4444;
898 case MATPLOT_HM3_Double :
899 case MATPLOT_HM4_Double :
901 const double * R = (const double *)data;
902 const double * G = R + numElements;
903 const double * B = G + numElements;
904 unsigned short * usdata = (unsigned short *) * dest;
905 for (int i = 0; i < numElements; i++)
907 usdata[i] = (unsigned short)((R12[_DOUBLE_TO_UCHAR_(R[i])] << 12) | (R12[_DOUBLE_TO_UCHAR_(G[i])] << 8) | (R12[_DOUBLE_TO_UCHAR_(B[i])] << 4) | 0xF);
909 *gltype = MATPLOT_GL_RGBA_4444;
912 case MATPLOT_HM1_Char :
913 case MATPLOT_HM1_UChar :
917 memcpy(*dest, data, size);
918 *gltype = MATPLOT_GL_RGBA_4444;
923 const unsigned int * ui = (const unsigned int *)data;
924 unsigned short * usdata = (unsigned short *) * dest;
926 for (int i = 0; i < numElements; i++)
928 usdata[i] = (unsigned short)((R12[(ui[i] & 0xFF0000) >> 16] << 12) | (R12[(ui[i] & 0xFF00) >> 8] << 8) | (R12[ui[i] & 0xFF] << 4) | 0xF);
930 *gltype = MATPLOT_GL_RGBA_4444;
935 const unsigned int * ui = (const unsigned int *)data;
936 unsigned short * usdata = (unsigned short *) * dest;
938 for (int i = 0; i < numElements; i++)
940 usdata[i] = (unsigned short)((R12[(ui[i] & 0xFF000000) >> 24] << 12) | (R12[(ui[i] & 0xFF0000) >> 16] << 8) | (R12[(ui[i] & 0xFF00) >> 8] << 4) | (R12[ui[i] & 0xFF]));
942 *gltype = MATPLOT_GL_RGBA_4444;
945 case MATPLOT_UShort :
948 const unsigned short * us = (const unsigned short *)data;
949 unsigned short * usdata = (unsigned short *) * dest;
950 for (int i = 0; i < numElements; i++)
952 usdata[i] = (us[i] << 4) | 0xF;
954 *gltype = MATPLOT_GL_RGBA_4444;
957 case MATPLOT_HM1_Double :
958 case MATPLOT_Double :
960 const double * d = (const double *)data;
961 unsigned short * usdata = (unsigned short *) * dest;
963 for (int i = 0; i < numElements; i++)
965 const unsigned int ui = (unsigned int)d[i];
966 usdata[i] = (unsigned short)((R12[(ui & 0xFF0000) >> 16] << 12) | (R12[(ui & 0xFF00) >> 8] << 8) | (R12[ui & 0xFF] << 4) | 0xF);
968 *gltype = MATPLOT_GL_RGBA_4444;
974 case MATPLOT_RGB_555 :
978 case MATPLOT_HM3_Char :
979 case MATPLOT_HM3_UChar :
980 case MATPLOT_HM4_Char :
981 case MATPLOT_HM4_UChar :
983 const unsigned char * R = (const unsigned char *)data;
984 const unsigned char * G = R + numElements;
985 const unsigned char * B = G + numElements;
986 unsigned short * usdata = (unsigned short *) * dest;
987 for (int i = 0; i < numElements; i++)
989 usdata[i] = (unsigned short)(((R[i] & 0xF8) << 8) | ((G[i] & 0xF8) << 3) | ((B[i] & 0xF8) >> 2) | 0x1);
991 *gltype = MATPLOT_GL_RGBA_5551;
994 case MATPLOT_HM3_Double :
995 case MATPLOT_HM4_Double :
997 const double * R = (const double *)data;
998 const double * G = R + numElements;
999 const double * B = G + numElements;
1000 unsigned short * usdata = (unsigned short *) * dest;
1001 for (int i = 0; i < numElements; i++)
1003 usdata[i] = (unsigned short)(((_DOUBLE_TO_UCHAR_(R[i]) & 0xF8) << 8) | ((_DOUBLE_TO_UCHAR_(G[i]) & 0xF8) << 3) | ((_DOUBLE_TO_UCHAR_(B[i]) & 0xF8) >> 2) | 0x1);
1005 *gltype = MATPLOT_GL_RGBA_5551;
1008 case MATPLOT_HM1_Char :
1009 case MATPLOT_HM1_UChar :
1010 case MATPLOT_UChar :
1013 memcpy(*dest, data, size);
1014 *gltype = MATPLOT_GL_RGBA_5551;
1019 const unsigned int * ui = (const unsigned int *)data;
1020 unsigned short * usdata = (unsigned short *) * dest;
1021 for (int i = 0; i < numElements; i++)
1023 usdata[i] = (unsigned short)(((ui[i] & 0xF80000) >> 8) | ((ui[i] & 0xF800) >> 5) | ((ui[i] & 0xF8) >> 2) | 0x1);
1025 *gltype = MATPLOT_GL_RGBA_5551;
1030 const unsigned int * ui = (const unsigned int *)data;
1031 unsigned short * usdata = (unsigned short *) * dest;
1032 for (int i = 0; i < numElements; i++)
1034 usdata[i] = (unsigned short)(((ui[i] & 0xF8000000) >> 16) | ((ui[i] & 0xF80000) >> 13) | ((ui[i] & 0xF800) >> 10) | 0x1);
1036 *gltype = MATPLOT_GL_RGBA_5551;
1039 case MATPLOT_UShort :
1040 case MATPLOT_Short :
1042 const unsigned short * ui = (const unsigned short *)data;
1043 unsigned short * usdata = (unsigned short *) * dest;
1044 for (int i = 0; i < numElements; i++)
1046 usdata[i] = (ui[i] << 1) | 0x1;
1048 *gltype = MATPLOT_GL_RGBA_5551;
1051 case MATPLOT_HM1_Double :
1052 case MATPLOT_Double :
1054 const double * d = (const double *)data;
1055 unsigned short * usdata = (unsigned short *) * dest;
1057 for (int i = 0; i < numElements; i++)
1059 const unsigned int ui = (unsigned int)d[i];
1060 usdata[i] = (unsigned short)(((ui & 0xF80000) >> 13) | ((ui & 0xF800) >> 5) | ((ui & 0xF8) >> 2) | 0x1);
1062 *gltype = MATPLOT_GL_RGBA_4444;
1068 case MATPLOT_RGBA_4444 :
1072 case MATPLOT_HM3_Char :
1073 case MATPLOT_HM3_UChar :
1075 const unsigned char * R = (const unsigned char *)data;
1076 const unsigned char * G = R + numElements;
1077 const unsigned char * B = G + numElements;
1078 unsigned short * usdata = (unsigned short *) * dest;
1079 for (int i = 0; i < numElements; i++)
1081 usdata[i] = (unsigned short)((R12[R[i]] << 12) | (R12[G[i]] << 8) | (R12[B[i]] << 4) | 0xF);
1083 *gltype = MATPLOT_GL_RGBA_4444;
1086 case MATPLOT_HM4_Char :
1087 case MATPLOT_HM4_UChar :
1089 const unsigned char * R = (const unsigned char *)data;
1090 const unsigned char * G = R + numElements;
1091 const unsigned char * B = G + numElements;
1092 const unsigned char * A = B + numElements;
1093 unsigned short * usdata = (unsigned short *) * dest;
1094 for (int i = 0; i < numElements; i++)
1096 usdata[i] = (unsigned short)((R12[R[i]] << 12) | (R12[G[i]] << 8) | (R12[B[i]] << 4) | R12[A[i]]);
1098 *gltype = MATPLOT_GL_RGBA_4444;
1101 case MATPLOT_HM3_Double :
1103 const double * R = (const double *)data;
1104 const double * G = R + numElements;
1105 const double * B = G + numElements;
1106 unsigned short * usdata = (unsigned short *) * dest;
1107 for (int i = 0; i < numElements; i++)
1109 usdata[i] = (unsigned short)((R12[_DOUBLE_TO_UCHAR_(R[i])] << 12) | (R12[_DOUBLE_TO_UCHAR_(G[i])] << 8) | (R12[_DOUBLE_TO_UCHAR_(B[i])] << 4) | 0xF);
1111 *gltype = MATPLOT_GL_RGBA_4444;
1114 case MATPLOT_HM4_Double :
1116 const double * R = (const double *)data;
1117 const double * G = R + numElements;
1118 const double * B = G + numElements;
1119 const double * A = B + numElements;
1120 unsigned short * usdata = (unsigned short *) * dest;
1121 for (int i = 0; i < numElements; i++)
1123 usdata[i] = (unsigned short)((R12[_DOUBLE_TO_UCHAR_(R[i])] << 12) | (R12[_DOUBLE_TO_UCHAR_(G[i])] << 8) | (R12[_DOUBLE_TO_UCHAR_(B[i])] << 4) | R12[_DOUBLE_TO_UCHAR_(A[i])]);
1125 *gltype = MATPLOT_GL_RGBA_4444;
1128 case MATPLOT_HM1_Char :
1129 case MATPLOT_HM1_UChar :
1130 case MATPLOT_UChar :
1133 memcpy(*dest, data, size);
1134 *gltype = MATPLOT_GL_RGBA_4444;
1139 const unsigned int * ui = (const unsigned int *)data;
1140 unsigned short * usdata = (unsigned short *) * dest;
1141 for (int i = 0; i < numElements; i++)
1143 usdata[i] = (unsigned short)((R12[(ui[i] & 0xFF0000) >> 16] << 12) | (R12[(ui[i] & 0xFF00) >> 8] << 8) | (R12[ui[i] & 0xFF] << 4) | 0xF);
1145 *gltype = MATPLOT_GL_RGBA_4444;
1150 const unsigned int * ui = (const unsigned int *)data;
1151 unsigned short * usdata = (unsigned short *) * dest;
1153 for (int i = 0; i < numElements; i++)
1155 usdata[i] = (unsigned short)((R12[(ui[i] & 0xFF000000) >> 24] << 12) | (R12[(ui[i] & 0xFF0000) >> 16] << 8) | (R12[(ui[i] & 0xFF00) >> 8] << 4) | R12[ui[i] & 0xFF]);
1157 *gltype = MATPLOT_GL_RGBA_4444;
1160 case MATPLOT_UShort :
1161 case MATPLOT_Short :
1163 memcpy(*dest, data, size);
1164 *gltype = MATPLOT_GL_RGBA_4444;
1167 case MATPLOT_HM1_Double :
1168 case MATPLOT_Double :
1170 const double * d = (const double *)data;
1171 unsigned short * usdata = (unsigned short *) * dest;
1173 for (int i = 0; i < numElements; i++)
1175 const unsigned int ui = (unsigned int)d[i];
1176 usdata[i] = (unsigned short)((R12[(ui & 0xFF000000) >> 24] << 12) | (R12[(ui & 0xFF0000) >> 16] << 8) | (R12[(ui & 0xFF) >> 8] << 4) | R12[ui & 0xFF]);
1178 *gltype = MATPLOT_GL_RGBA_4444;
1184 case MATPLOT_RGBA_5551 :
1188 case MATPLOT_HM3_Char :
1189 case MATPLOT_HM3_UChar :
1191 const unsigned char * R = (const unsigned char *)data;
1192 const unsigned char * G = R + numElements;
1193 const unsigned char * B = G + numElements;
1194 unsigned short * usdata = (unsigned short *) * dest;
1195 for (int i = 0; i < numElements; i++)
1197 usdata[i] = (unsigned short)(((R[i] & 0xF8) << 8) | ((G[i] & 0xF8) << 3) | ((B[i] & 0xF8) >> 2) | 0x1);
1199 *gltype = MATPLOT_GL_RGBA_5551;
1202 case MATPLOT_HM4_Char :
1203 case MATPLOT_HM4_UChar :
1205 const unsigned char * R = (const unsigned char *)data;
1206 const unsigned char * G = R + numElements;
1207 const unsigned char * B = G + numElements;
1208 const unsigned char * A = B + numElements;
1209 unsigned short * usdata = (unsigned short *) * dest;
1210 for (int i = 0; i < numElements; i++)
1212 usdata[i] = (unsigned short)(((R[i] & 0xF8) << 8) | ((G[i] & 0xF8) << 3) | ((B[i] & 0xF8) >> 2) | ((A[i] & 0x1)));
1214 *gltype = MATPLOT_GL_RGBA_5551;
1217 case MATPLOT_HM3_Double :
1219 const double * R = (const double *)data;
1220 const double * G = R + numElements;
1221 const double * B = G + numElements;
1222 unsigned short * usdata = (unsigned short *) * dest;
1223 for (int i = 0; i < numElements; i++)
1225 usdata[i] = (unsigned short)(((_DOUBLE_TO_UCHAR_(R[i]) & 0xF8) << 8) | ((_DOUBLE_TO_UCHAR_(G[i]) & 0xF8) << 3) | ((_DOUBLE_TO_UCHAR_(B[i]) & 0xF8) >> 2) | 0x1);
1227 *gltype = MATPLOT_GL_RGBA_5551;
1230 case MATPLOT_HM4_Double :
1232 const double * R = (const double *)data;
1233 const double * G = R + numElements;
1234 const double * B = G + numElements;
1235 const double * A = B + numElements;
1236 unsigned short * usdata = (unsigned short *) * dest;
1237 for (int i = 0; i < numElements; i++)
1239 usdata[i] = (unsigned short)(((_DOUBLE_TO_UCHAR_(R[i]) & 0xF8) << 8) | ((_DOUBLE_TO_UCHAR_(G[i]) & 0xF8) << 3) | ((_DOUBLE_TO_UCHAR_(B[i]) & 0xF8) >> 2) | (_DOUBLE_TO_UCHAR_(A[i]) & 0x1));
1241 *gltype = MATPLOT_GL_RGBA_5551;
1244 case MATPLOT_HM1_Char :
1245 case MATPLOT_HM1_UChar :
1246 case MATPLOT_UChar :
1249 memcpy(*dest, data, size);
1250 *gltype = MATPLOT_GL_RGBA_5551;
1255 const unsigned int * ui = (const unsigned int *)data;
1256 unsigned short * usdata = (unsigned short *) * dest;
1258 for (int i = 0; i < numElements; i++)
1260 usdata[i] = (unsigned short)(((ui[i] & 0xF80000) >> 13) | ((ui[i] & 0xF800) >> 5) | ((ui[i] & 0xF8) >> 2) | 0x1);
1262 *gltype = MATPLOT_GL_RGBA_5551;
1267 const unsigned int * ui = (const unsigned int *)data;
1268 unsigned short * usdata = (unsigned short *) * dest;
1270 for (int i = 0; i < numElements; i++)
1272 usdata[i] = (unsigned short)(((ui[i] & 0xF8000000) >> 21) | ((ui[i] & 0xF80000) >> 13) | ((ui[i] & 0xF800) >> 10) | ((ui[i] & 0xFF) >> 7));
1274 *gltype = MATPLOT_GL_RGBA_5551;
1277 case MATPLOT_UShort :
1278 case MATPLOT_Short :
1280 memcpy(*dest, data, size);
1281 *gltype = MATPLOT_GL_RGBA_5551;
1284 case MATPLOT_HM1_Double :
1285 case MATPLOT_Double :
1287 const double * d = (const double *)data;
1288 unsigned short * usdata = (unsigned short *) * dest;
1290 for (int i = 0; i < numElements; i++)
1292 const unsigned int ui = (unsigned int)d[i];
1293 usdata[i] = (unsigned short)(((ui & 0xF8000000) >> 21) | ((ui & 0xF80000) >> 13) | ((ui & 0xF800) >> 10) | ((ui & 0xFF) >> 7));
1295 *gltype = MATPLOT_GL_RGBA_4444;
1307 bool Texture::initGrayTables()
1309 for (int c = 0; c < 256; c++)
1311 GrayR[c] = 0.299f * (float)c;
1312 GrayG[c] = 0.587f * (float)c;
1313 GrayB[c] = 0.114f * (float)c;
1319 bool Texture::initRGB8Tables()
1321 for (int c = 0; c < 256; c++)
1323 RGB8R[c] = (c / 36) << 5;
1324 RGB8G[c] = (c / 36) << 2;
1331 bool Texture::initR12Table()
1333 for (int c = 0; c < 256; c++)