123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "CameraDataHandleAndShow.h"
- void ImageGrabber::stop() {
- m_running = false;
- };
- long ImageGrabber::GrabImagetest(int iCameraId, ImageInfo& image) {
- static QMutex mutex;
- QMutexLocker locker(&mutex);
-
- static int counter = 0;
- counter = (counter + 1) % 1000;
- const int width = 640;
- const int height = 480;
-
- static QByteArray buffer(width * height * 3, 0);
- unsigned char* ptr = reinterpret_cast<unsigned char*>(buffer.data());
- for(int y = 0; y < height; ++y) {
- for(int x = 0; x < width; ++x) {
- int offset = (y * width + x) * 3;
-
- int modulatedX = (x + counter) % width;
-
- float dist = sqrt(pow(x-width/2,2) + pow(y-height/2,2));
- float pulse = (sin(counter*0.1) + 1) * 50;
-
- float noise = (rand() % 100)/100.0 * 50 * sin(counter*0.05);
-
- ptr[offset] = static_cast<int>(modulatedX*255/width + pulse + noise) % 256;
- ptr[offset+1] = static_cast<int>((x+y+counter)*255/(width+height) + noise) % 256;
- ptr[offset+2] = static_cast<int>(y*255/height + pulse) % 256;
- }
- }
-
- image.width = width;
- image.height = height;
- image.format = RGB888;
- image.channel = 3;
- image.data = ptr;
- return 0;
- }
- void ImageGrabber::run(){
- while(m_running) {
- ImageInfo imgInfo;
- if(GrabImagetest(1, imgInfo) == 0) {
- QImage image = convertImage(imgInfo);
- emit imageGrabbed(image);
- }
- msleep(1000);
- }
- }
- QImage ImageGrabber::convertImage(const ImageInfo &info) {
- QImage::Format format = QImage::Format_Invalid;
- switch(info.format) {
- case GRAY8: format = QImage::Format_Grayscale8; break;
- case RGB888: format = QImage::Format_RGB888; break;
- case ARGB8888:format = QImage::Format_ARGB32; break;
- case RGB32: format = QImage::Format_RGB32; break;
- case YUV422: return convertYUV422ToRGB(info);
- default: return QImage();
- }
- return QImage(info.data, info.width, info.height,
- info.width * info.channel, format).copy();
- }
- QImage ImageGrabber::convertYUV422ToRGB(const ImageInfo &info) {
-
- QVector<unsigned char> rgbData(info.width * info.height * 3);
- for (int y = 0; y < info.height; ++y) {
- for (int x = 0; x < info.width; x += 2) {
-
- int yuvIndex = y * info.width * 2 + x * 2;
-
- unsigned char Y0 = info.data[yuvIndex];
- unsigned char U = info.data[yuvIndex + 1];
- unsigned char Y1 = info.data[yuvIndex + 2];
- unsigned char V = info.data[yuvIndex + 3];
-
- float u = (float)U - 128.0f;
- float v = (float)V - 128.0f;
-
- float yScaled = (Y0 - 16) * 1.164f;
- int r = (int)(yScaled + 1.596f * v);
- int g = (int)(yScaled - 0.391f * u - 0.813f * v);
- int b = (int)(yScaled + 2.018f * u);
-
- int rgbIndex = (y * info.width + x) * 3;
- rgbData[rgbIndex] = (unsigned char)(r < 0 ? 0 : r > 255 ? 255 : r);
- rgbData[rgbIndex + 1] = (unsigned char)(g < 0 ? 0 : g > 255 ? 255 : g);
- rgbData[rgbIndex + 2] = (unsigned char)(b < 0 ? 0 : b > 255 ? 255 : b);
-
- yScaled = (Y1 - 16) * 1.164f;
- r = (int)(yScaled + 1.596f * v);
- g = (int)(yScaled - 0.391f * u - 0.813f * v);
- b = (int)(yScaled + 2.018f * u);
-
- rgbIndex += 3;
- if (x + 1 < info.width) {
- rgbData[rgbIndex] = (unsigned char)(r < 0 ? 0 : r > 255 ? 255 : r);
- rgbData[rgbIndex + 1] = (unsigned char)(g < 0 ? 0 : g > 255 ? 255 : g);
- rgbData[rgbIndex + 2] = (unsigned char)(b < 0 ? 0 : b > 255 ? 255 : b);
- }
- }
- }
-
- QImage image(rgbData.data(), info.width, info.height, QImage::Format_RGB888);
- return image;
- }
|