m3g/m3gcore11/src/m3g_image.c
changeset 45 36b2e23a8629
parent 36 01a6848ebfd7
equal deleted inserted replaced
36:01a6848ebfd7 45:36b2e23a8629
   833 static void m3gConvertPixels(M3GPixelFormat srcFormat, const M3Gubyte *src,
   833 static void m3gConvertPixels(M3GPixelFormat srcFormat, const M3Gubyte *src,
   834                              M3GPixelFormat dstFormat, M3Gubyte *dst,
   834                              M3GPixelFormat dstFormat, M3Gubyte *dst,
   835                              M3Gsizei count)
   835                              M3Gsizei count)
   836 {
   836 {
   837     M3Guint temp[SPAN_BUFFER_SIZE];
   837     M3Guint temp[SPAN_BUFFER_SIZE];
       
   838     const char endianTest[4] = { 1, 0, 0, 0 };
   838 
   839 
   839     M3Guint srcBpp = m3gBytesPerPixel(srcFormat);
   840     M3Guint srcBpp = m3gBytesPerPixel(srcFormat);
   840     M3Guint dstBpp = m3gBytesPerPixel(dstFormat);
   841     M3Guint dstBpp = m3gBytesPerPixel(dstFormat);
   841     M3G_ASSERT(srcBpp > 0 && dstBpp > 0);
   842     M3G_ASSERT(srcBpp > 0 && dstBpp > 0);
   842 
   843 
   843     while (count > 0) {
   844     while (count > 0) {
   844         M3Gsizei n = (count < SPAN_BUFFER_SIZE) ? count : SPAN_BUFFER_SIZE;
   845         M3Gsizei n = count;
   845         if (srcFormat == M3G_ARGB8 && dstFormat != M3G_ARGB8) {
   846 
       
   847         /* Check the source and destination formats to avoid 
       
   848            the intermediate ARGB format conversion. */
       
   849         if (((srcFormat == M3G_RGBA8 && (dstFormat == M3G_BGRA8 || dstFormat == M3G_BGR8_32))
       
   850             || (dstFormat == M3G_RGBA8 && (srcFormat == M3G_BGRA8 || srcFormat == M3G_BGR8_32))) 
       
   851             && (n > 2) && ((*(const int *)endianTest) == 1)) {
       
   852             /* use fast path for RGBA<->BGRA conversion */
       
   853             fastConvertBGRAToRGBA(src, n * srcBpp, n, 1, dst);
       
   854         } else if (srcFormat == M3G_ARGB8 && dstFormat != M3G_ARGB8) {
   846             convertFromARGB((M3Guint*)src, n, dstFormat, dst);
   855             convertFromARGB((M3Guint*)src, n, dstFormat, dst);
   847         } else if (srcFormat != M3G_ARGB8 && dstFormat == M3G_ARGB8) {
   856         } else if (srcFormat != M3G_ARGB8 && dstFormat == M3G_ARGB8) {
   848             convertToARGB(srcFormat, src, n, (M3Guint*)dst);
   857             convertToARGB(srcFormat, src, n, (M3Guint*)dst);
   849         } else {
   858         } else {
       
   859             /* no luck, do the conversion via ARGB (source format -> ARGB -> destination format) */
       
   860             n = (count < SPAN_BUFFER_SIZE) ? count : SPAN_BUFFER_SIZE;
   850             convertToARGB(srcFormat, src, n, temp);
   861             convertToARGB(srcFormat, src, n, temp);
   851             convertFromARGB(temp, n, dstFormat, dst);
   862             convertFromARGB(temp, n, dstFormat, dst);
   852         }
   863         }
   853         count -= SPAN_BUFFER_SIZE; /* \note may go negative */
   864         count -= n;
   854         src += n * srcBpp;
   865         src += n * srcBpp;
   855         dst += n * dstBpp;
   866         dst += n * dstBpp;
   856     }
   867     }
   857 }
   868 }
   858 
   869 
  1300             if (img->data == 0) {
  1311             if (img->data == 0) {
  1301                 m3gFree(m3g, img);
  1312                 m3gFree(m3g, img);
  1302                 return NULL;
  1313                 return NULL;
  1303             }
  1314             }
  1304 
  1315 
       
  1316 #ifdef M3G_ENABLE_GLES_RESOURCE_HANDLING
       
  1317             /* If GLES resource freeing (see function m3gFreeGLESResources) 
       
  1318                is enabled, the GL texture might get deleted at any point, so
       
  1319 			   a copy of the texture data has to be always kept in memory. */
       
  1320             img->pinned = M3G_TRUE;
       
  1321 #else           
  1305             /* Lock the image data in memory if the image is dynamic,
  1322             /* Lock the image data in memory if the image is dynamic,
  1306              * or the format has alpha information; otherwise, we'll
  1323              * or the format has alpha information; otherwise, we'll
  1307              * be able to get rid of an extra copy when generating a
  1324              * be able to get rid of an extra copy when generating a
  1308              * power-of-two version or uploading to OpenGL */
  1325              * power-of-two version or uploading to OpenGL */
  1309             
  1326             
  1310             if ((img->flags & M3G_DYNAMIC) != 0
  1327             if ((img->flags & M3G_DYNAMIC) != 0
  1311                 || (img->format != M3G_RGB &&
  1328                 || (img->format != M3G_RGB &&
  1312                     img->format != M3G_LUMINANCE)) {
  1329                     img->format != M3G_LUMINANCE)) {
  1313                 img->pinned = M3G_TRUE;
  1330                 img->pinned = M3G_TRUE;
  1314             }
  1331             }
  1315 
  1332 #endif
  1316             /* If the image can be used as a rendering target, clear
  1333             /* If the image can be used as a rendering target, clear
  1317              * to opaque white by default */
  1334              * to opaque white by default */
  1318             
  1335             
  1319             if ((img->flags & M3G_RENDERING_TARGET) != 0) {
  1336             if ((img->flags & M3G_RENDERING_TARGET) != 0) {
  1320                 M3Gubyte *pixels = ((M3Gubyte *)m3gMapObject(m3g, img->data))
  1337                 M3Gubyte *pixels = ((M3Gubyte *)m3gMapObject(m3g, img->data))
  1366     flags &= ~(M3G_DYNAMIC|M3G_RENDERING_TARGET);
  1383     flags &= ~(M3G_DYNAMIC|M3G_RENDERING_TARGET);
  1367     flags |= M3G_STATIC;
  1384     flags |= M3G_STATIC;
  1368     
  1385     
  1369     image->flags = flags;
  1386     image->flags = flags;
  1370 
  1387 
       
  1388 #ifndef M3G_ENABLE_GLES_RESOURCE_HANDLING
  1371     /* If the image format has no alpha information, we can discard
  1389     /* If the image format has no alpha information, we can discard
  1372      * the image data under suitable conditions */
  1390      * the image data under suitable conditions */
  1373     
  1391     
  1374     if (image->format == M3G_RGB || image->format == M3G_LUMINANCE) {
  1392     if (image->format == M3G_RGB || image->format == M3G_LUMINANCE) {
  1375         image->pinned = M3G_FALSE;
  1393         image->pinned = M3G_FALSE;
  1376     }
  1394     }
  1377     
  1395 #endif    
  1378     M3G_LOG1(M3G_LOG_IMAGES, "Image 0x%08X made immutable\n",
  1396     M3G_LOG1(M3G_LOG_IMAGES, "Image 0x%08X made immutable\n",
  1379              (unsigned) image);
  1397              (unsigned) image);
  1380 }
  1398 }
  1381 
  1399 
  1382 /*!
  1400 /*!