前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux下c++代码fwrite处出现段错误

Linux下c++代码fwrite处出现段错误

原创
作者头像
用户10182912
修改2022-11-11 16:10:13
5.4K1
修改2022-11-11 16:10:13
举报
文章被收录于专栏:C++ OpenGL ffmpegC++ OpenGL ffmpeg

在windows系统下运行下面的代码可以正常运行

但到了linux下,出现段错误

通过gbd调试检测到是fwrite出现的问题(段错误提示在代码下面)

通过打断点检测也确实是fwrite将数据写入流的时候不能写入出现的段错误。

尝试了很多方法都不能解决,求助大佬

#include <GL/glew.h>

#include <GL/freeglut.h>

#include <bits/stdc++.h>

#include <stdlib.h>

#include <GL/glut.h>

#include <ctime>

#include <unistd.h>

#include <opencv2/opencv.hpp>

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace cv;

using namespace std;

Mat I = imread("./pic/h1.png");

int width = I.cols;

int height = I.rows;

GLubyte* pixels;

static GLfloat spin1 = 0.0;    

static GLfloat move1 =0.0;      

static GLfloat size1 = 1.0;

int n = 0;

FILE* pPipe = NULL;

long long lSize = 544 * 960 * 4;

int fpipe;

GLuint load_texture(const char* fileName)

{

    GLuint texture_ID;

    GLubyte* pixels = NULL;

    Mat  dst = imread(fileName, -1);

    Mat img;

if (dst.channels() != 4)

    {

cvtColor(dst, img, CV_BGR2BGRA);

    }

if (dst.channels() == 4)

        img = dst;

    // if (img.empty())

    // {

    //  fprintf(stderr, "Can not load image %s", fileName);

    //  return -1;

    // }

int width = img.cols;

int height = img.rows;

int channel = img.channels();

int pixellength = width * height * channel;

    pixels = new GLubyte[pixellength];

memcpy(pixels, img.data, pixellength *sizeof(char));

glGenTextures(1, &texture_ID);

glBindTexture(GL_TEXTURE_2D, texture_ID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);

glEnable(GL_TEXTURE_2D);

free(pixels);

return texture_ID;

}

GLubyte* SavePNG()

{

    GLubyte * pPixelData = NULL;

    GLint picWidth = 544;

    GLint picHeight = 960;

    pPixelData = new GLubyte[544 * 960 * 4];

if (pPixelData == 0)

return 0;

glReadBuffer(GL_FRONT);

glReadPixels(0, 0, picWidth, picHeight, GL_RGBA, GL_UNSIGNED_BYTE, pPixelData);

return pPixelData;

}

void pipe(GLubyte* data)

{

    cout<<"pipe start"<<endl;

fwrite(data, lSize, 1,  pPipe); //出现段错误,无法向流中写入数据

    cout<<"pipe end"<<endl;

fflush(pPipe);

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    GLuint image = load_texture("./pic/h1.png");

glViewport(0, 0, (GLsizei)width, (GLsizei)height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1, 1);

glEnable(GL_TEXTURE_2D);    

glBindTexture(GL_TEXTURE_2D, image);    

glTranslatef(move1, 0, 0);              

glRotatef(spin1, 0, 0, 1);              

glScalef(size1, size1, 1);          

glBegin(GL_POLYGON);    

glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5, 0.5);    

glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5, -0.5);  

glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5, -0.5);    

glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5, 0.5);

glEnd();    

glDisable(GL_TEXTURE_2D);  

glutSwapBuffers();

}

void spinAndSizeDisplay()

{

    n += 1;

if (spin1 < 180)

    {

        spin1 += 1;

if (spin1 > 180)

        {

            spin1 -= 180;

        }

    }

    GLubyte * data = SavePNG();

pipe(data);

free(data);

    cout<<"pipe"<<endl;

if (n > 200)

    {

        // close(fpipe);

pclose(pPipe);

exit(0);

    }

    //spin1 < -180 ? spin1 += 180 : spin1 -= 1;

    //size1 > 2 ? size1 -= 2 : size1 += 0.003;

    //move1 = move1 > 100 ? move1 -= 100 : move1 += 1;

glutPostRedisplay();                

    //sleep(0.5);

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

    string a = "/usr/local/ffmpeg/bin/ffmpeg -f rawvideo -vcodec rawvideo  -video_size 544x960 -pixel_format rgba -itsoffset 1.5 -i - -i ./pic/db.mp4 -i ./pic/sc/sc_%3d.png -filter_complex [0:v]vflip[a];[1:v][a]overlay[b];[b][2:v]overlay -r 25 -t 14.04 -loglevel quiet -y ./pic/output.mp4";

const char* p = NULL;

    p = a.c_str();

    pPipe = popen(p, "wb");

    // cout << "trans before"<<endl;

    // fpipe = fileno(pPipe);

    // cout << "trans after"<<endl;

glutInitWindowSize(width, height);

glutInitWindowPosition(100, 100);

glutCreateWindow("OpenGL");

glutDisplayFunc(display);

glutIdleFunc(spinAndSizeDisplay);

glutMainLoop();

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档