Jelajahi Sumber

Re-enable zbar, don't crash application if using an unsupported pixel format for preview for zbar

Kristian Vos 5 bulan lalu
induk
melakukan
db188f2db7
1 mengubah file dengan 41 tambahan dan 40 penghapusan
  1. 41 40
      src/zbar_pipeline.c

+ 41 - 40
src/zbar_pipeline.c

@@ -183,49 +183,51 @@ process_image(MPPipeline *pipeline, MPZBarImage **_image)
                 libmegapixels_mode_width_to_bytes(image->format, image->width);
         int padding_bytes =
                 libmegapixels_mode_width_to_padding(image->format, image->width);
+        uint32_t pixel_format = libmegapixels_format_to_v4l_pixfmt(image->format);
         size_t i = 0, padding_offset = 0;
         size_t offset;
-        /* TODO: implement
-        switch (image->pixel_format) {
-        case MP_PIXEL_FMT_BGGR8:
-        case MP_PIXEL_FMT_GBRG8:
-        case MP_PIXEL_FMT_GRBG8:
-        case MP_PIXEL_FMT_RGGB8:
-                for (int y = 0; y < image->height; y += 2) {
-                        for (int x = 0; x < row_length; x += 2) {
-                                data[i++] = image->data[x + row_length * y];
+
+        switch (pixel_format) {
+                case V4L2_PIX_FMT_SBGGR8:
+                case V4L2_PIX_FMT_SGBRG8:
+                case V4L2_PIX_FMT_SGRBG8:
+                case V4L2_PIX_FMT_SRGGB8:
+                        for (int y = 0; y < image->height; y += 2) {
+                                for (int x = 0; x < row_length; x += 2) {
+                                        data[i++] = image->data[x + row_length * y];
+                                }
                         }
-                }
-                break;
-        case MP_PIXEL_FMT_BGGR10P:
-        case MP_PIXEL_FMT_GBRG10P:
-        case MP_PIXEL_FMT_GRBG10P:
-        case MP_PIXEL_FMT_RGGB10P:
-                // Skip 5th byte of each 4-pixel segment by incrementing an
-                // offset every time a 5th byte is reached, making the
-                // X coordinate land on the next byte:
-                //
-                // image->data | | | | X | | | | X | | | | X | | | | X | ...
-                // x           0   2   4   6   8  10  12  14  16  18  20 ...
-                // offset      0       1       2       3       4       5 ...
-                //                     >       --->    ----->  ------->
-                // x + offset  0   2     4   6     8  10    12  16    18 ...
-                for (int y = 0; y < image->height; y += 2) {
-                        offset = 0;
-                        for (int x = 0; x < image->width; x += 2) {
-                                if (x % 4 == 0)
-                                        offset += 1;
-
-                                data[i++] = image->data[x + offset + padding_offset +
-                                                        row_length * y];
+                        break;
+                case V4L2_PIX_FMT_SBGGR10P:
+                case V4L2_PIX_FMT_SGBRG10P:
+                case V4L2_PIX_FMT_SGRBG10P:
+                case V4L2_PIX_FMT_SRGGB10P:
+                        // Skip 5th byte of each 4-pixel segment by incrementing an
+                        // offset every time a 5th byte is reached, making the
+                        // X coordinate land on the next byte:
+                        //
+                        // image->data | | | | X | | | | X | | | | X | | | | X | ...
+                        // x           0   2   4   6   8  10  12  14  16  18  20 ...
+                        // offset      0       1       2       3       4       5 ...
+                        //                     >       --->    ----->  ------->
+                        // x + offset  0   2     4   6     8  10    12  16    18 ...
+                        for (int y = 0; y < image->height; y += 2) {
+                                offset = 0;
+                                for (int x = 0; x < image->width; x += 2) {
+                                        if (x % 4 == 0)
+                                                offset += 1;
+
+                                        data[i++] = image->data[x + offset + padding_offset +
+                                                                row_length * y];
+                                }
+
+                                // Skip padding
+                                padding_offset += padding_bytes * 2;
                         }
-
-                        // Skip padding
-                        padding_offset += padding_bytes * 2;
-                }
-                break;
-        default:
-                assert(0);
+                        break;
+                default:
+                        printf("Preview pixel format not supported - zbar won't work\n");
+                        return;
         }
 
         // Create image for zbar
@@ -261,7 +263,6 @@ process_image(MPPipeline *pipeline, MPZBarImage **_image)
         mp_zbar_image_unref(image);
 
         ++frames_processed;
-         */
 }
 
 void