commit 7dfa730f22f543985f7505ed65ab5f45b830fca4
parent a2640ad729188cec92bbb44e83bbda9be656c66d
Author: Adam M <aemalone@gmail.com>
Date: Thu, 6 Aug 2020 22:30:50 -0500
animated gif works but crashes
Diffstat:
2 files changed, 92 insertions(+), 29 deletions(-)
diff --git a/src/ComputerscareBlank.cpp b/src/ComputerscareBlank.cpp
@@ -28,6 +28,7 @@ struct ComputerscareBlank : Module {
int currentFrame = 0;
int numFrames = 0;
int stepCounter = 0;
+ int frameDelay=10000;
int speed = 100000;
ComputerscareSVGPanel* panelRef;
@@ -56,7 +57,7 @@ struct ComputerscareBlank : Module {
}
void process(const ProcessArgs &args) override {
stepCounter++;
- if (stepCounter > speed) {
+ if (stepCounter > frameDelay) {
stepCounter = 0;
if(numFrames > 1) {
currentFrame ++;
@@ -84,17 +85,24 @@ struct ComputerscareBlank : Module {
void setPath(std::string path, int index = 0) {
//if (paths.size() <= index) {
- paths.push_back(path);
+ //paths.push_back(path);
//}
//else {
- // paths[index] = path;
+ paths[index] = path;
//}
printf("setted %s\n", path.c_str());
- numFrames = paths.size();
- currentFrame = numFrames - 1;
+ //numFrames = paths.size();
+ currentFrame = 0;
+ }
+ void setFrameCount(int frameCount) {
+ numFrames=frameCount;
+ }
+ void setFrameDelay(int frameDelayCentiseconds) {
+ frameDelay=frameDelayCentiseconds;
}
std::string getPath() {
- return numFrames > 0 ? paths[currentFrame] : "";
+ //return numFrames > 0 ? paths[currentFrame] : "";
+ return paths[0];
}
json_t *dataToJson() override {
@@ -136,7 +144,7 @@ struct ComputerscareBlank : Module {
// currentFormula[i] = val;
//currentTextFieldValue[i] = val;
//paths.push_back(val);
- setPath(val, i);
+ setPath(val, 0);
}
}
}
@@ -218,6 +226,8 @@ struct PNGDisplay : TransparentWidget {
int lastEnum = -1;
std::string path = "empty";
int img = 0;
+ int currentFrame=-1;
+ AnimatedGifBuddy gifBuddy;
PNGDisplay() {
}
@@ -254,7 +264,11 @@ struct PNGDisplay : TransparentWidget {
//printf("%s\n", modulePath.c_str());
if (path != modulePath) {
//img = nvgCreateImage(args.vg, modulePath.c_str(), 0);
- img = animatedGifCreateImage(args.vg, modulePath.c_str(), 0);
+ gifBuddy = AnimatedGifBuddy(args.vg, modulePath.c_str());
+ img = gifBuddy.getHandle();
+ blankModule->setFrameCount(gifBuddy.getFrameCount());
+ blankModule->setFrameDelay(gifBuddy.getFrameDelay());
+
nvgImageSize(args.vg, img, &imgWidth, &imgHeight);
imgRatio = ((float)imgWidth / (float)imgHeight);
@@ -279,6 +293,10 @@ struct PNGDisplay : TransparentWidget {
nvgFill(args.vg);
nvgClosePath(args.vg);
}
+ if(blankModule->currentFrame != currentFrame) {
+ currentFrame = blankModule->currentFrame;
+ gifBuddy.displayGifFrame(args.vg,currentFrame);
+ }
}
}
};
diff --git a/src/animatedGif.hpp b/src/animatedGif.hpp
@@ -7,6 +7,7 @@
#include "stb_image.h"
#include "nanovg.h"
+#include "dtpulse.hpp"
//#include "stb_image_write.h"
@@ -19,7 +20,7 @@ typedef struct gif_result_t {
-STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *frames)
+STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *frames, std::vector<unsigned char*> &framePointers)
{
FILE *f;
stbi__context s;
@@ -42,9 +43,9 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
*frames = 0;
- while (gr->data = stbi__gif_load_next(&s, &g, &c, 4))
+ while ((gr->data = stbi__gif_load_next(&s, &g, &c, 4)))
{
- printf("loading gif frame %i\n",frames);
+
if (gr->data == (unsigned char*)&s)
{
gr->data = 0;
@@ -56,6 +57,8 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
prev = gr;
gr = (gif_result*) stbi__malloc(sizeof(gif_result));
memset(gr, 0, sizeof(gif_result));
+ printf("loading gif frame %i, delay:%i/100s\n", *frames, g.delay);
+ printf("gr:%i, size:%i\n", gr, sizeof(gif_result));
++(*frames);
}
@@ -76,20 +79,24 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
{
unsigned int size = 4 * g.w * g.h;
unsigned char *p = 0;
+ printf("malloc amount %i\n", *frames * (size + 2));
result = (unsigned char*)stbi__malloc(*frames * (size + 2));
+ printf("result:%i, frames:%i, size:%i\n", result, *frames, size);
gr = &head;
p = result;
-
+ int counter = 0;
while (gr)
{
prev = gr;
+ printf("p:%i, &p:%i, *p:%i\n", p, &p, *p);
+ framePointers.push_back(p);
memcpy(p, gr->data, size);
p += size;
*p++ = gr->delay & 0xFF;
*p++ = (gr->delay & 0xFF00) >> 8;
gr = gr->next;
-
+ counter++;
STBI_FREE(prev->data);
if (prev != &head) STBI_FREE(prev);
}
@@ -97,6 +104,7 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
}
else
{
+ printf("NOT A GIF\n");
result = stbi__load_main(&s, x, y, frames, 4);
*frames = !!result;
}
@@ -104,20 +112,57 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
fclose(f);
return result;
}
-int animatedGifCreateImage(NVGcontext* ctx, const char* filename, int imageFlags)
-{
- int w, h, n, image;
- unsigned char* img;
- int frame = 1;
- stbi_set_unpremultiply_on_load(1);
- stbi_convert_iphone_png_to_rgb(1);
- //img = stbi_load(filename, &w, &h, &n, 4);
- img = stbi_xload(filename, &w, &h, &frame);
- if (img == NULL) {
+
+
+
+
+
+struct AnimatedGifBuddy {
+ std::vector<unsigned char*> framePointers;
+ int imageHandle;
+ bool initialized=false;
+ AnimatedGifBuddy() {
+
+ }
+ AnimatedGifBuddy(NVGcontext* ctx, const char* filename) {
+ imageHandle = animatedGifCreateImage(ctx, filename, 0);
+ }
+ int getHandle() {
+ return imageHandle;
+ }
+ int animatedGifCreateImage(NVGcontext* ctx, const char* filename, int imageFlags) {
+ int w, h, n, image;
+ unsigned char* img;
+ int frame = 0;
+ stbi_set_unpremultiply_on_load(1);
+ stbi_convert_iphone_png_to_rgb(1);
+ //img = stbi_load(filename, &w, &h, &n, 4);
+ framePointers = {};
+ img = stbi_xload(filename, &w, &h, &frame, framePointers);
+ printf("loaded %i frames\n", framePointers.size());
+ //printVector(framePointers);
+ if (img == NULL) {
// printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
- return 0;
+ return 0;
+ }
+ printf("width:%i, height:%i\n", w, h);
+ image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
+ stbi_image_free(img);
+ initialized=true;
+ return image;
+ }
+ void displayGifFrame(NVGcontext* ctx, int frameNumber) {
+ if(initialized) {
+
+
+ const unsigned char* dataAtFrame = framePointers[frameNumber];
+ nvgUpdateImage(ctx, imageHandle, dataAtFrame);
+ }
+ }
+ int getFrameCount() {
+ return (int)framePointers.size();
+ }
+ int getFrameDelay() {
+ return 1764;
}
- image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
- stbi_image_free(img);
- return image;
-}
-\ No newline at end of file
+};
+\ No newline at end of file