反映pset4 CS50

时间:2020-04-11 08:58:26

标签: c cs50

所有CS50使用者,

当前,我正在对pset4进行过滤,反射并在我编写的代码中苦苦挣扎。它可以很好地编译,但是输出图片看起来像我所附的图片。有什么线索可以解决这个问题吗? (如果可能的话,没有解决方案,那么提示完全没问题:))

#include "helpers.h"
#include <stdio.h>
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       for (int j = 0; j < (width - 1); j++)
       {
           //get the average
           float average = 0;
           average = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;

           //set color channels with the average value
           image[i][j].rgbtRed = round(average);
           image[i][j].rgbtGreen = round(average);
           image[i][j].rgbtBlue = round(average);
       }
   }
   return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       for (int j = 0; j < (width - 1); j++)
       {
           //calculate the new values
           float red = 0;
           float green = 0;
           float blue = 0;


           red = .393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue;
           if (round(red) > 255)
           {
               image[i][j].rgbtRed = 255;
           }
           else if (round(red <= 255))
           {
               image[i][j].rgbtRed = red;
           }

           green = .349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue;
           if (round(green) > 255)
           {
               image[i][j].rgbtGreen = 255;
           }
           else if (round(green) <= 255)
           {
               image[i][j].rgbtGreen = green;
           }

           blue = .272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue;
           if (round(blue) > 255)
           {
               image[i][j].rgbtBlue = 255;
           }
           else if (round(blue) <= 255)
           {
               image[i][j].rgbtBlue = blue;
           }
       }
   }
   return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
   for (int i = 0; i < (height - 1); i++)
   {
       //RGBTRIPLE

       int a[width - 1 /2];
       int b[width - 1 /2];
       int c[width - 1 /2];

       for (int j = 0; j < ((width/2) - 1); j++)
       {
           a[j] = image[i][(width - 1) - j].rgbtRed;
           b[j] = image[i][(width - 1) - j].rgbtGreen;
           c[j] = image[i][(width - 1) - j].rgbtBlue;
       }

       // GERADE / UNGERADE ZAHLEN VERARBEITEN

       for (int n = 0; (n + (width - 1) /2) < (width - 1); n++)
       {
           image[i][((width - 1)/2) - n].rgbtRed = image[i][((width - 1)/2) - n].rgbtRed;
           image[i][((width - 1)/2) - n].rgbtGreen = image[i][((width - 1)/2) - n].rgbtGreen;
           image[i][((width - 1)/2) - n].rgbtBlue = image[i][((width - 1)/2) - n].rgbtBlue;
       }

       for (int m = 0; m < ((width/2) - 1); m++)
       {
           image[i][m].rgbtRed = a[m];
           image[i][m].rgbtRed = b[m];
           image[i][m].rgbtRed = c[m];
       }
   }
   return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
   return;
}

输入:

./filter -r stadium.bmp outfile.bmp 

(stadium.bmp只是我选择的文件。您可以选择任何文件作为输入)

输出:编译成功,请参见所附图片enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码太复杂。您无需将图像划分为RGB值。您需要从左向右交换图像值,反之亦然,以实现反射。

为此,您可能需要创建一个与“图像”相同的临时数据结构。

temp

使用image,您需要存储//Pseudo code 1. //Loop though the row of an image (start from row = 0). We need to iterate every row. 2. // Loop through the height of an image to save current row in an image to the temp. 3. // Swap the values in image by using the temp. 中的值,并交换它们。以下是您可以使用的设计模式。

val1, val2, valn, val3, val4

如果要在下面过滤此一维图像。

val4, val3, valn, val2, val1

经过反射滤镜后,将是这样。

    import 'package:flutter/material.dart';
    import 'dart:math';
    import 'dart:convert';
    import 'package:http/http.dart' as http;
    import 'dart:async';
    import 'package:flutter_app/menu_member.dart';
    import 'package:flutter_app/globals.dart' as globals;

class Lotto extends StatefulWidget {
  @override
  _LottoState createState() => new _LottoState();
}

class _LottoState extends State<Lotto> {

  @override
  void initState() {
    super.initState();
  }

  var i=1;
  var nb_num=49;
  var no_select=[];
  var no_a_select=5;

  List<Color> colorList = List<Color>.generate(49, (int index) => Colors.lightBlue);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: new AppBar(
            title: new Text('GRILLE DE LOTTO'),
          ),
            body:
            Center(
              child: Column(
                children: <Widget>[
                  Container(
                    width:400,
                    height:30,
                    margin: const EdgeInsets.only(top: 10.0),
                      child : new Text("Selectionnez 5 numéros",textAlign: TextAlign.center,style: TextStyle(fontSize: 30.0),),
                  ),
                  Container(
                    width:400,
                    height:300,
                child: new GridView.count(
                crossAxisCount: 9,
                padding: const EdgeInsets.all(30.0),
                mainAxisSpacing: 4.0,
                crossAxisSpacing: 4.0,
                children: new List<Widget>.generate(49, (index) {
                  return new GestureDetector(
                    onTap: () {
                      setState(() {
                        if (colorList[index] == Colors.lightBlue) {
                          if (no_select.length<no_a_select) {
                            colorList[index] = Colors.redAccent;
                            no_select.add(index+1);
                          }
                          else {
                            showDialog(
                                context: context,
                                builder: (BuildContext context){
                                  return AlertDialog(
                                    title: Text("INFORMATION"),
                                    content: Text("Vous ne pouvez pas sélectionner plus de 5 numéros !!!"),
                                  );
                                }
                            );
                          }
                          print(no_select);
                        }
                        else {
                          colorList[index] = Colors.lightBlue;
                          no_select.remove(index+1);
                          print(no_select);
                        }
                      });
                    },
                    child: Container(
                      child: ClipOval(
                        child: Container(
                          color: colorList[index],
                          height: 20.0,
                          width: 20.0,
                          child: Center(
                            child: new Text((index+1).toString(),
                                style: TextStyle(color: Colors.white, fontSize: 24),
                                textAlign: TextAlign.center),
                          ),
                        ),
                      ),
                    ),
                  );
                }
                ),
              ),
                  ),
                  Container(
                    width:400,
                    height:30,
                    margin: const EdgeInsets.only(top: 10),
                      child : new Text("Vos Numéros",textAlign: TextAlign.center,style: TextStyle(fontSize: 30.0),),
                  ),
                  Container(
                    width:400,
                    height:80,
                    margin: const EdgeInsets.only(top: 10.0),
                    decoration: BoxDecoration(
                      border: Border.all(
                    color: Colors.lightBlueAccent,
                    width: 2,
                  ),
                      borderRadius: BorderRadius.circular(12),
                    ),
                  child:
                    getWidget()
                  ),
                  Container(
                    width:300,
                    height:45,
                    margin: const EdgeInsets.only(top: 10.0),
                    child:
                  RaisedButton(
                    color: Colors.green,
                    textColor: Colors.white,
                    padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
                    child: Text('TIRAGE ALEATOIRE'),
                    onPressed: () {
                      Select_numbers();
                    },
                  ),
                  ),
                  Container(
                    width:300,
                    height:45,
                    margin: const EdgeInsets.only(top: 10.0),
                    child:
                  RaisedButton(
                    color: Colors.green,
                    textColor: Colors.white,
                    padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
                    child: Text('VALIDER VOTRE GRILLE'),
                    onPressed: () {
                      Valide_grille();
                    },
                  ),
                  ),
                  ]
              )
                          ),
                        ),
                    );
  }

  getWidget() {
    if (no_select.length==0) {
      return Text("Pas de numéros");
    }
    else {
      return GridView.count(
          crossAxisCount: 5,
          padding: const EdgeInsets.all(10.0),
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          children: new List<Widget>.generate(no_select.length, (index) {
            return  ClipOval(
            child: Container(
            color: Colors.red,
    height: 20.0,
    width: 20.0,
    child: Center(
    child: new Text((no_select[index].toString()),
    style: TextStyle(color: Colors.white, fontSize: 24),
    textAlign: TextAlign.center),
    ),
    ),
    );
          }
    )
      );
    }
  }

  Select_numbers() {
    setState(() {
      var j = 1;
      var num_sel;
      var pos_sel;
      no_select=[];
      colorList=[];
      colorList=List<Color>.generate(49, (int index) => Colors.lightBlue);
      var rng = new Random();
      List tab=[];
      tab = List.generate(49, (int index) => index + 1);
      print (tab);
      while (j <= no_a_select) {
        pos_sel = rng.nextInt(tab.length-1);
        num_sel=tab[pos_sel];
        no_select.add(num_sel);
        colorList[num_sel-1] = Colors.redAccent;
        tab.remove(num_sel);
        print(tab);
        j++;
      }
      print(no_select);
    });
  }

  Future Valide_grille() async{
    // For CircularProgressIndicator.
    bool visible = false ;
    // Showing CircularProgressIndicator.
    setState(() {
      visible = true ;
    });

    // SERVER LOGIN API URL
    var url = 'https://www.easytrafic.fr/game_app/valide_lotto.php';

    // Store all data with Param Name.
    var data = {'id_membre':globals.id_membre, 'result':no_select};

    print (data);

    var grille_encode=jsonEncode(data);

    print(grille_encode);

    // Starting Web API Call.
    var response = await http.post(url, body: grille_encode,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});

    print(response.body);

    // Getting Server response into variable.
    var message = json.decode(response.body);

    // If the Response Message is Matched.
    if(message == 'OK')
    {
      print('VALIDATION DE LA GRILLE OK');
      // Hiding the CircularProgressIndicator.
      setState(() {
        visible = false;
      });

    }else{
      // Hiding the CircularProgressIndicator.
      setState(() {
        visible = false;
      });

      // Showing Alert Dialog with Response JSON Message.
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Text(message),
            actions: <Widget>[
              FlatButton(
                child: new Text("OK"),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
  }
}