OSDN Git Service

avoid temporaries in dither_to_Mono()
authorIvailo Monev <xakepa10@laimg.moc>
Thu, 2 Apr 2020 02:58:02 +0000 (02:58 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Thu, 2 Apr 2020 02:58:02 +0000 (02:58 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/gui/image/qimage.cpp

index 6f8702c..f8b334c 100644 (file)
@@ -2016,11 +2016,8 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
             dithermode = Diffuse;
     }
 
-    int          w = src->width;
-    int          h = src->height;
-    int          d = src->depth;
     uchar gray[256];                                // gray map for 8 bit images
-    bool  use_gray = (d == 8);
+    bool  use_gray = (src->depth == 8);
     if (use_gray) {                                // make gray map
         if (fromalpha) {
             // Alpha 0x00 -> 0 pixels (white)
@@ -2036,19 +2033,17 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
     }
 
     uchar *dst_data = dst->data;
-    int dst_bpl = dst->bytes_per_line;
     const uchar *src_data = src->data;
-    int src_bpl = src->bytes_per_line;
 
     switch (dithermode) {
     case Diffuse: {
-        QScopedArrayPointer<int> lineBuffer(new int[w * 2]);
+        QScopedArrayPointer<int> lineBuffer(new int[src->width * 2]);
         int *line1 = lineBuffer.data();
-        int *line2 = lineBuffer.data() + w;
-        int bmwidth = (w+7)/8;
+        int *line2 = lineBuffer.data() + src->width;
+        int bmwidth = (src->width+7)/8;
 
         int *b1, *b2;
-        int wbytes = w * (d/8);
+        int wbytes = src->width * (src->depth/8);
         const uchar *p = src->data;
         const uchar *end = p + wbytes;
         b2 = line2;
@@ -2068,9 +2063,9 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                 }
             }
         }
-        for (int y=0; y<h; y++) {                        // for each scan line...
+        for (int y=0; y<src->height; y++) {                        // for each scan line...
             int *tmp = line1; line1 = line2; line2 = tmp;
-            bool not_last_line = y < h - 1;
+            bool not_last_line = y < src->height - 1;
             if (not_last_line) {                // calc. grayvals for next line
                 p = src->data + (y+1)*src->bytes_per_line;
                 end = p + wbytes;
@@ -2099,7 +2094,7 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
             b1 = line1;
             b2 = line2;
             int bit = 7;
-            for (int x=1; x<=w; x++) {
+            for (int x=1; x<=src->width; x++) {
                 if (*b1 < 128) {                // black pixel
                     err = *b1++;
                     *p |= 1 << bit;
@@ -2112,13 +2107,13 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                 } else {
                     bit--;
                 }
-                if (x < w)
+                if (x < src->width)
                     *b1 += (err*7)>>4;                // spread error to right pixel
                 if (not_last_line) {
                     b2[0] += (err*5)>>4;        // pixel below
                     if (x > 1)
                         b2[-1] += (err*3)>>4;        // pixel below left
-                    if (x < w)
+                    if (x < src->width)
                         b2[1] += err>>4;        // pixel below right
                 }
                 b2++;
@@ -2128,10 +2123,10 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
     case Ordered: {
 
         memset(dst->data, 0, dst->nbytes);
-        if (d == 32) {
-            for (int i=0; i<h; i++) {
+        if (src->depth == 32) {
+            for (int i=0; i<src->height; i++) {
                 const uint *p = (const uint *)src_data;
-                const uint *end = p + w;
+                const uint *end = p + src->width;
                 uchar *m = dst_data;
                 int bit = 7;
                 int j = 0;
@@ -2158,14 +2153,14 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                         }
                     }
                 }
-                dst_data += dst_bpl;
-                src_data += src_bpl;
+                dst_data += dst->bytes_per_line;
+                src_data += src->bytes_per_line;
             }
         } else
-            /* (d == 8) */ {
-            for (int i=0; i<h; i++) {
+            /* (src->depth == 8) */ {
+            for (int i=0; i<src->height; i++) {
                 const uchar *p = src_data;
-                const uchar *end = p + w;
+                const uchar *end = p + src->width;
                 uchar *m = dst_data;
                 int bit = 7;
                 int j = 0;
@@ -2179,17 +2174,17 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                         bit--;
                     }
                 }
-                dst_data += dst_bpl;
-                src_data += src_bpl;
+                dst_data += dst->bytes_per_line;
+                src_data += src->bytes_per_line;
             }
         }
     } break;
     default: { // Threshold:
         memset(dst->data, 0, dst->nbytes);
-        if (d == 32) {
-            for (int i=0; i<h; i++) {
+        if (src->depth == 32) {
+            for (int i=0; i<src->height; i++) {
                 const uint *p = (const uint *)src_data;
-                const uint *end = p + w;
+                const uint *end = p + src->width;
                 uchar *m = dst_data;
                 int bit = 7;
                 if (fromalpha) {
@@ -2215,14 +2210,14 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                         }
                     }
                 }
-                dst_data += dst_bpl;
-                src_data += src_bpl;
+                dst_data += dst->bytes_per_line;
+                src_data += src->bytes_per_line;
             }
         } else
-            if (d == 8) {
-                for (int i=0; i<h; i++) {
+            if (src->depth == 8) {
+                for (int i=0; i<src->height; i++) {
                     const uchar *p = src_data;
-                    const uchar *end = p + w;
+                    const uchar *end = p + src->width;
                     uchar *m = dst_data;
                     int bit = 7;
                     while (p < end) {
@@ -2235,8 +2230,8 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src,
                             bit--;
                         }
                     }
-                    dst_data += dst_bpl;
-                    src_data += src_bpl;
+                    dst_data += dst->bytes_per_line;
+                    src_data += src->bytes_per_line;
                 }
             }
         }